Android仿京东分类效果

本文实例为大家分享了Android仿京东分类效果展示的具体代码,供大家参考,具体内容如下

1.写一个fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.myfragment, null);
    TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
    //得到数据
    PersionInfo info = (PersionInfo) getArguments().getSerializable("info");
    tv_title.setText(info.getNameString());
    return view;
  }
}

2.写一个实体类:

public class PersionInfo implements Serializable {

  private String nameString;
  private boolean chick;  //标识

  public PersionInfo(String nameString) {
    this.nameString = nameString;
  }

  public String getNameString() {
    return nameString;
  }

  public void setNameString(String nameString) {
    this.nameString = nameString;
  }

  public boolean isChick() {
    return chick;
  }

  public void setChick(boolean chick) {
    this.chick = chick;
  }

}

3.写一个adapter

public class MyAdapter extends BaseAdapter {

  private Context context;
  private List<PersionInfo> listinfos;

  public MyAdapter(Context context, List<PersionInfo> listinfos){
    this.context =context;
    this.listinfos = listinfos;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return listinfos.size();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listinfos.get(position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
    TextView tv = (TextView) convertView.findViewById(R.id.tv);
    PersionInfo persionInfo = listinfos.get(position);
    tv.setText(persionInfo.getNameString());
    if (persionInfo.isChick()) {
      convertView.setBackgroundResource(R.drawable.tongcheng_all_bg01);
    } else {
      convertView.setBackgroundColor(Color.parseColor("#f4f4f4"));
    }
    return convertView;
  }
}

4.adapter所需的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:minHeight="60dp"
  android:gravity="center"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

</LinearLayout>

Maintivity

/**
 *
 * @author yihuanxing
 *
 */
public class MainActivity extends FragmentActivity implements OnItemClickListener {
  List<PersionInfo> listinfoInfos=new ArrayList<PersionInfo>();
  private ListView listView;
  private MyAdapter adapter;
  private MyFragment myFragment;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
  }

  /**
   * 初始化view
   */
  private void initView() {
    // TODO Auto-generated method stub
    for (int i = 0; i < 10; i++) {
      PersionInfo info=new PersionInfo("常用分类"+i);
      listinfoInfos.add(info);
    }

    //
    listView = (ListView) findViewById(R.id.listview);
    //默认
    listinfoInfos.get(0).setChick(true);
    adapter = new MyAdapter(this, listinfoInfos);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(this);

    //创建MyFragment对象
    myFragment = new MyFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, myFragment);

    Bundle mBundle=new Bundle();
    mBundle.putSerializable("info", listinfoInfos.get(0));
    myFragment.setArguments(mBundle);
    fragmentTransaction.commit();
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {

     PersionInfo info = listinfoInfos.get(position);
    for (int i = 0; i < listinfoInfos.size(); i++) {
      if(listinfoInfos.get(i).getNameString().equals(info.getNameString())){
        listinfoInfos.get(i).setChick(true);
      }else {
        listinfoInfos.get(i).setChick(false);
      }
    }

    adapter.notifyDataSetChanged();

    //
    //创建MyFragment对象
    myFragment = new MyFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, myFragment);

    Bundle mBundle=new Bundle();
    mBundle.putSerializable("info", listinfoInfos.get(position));
    myFragment.setArguments(mBundle);
    fragmentTransaction.commit();
  }
}

6 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <RelativeLayout
    android:id="@+id/jzfw_top_layout"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:background="@drawable/home_title"
    android:gravity="center_vertical" >

    <TextView
      android:id="@+id/jzfw_top_layout_02"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:text="仿京东分类"
      android:textColor="#ffffff"
      android:textSize="20sp" />
  </RelativeLayout>

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#cdcdcd" />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fbfbfb"
    android:orientation="horizontal" >

    <ListView
      android:id="@+id/listview"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:scrollbars="none"
      android:layout_weight="1.0"
      android:background="#f4f4f4" />

    <View
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#cdcdcd" />

    <FrameLayout
      android:id="@+id/fragment_container"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="3.0" />
  </LinearLayout>

