ExpandableListView (二级列表)使用demo
2016-11-04 15:40
316 查看
1.先建立两个item布局 分别为groupItem 和 childItem
2.在主函数xml中创建ExpandableListView 该控件在V7包下
3.写Adapter
4.布局代码:
groupItem:
</LinearLayout>
childItem:
</LinearLayout>
xml布局代码:
</LinearLayout>
Adapter代码:
主函数代码(Fragment中):
添加数据,删除数据的自定义方法(适配器代码):
主函数代码:
demo效果:
2.在主函数xml中创建ExpandableListView 该控件在V7包下
3.写Adapter
4.布局代码:
groupItem:
<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:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/group_name"/>
</LinearLayout>
childItem:
<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:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/child_name_second"/> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/child_sex_second"/>
</LinearLayout>
xml布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/expandablelistview_second"/>
</LinearLayout>
Adapter代码:
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.tianhe.mydemo.second.SecondChildBean; import com.tianhe.mydemo.second.SecondGroupBean; import java.util.ArrayList; /** * Created by dllo on 15/12/29. * ExpandableListView(二级列表) */ public class SecondExpandableBaseAdapter extends BaseExpandableListAdapter { //定义二个实体类承载数据 //一为groupitem,一为childitem private ArrayList<SecondGroupBean> groupBeans; private ArrayList<SecondChildBean> childBeans; private Context context; //构造方法,将实体类的值传到主函数中 public SecondExpandableBaseAdapter(ArrayList<SecondGroupBean> groupBeans, ArrayList<SecondChildBean> childBeans, Context context) { this.groupBeans = groupBeans; this.childBeans = childBeans; this.context = context; } //item的数量 为groupitem的size @Override public int getGroupCount() { return groupBeans.size(); } /** *在ExpandableListView中 继承了BaseExpandableListAdapter之后 * 会复写是个方法 其中前两个方法的返回值需要填写 其他的方法都不需要更改 * 其次是 getGroupView和getChildView按照ListView的getView填写 * 需要两个缓存类 * 缓存类的通用方法都类似 记住模式 */ @Override public int getChildrenCount(int groupPosition) { return groupPosition; } @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_second_group, parent, false); //切记 此处别忘了缓存类的初始化 groupViewHolder = new GroupViewHolder(); groupViewHolder.groupName = (TextView) convertView.findViewById(R.id.group_name); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } groupViewHolder.groupName.setText(groupBeans.get(groupPosition).getName()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_second_child, parent, false); //切记 此处别忘了缓存类的初始化 childViewHolder = new ChildViewHolder(); childViewHolder.childNmae = (TextView) convertView.findViewById(R.id.child_name_second); childViewHolder.childSex = (TextView) convertView.findViewById(R.id.child_sex_second); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } childViewHolder.childNmae.setText(childBeans.get(childPosition).getName()); childViewHolder.childSex.setText(childBeans.get(childPosition).getSex()); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { TextView groupName; } class ChildViewHolder { TextView childNmae, childSex; } }
主函数代码(Fragment中):
import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import com.tianhe.mydemo.second.SecondChildBean; import com.tianhe.mydemo.second.SecondGroupBean; import java.util.ArrayList; /** * Created by dllo on 15/12/29. * fragment中为主函数构建ExpandableListView * 创建了Fragment生命周期 */ public class SecondFragment extends Fragment{ private Context context; private ArrayList<SecondGroupBean> groupBeans; private ArrayList<SecondChildBean> childBeans; private ExpandableListView mExpandableListView; private SecondExpandableBaseAdapter mSecondExpandableBaseAdapter; @Override public void onAttach(Context context) { super.onAttach(context); this.context = context; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mExpandableListView = (ExpandableListView) getActivity().findViewById(R.id.expandablelistview_second); groupBeans = new ArrayList<>(); for (int i = 0; i < 30; i++) { groupBeans.add(new SecondGroupBean("我是第" + i + "老爹")); } childBeans = new ArrayList<>(); for (int j = 0; j < 30; j++) { childBeans.add(new SecondChildBean("我是第" + j + "儿子","我是第" + j + "小儿子")); } mSecondExpandableBaseAdapter = new SecondExpandableBaseAdapter(groupBeans,childBeans,context); mExpandableListView.setAdapter(mSecondExpandableBaseAdapter); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_second,null); return view; } }
添加数据,删除数据的自定义方法(适配器代码):
//向已有的组加信息 public void addData(String newData,int group,int childPos){ child.get(group).add(childPos,newData); notifyDataSetChanged(); } //插入新的组并插入信息 public void addData(String groupName,String childData,int groupPos){ //新的这一组加到groupPos的位置,组名是groupName group.add(groupPos,groupName); //为这新的一组,创建一个新的集合,装着child的数据 List<String> newChild = new ArrayList<>(); //向这个新的集合里添加数据 newChild.add(childData); //将这个新的集合加到指定的组的位置 child.add(groupPos, newChild); //通知适配器,刷新数据 notifyDataSetChanged(); } public void delData(int grouppos) { //通知adapter,position位置数据被删除 if (group.size() <= grouppos) { return; } notifyDataSetChanged(); group.remove(grouppos); }
主函数代码:
//guroup组添加监听 mExpandableListView.setOnGroupClickListener(this); //长按监听 mExpandableListView.setOnItemLongClickListener(this); //子项添加监听 mExpandableListView.setOnChildClickListener(this);
////返回false则子项可以打开,返回true可以获取焦点,但是子项不能打开 @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Toast.makeText(context, "group" + groupPosition, Toast.LENGTH_SHORT).show(); return false; } //长按监听事件中,返回true为正确 若同时有短按监听和长按监听,返回false则都会相应 @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { mSecondExpandableBaseAdapter.delData(position); return true; } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(context, "child" + childPosition, Toast.LENGTH_SHORT).show(); return true; }
demo效果:
相关文章推荐
- Android::ExpandableListView二级列表控件的使用
- 二级列表ExpandableListView的使用
- 追溯-----ExpandableListView,二级列表的基本使用
- Android电商项目 ExpandableListView(二级列表)实现购物车(高仿淘宝) demo
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- android下ExpandableListView的详细使用demo
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- Android ExpandableListView展开列表控件使用实例
- android端获取网络数据添加到二级列表ExpandableListView
- 二级列表ExpandableListView
- 仿QQ列表--Android控件ExpandableListView的使用
- android ExpandableListView二级列表
- 【深入篇】自定义ExpandableListView,实现二级列表效果
- ExpandableListView(二级ListView)的使用
- ExpandableListView的使用,实现类似QQ好友列表
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- android_ExpandableListView 下拉列表使用
- 05.实例篇:仿QQ好友列表——ExpandableListView可扩展列表的使用(上篇)
- Android多级列表ExpandableListView使用实例
- ExpandableListView 二级展开列表