ListView 或 Recyclerview 子项里面再添加子项
来源:互联网 发布:楠木鞋架淘宝 编辑:程序博客网 时间:2024/05/16 10:46
今天我写下关于,ListView 或 Recyclerview 子项里面再添加子项,或者是点击子项,展开该子项的子项信息,先看看xml代码
下面是MaintActivit里面的代码
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
然后看看item里面的布局
<?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"> <TextView android:id="@+id/bean_title" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/ll_order" android:visibility="gone" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorAccent" /></LinearLayout>
我这个里面放了一个 LinearLayout,设置它是不可以见的,然后通过代码的方式去addView进入LinearLayout,然后再看看LinearLyout里面我的View
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/order_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/order_message" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
OK,xml代码写完了,然后看看代码吧,嘻嘻
MainActivity:
private ListView list; private Adapter adapter; private List<Bean> beanList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); beanList = new ArrayList<>(); list = (ListView) this.findViewById(R.id.list); for (int i=0;i<30;i++){ List<Order> orderList = new ArrayList<>(); Bean bean = new Bean(); bean.setTitle("天猫"+i); if (i%2==0){ Order order = new Order(); order.setTitle("京东"+i); order.setTitle("京东信息"+i); Order order2 = new Order(); order2.setTitle("京东2"+i); order2.setTitle("京东信息2"+i); orderList.add(order); orderList.add(order2); }else { Order order = new Order(); order.setTitle("京东"+i); order.setTitle("京东信息"+i); orderList.add(order); } bean.setOrderList(orderList); beanList.add(bean); } adapter = new Adapter(beanList,this); list.setAdapter(adapter); }
我觉得上面的大家都看得懂
然后看看关键的Adapter
Adapter:
private List<Bean> beanList; private Activity mActivity; int currentItem=-1; public Adapter(List<Bean> beanList, Activity mActivity) { this.beanList = beanList; this.mActivity = mActivity; } @Override public int getCount() { return beanList.size(); } @Override public Bean getItem(int position) { return beanList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; Bean bean = beanList.get(position); if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_bean, parent, false); holder.textView = (TextView) convertView.findViewById(R.id.bean_title); holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.ll_order); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(bean.getTitle()); setItmes(bean.getOrderList(), holder.linearLayout); //控制条目隐藏和显示 if (currentItem==position){ holder.linearLayout.setVisibility(View.VISIBLE); }else { holder.linearLayout.setVisibility(View.GONE); } holder.textView.setTag(position); holder.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int tag = (int) v.getTag(); if (tag == currentItem) { currentItem = -1; } else { currentItem = tag; } notifyDataSetChanged();//数据源改变 } }); return convertView; }//给LinearLyout 添加子视图 private void setItmes(List<Order> orderList, LinearLayout linearLayout) { if (orderList == null || orderList.isEmpty()) {//如果没有子项,就删除子项容器里原有的子项视图 linearLayout.removeAllViews(); return; } int childCount = linearLayout.getChildCount(); int itemSize = orderList.size();//这里的目的是为了防止linearLayout 无限制的增加,因为这个是用在复用里面的 if (itemSize > childCount) { for (int i = 0; i < (itemSize - childCount); i++) { linearLayout.addView(LayoutInflater.from(mActivity).inflate(R.layout.item_order, null)); } } else if (itemSize < childCount) { for (int i = 0; i < (childCount - itemSize); i++) { linearLayout.removeViewAt(0); } } for (int i = 0; i < orderList.size(); i++) { View iView = linearLayout.getChildAt(i); TextView title = (TextView) iView.findViewById(R.id.order_title); TextView message = (TextView) iView.findViewById(R.id.order_message); title.setText(orderList.get(i).getTitle()); message.setText(orderList.get(i).getMessage()); } } static class ViewHolder { TextView textView; LinearLayout linearLayout; }
好了,代码都写完了
阅读全文
0 0
- ListView 或 Recyclerview 子项里面再添加子项
- RecyclerView和ListView子控件缓存要点
- ListView拖动(或长按拖动)子项item,更改位置,且可以左右滑删除子项item
- 关于为使用ArrayAdapter创建ListView时,使用TextView添加子项的问题
- 为ListView子视图添加动画
- 更改ListView子项中的EditText
- 【issue】listView子项不能获取焦点
- JavaScript 添加或删除子节点、子元素
- 如何通过使用注册项 (.reg) 文件添加、修改或删除注册表子项和值
- 使用REG注册表文件添加、修改或删除注册表键值和子项介绍
- 使用REG注册表文件添加、修改或删除注册表键值和子项介绍
- 使用REG注册表文件添加、修改或删除注册表键值和子项介绍
- ExpandableListActivity实现展开或隐藏子项
- select动态添加子选择项option
- 菜单项下添加子菜单
- duilib combo添加子项简化
- ScrollView里面嵌套ListView,RecyclerView
- 分享+ 在根视图scrollView里面添加子视图
- SQL Server 2017, Cluster-less AlwaysOn
- java: switch中的参数类型
- Java 使用 switch 语句和枚举类型
- 字符串匹配——KMP算法
- EMAIL服务器的创建与配置
- ListView 或 Recyclerview 子项里面再添加子项
- MSYQL学习笔记(一) ----数据库基本操作
- html创建菜单列表
- cnn architecture
- ThreadLocal和InheritableThreadLocal的区别
- c语言中结构体对齐详解
- 史上最最简单的登录注册方法(Bomb后端云)
- 线程的让步(礼让)
- redis源码阅读笔记(一):sds