Android仿京东分类效果
作者:jun_tong 发布时间:2021-06-27 23:07:55
标签: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>
效果图:
来源:https://blog.csdn.net/jun_tong/article/details/80756186


猜你喜欢
- 1.概述前几天群里有人问如何制作备忘录,感觉这样一个小实例挺适合新手们入门学习使用,所以就抽空做了出来。界面如下图这个备忘录主要包括了如下功
- 3.3 do...while()循环3.3.1 do语句的语法:do
- 字符串采用unicode编码的方式时,计算字符串长度的方法找出UNICODE编码中的汉字的代表的范围“\u4E00” 到“\u9FBB”之间
- 在微服务架构下,我们在完成一个订单流程时经常遇到下面的场景:一个订单创建接口,第一次调用超时了,然后调用方重试了一次在订单创建时,我们需要去
- 前言Android 8.0系统更新之后,app的更新将不再像之前的系统版本一样能够直接下载安装包之后直接安装(以前安装未知来源应用的时候一般
- 前言在工作中有遇到这样的需求,需要把hmtl的富文本内容,进行深色模式适配。原先的富文本内容是在直接在webview上进行展示。解决思路:替
- 前端使用的是vue+elementui,这款系统只适合学习巩固SpringBoot+VUE,后面还要在这上面加校园公告、校园零食等功能,后期
- 1. 为什么写这篇文章?事情是这样的,在 2021年6月10日早上我在CSDN上发布了文章《你真的懂Java怎么输出Hello World吗
- Android输入框实时模糊搜索很多开发场景会用到搜索框实时模糊搜索来帮助用户输入内容,如图思路是在EditText 字符变动的时候 弹出L
- string 类型是C#的基元类型之一,它是一个引用类型,对应FCL中的System.String类型。string 类型和普通的引用类型相
- 今天被数据大神说了,对接第三方接口返回的json字段我想用驼峰形式,他说我这样不专业。所以就改了,认怂。记住以后再次对接rest接口,返回的
- using System;using System.Drawing;namespace PubLib{ /// <summa
- 本文实例讲述了Android返回键功能的实现方法。分享给大家供大家参考。具体如下:在开发android应用时,常常通过按返回键(即keyCo
- 前两天给同事做 code review,感觉自己对 Java 的 Generics 掌握得不够好,便拿出 《Effective Java》1
- 这篇文章写的非常好,深入浅出,关键还是一位大三学生自己剖析的心得。这是我喜欢此文的原因。下面请看正文:作为一个大三的预备程序员,我学习and
- java数据结构的堆什么是堆堆指的是使用数组保存完全二叉树结构,以层次遍历的方式放入数组中。如图:注意:堆方式适合于完全二叉树,对于非完全二
- 1.先看源码文档/** * Indicates that an annotation type is automatically inher
- 两个小解释:FileProvider是ContentProvider特殊的子类,ContentProvider通过创建content://
- 本文实例为大家分享了java利用udp实现发送数据的具体代码,供大家参考,具体内容如下1.udp的特点数据以包的形式发送数据udp是面向无连
- 1.定义Token的注解,需要Token校验的接口,方法上加上此注解import java.lang.annotation.ElementT