热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android仿京东分类效果

这篇文章主要为大家详细介绍了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 listinfos;

  public MyAdapter(Context context, List 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

<&#63;xml version="1.0" encoding="utf-8"&#63;>


  

Maintivity

/**
 * 
 * @author yihuanxing
 *
 */
public class MainActivity extends FragmentActivity implements OnItemClickListener {
  List listinfoInfos=new ArrayList();
  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<&#63;> parent, View view, int position,
      long id) {

     PersionInfo info = listinfoInfos.get(position);
    for (int i = 0; i 

6 main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


  

    
  

  

  

    

    

    
  

效果图:

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


推荐阅读
author-avatar
轩轩20110804
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有