Android仿京东分类效果

作者:jun_tong 时间:2021-06-27 23:07:55 

本文实例为大家分享了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>

效果图:

Android仿京东分类效果

来源:https://blog.csdn.net/jun_tong/article/details/80756186

标签:Android,京东分类
0
投稿

猜你喜欢

  • Java利用Dijkstra算法求解拓扑关系最短路径

    2021-08-24 10:30:54
  • 通过Feign进行调用@FeignClient 找不到的解决方案

    2023-08-18 19:06:52
  • C#、vb.net及SQL判断指定年份是否为闰年的方法

    2023-05-18 09:49:55
  • 浅谈Action+Service +Dao 功能

    2023-01-04 13:44:20
  • 在IDEA中安装MyBatis Log Plugin插件,执行mybatis的sql语句(推荐)

    2022-02-17 08:04:43
  • C#实现XML文件读取

    2023-03-06 13:38:44
  • Android Studio中生成aar文件及本地方式使用aar文件的方法

    2022-06-14 23:43:59
  • Android实现语音合成与识别功能

    2023-10-01 01:41:00
  • C#类中static变量用法分析

    2022-06-20 16:14:23
  • spring boot 静态资源处理方法

    2022-07-14 22:12:42
  • Java中控制流程语句的深入讲解

    2022-07-21 15:29:25
  • C++中左值和右值的区别详解

    2021-09-11 00:07:42
  • 详解Spring Data Jpa当属性为Null也更新的完美解决方案

    2022-04-13 03:06:06
  • Android开发Jetpack组件DataBinding用例详解

    2021-10-08 02:17:17
  • SpringBoot项目没有把依赖的jar包一起打包的问题解决

    2021-08-11 12:15:02
  • MyBatis中的properties配置(推荐)

    2023-06-04 20:47:58
  • Mybatis核心组成部分之SQL映射文件揭秘详解

    2023-08-22 18:45:21
  • java gui详解贪吃蛇小游戏实现流程

    2023-10-08 07:12:24
  • Java使用Collections.sort对中文进行排序方式

    2021-09-17 15:05:46
  • Java代码实现简单酒店管理系统

    2023-11-21 03:38:57
  • asp之家 软件编程 m.aspxhome.com