前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android仿京东分类效果

Android仿京东分类效果

作者头像
砸漏
发布2020-11-05 11:36:23
1K0
发布2020-11-05 11:36:23
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

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

1.写一个fragment

代码语言:javascript
复制
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.写一个实体类:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
/**
 * 
 * @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

代码语言:javascript
复制
<?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 

效果图:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档