</LinearLayout>

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现京东App分类页面效果

    今天群里有人问了关于仿京东App分类页面的实现,而我之前正好查过这方面的资料,手上正好有一个demo,正好分享给大家看看,个人觉得效果棒棒哒! 首先来看效果图吧 是不是很6呢,来分析这个demo的主体构成吧,顶部为搜索栏,左侧是scroview,不要担心优化问题,因为scroview里面的TextView是动态生成的,完全不用担心优化问题,右侧是viewPager,Scroview可以控制viewPager的滑动,反之ViewPager也可以控制scroview的滑动. 闲话少说,直接上代码:

  • Android仿京东分类模块左侧分类条目效果

    本文实例为大家分享了Android仿京东左侧分类条目效果的具体代码,供大家参考,具体内容如下 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import com.frame.R;

  • Android使用Scroll+Fragment仿京东分类效果

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 实现思路:首先说下布局,整个是一个横向的线性布局,左边是一个ScrollView,右边是一个FrameLayout,在代码中动态向ScrollView中添加TextView,然后根据TextView的点击事件使用Fragment替换FrameLayout 首先看下布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr

  • Android 仿京东、拼多多商品分类页的示例代码

    最近接了一个项目,要仿照京东写一个商品分类页,但需要滑动右边子分类,左边的主分类也跟着变换,写了个demo,需要的同学可以自取. 先放一个写完之后的样子: 写这个需求的思路也很清晰,首先左边肯定是一个listView,右边也是一个listView,这两个listView要达到一个联动的效果.右边的listView再嵌套一个GridView即可.如下图所示. 所以,我们需要的数据结构也就确定了,应该是数组套数组,也就说护肤大分类下又有子分类商品,类似于这个样子: ok,数据和UI结构确定了,就可以

  • Android仿京东分类效果

    本文实例为大家分享了Android仿京东分类效果展示的具体代码,供大家参考,具体内容如下 1.写一个fragment import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

  • Android仿京东手机端类别页

    京东手机端的类别标签页, 是一个左侧滑动可选择类别, 右侧一个类别明细的列表联动页面. 当用户选择左侧选项, 可在右侧显示更多选项来选择. 实现方式也不少. 最常见的当然是左侧和右侧各一个Fragment, 左侧Fragment放置ListView, 右侧放显示类别明细的Fragment. 如果觉得页面包含的Fragment太多, 左侧直接给一个ListView就可以了.不影响效果. 效果图: 例子中值得注意的三点: 左侧列表点击某个Item可以自动上下滑动,使所点击的item自动移至列表中间

  • jQuery简单实现仿京东分类导航层效果

    本文实例讲述了jQuery简单实现仿京东分类导航层效果.分享给大家供大家参考,具体如下: <script src="jquery-1.11.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var alink01 = $(".item > ul"); al

  • Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)

    京东商城的底部布局的选择效果看上去很复杂,其实很简单,这主要是要感谢 selector 选择器,本文将讲解仿照京东商城的底部布局的选择效果,如何实现 selector 选择器,在不同的状态下,给 view 设置不同的背景. 京东商城底部布局的选择效果如下. View主要的几种状态 主要状态有8种,设置状态的代码以及相应的含义如下. android:state_pressed = "true/false" //true表示按下状态,false表示非按下状态. android:state_

  • Android仿京东快报信息滚动效果

    先来看看效果吧,Android仿京东快报信息滚动效果,具体内容如下 (截图效果不是很好,但是差不多出来了) 代码: package com.test.scrolltransptoolbar; import android.content.Context; import android.graphics.Color; import android.os.Handler; import android.text.TextUtils; import android.util.AttributeSet;

  • Android通过实现GridView的横向滚动实现仿京东秒杀效果

    实现GridView的横向滚动 效果如下图: 具体实现的代码 •1. 主界面布局代码:activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

  • android仿京东商品属性筛选功能

    筛选和属性选择是目前非常常用的功能模块:几乎所有的APP中都会使用: 点击筛选按钮会弹出一个自己封装好的popupWindow,实用方法非常简单:两行代码直接显示:(当然初始化数据除外) 这里和以前用到的流式布局有些不一样:流式布局 以前使用的是单个分类,而且也没有在项目中大量实用:这个筛选功能除了数据外几乎都是从项目中Copy出来的: 整个popupWindow布局就是一个自定义的ListView,这个自定义的listview主要是控制listview的高度: 如果数据少的话就是自适应,如果数

  • Android仿京东淘宝自动无限循环轮播控件思路详解

    在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于RelativeLayout,首先要考虑的就是自定义的控件需要扩展那些属性,把这些属性列出来.在这里是要实现类似于京东淘宝的无限轮播广告栏,那么首先想到的就是轮播的时长.轮播指示器的样式等等.我在这里列举了一些并且结合到了代码中. 1.扩展属性 (1)是否开启自动轮播的功能. (2)指示器的图形样式,一

随机推荐