android-ExpandableListView的使用
来源:互联网 发布:零壹乐队 知乎 编辑:程序博客网 时间:2024/05/10 01:13
介绍
含有Expandable功能的ListView
看图
主界面
<ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="match_parent"> </ExpandableListView>
父界面
# content_expandable_list_view.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"> <TextView android:id="@+id/parent_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="这是父item" android:textColor="#000" android:textSize="20sp" android:textStyle="bold" /></LinearLayout>
子界面
# content_expandable_list_view_child.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"> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="这是子item" android:textColor="#000" android:textSize="18sp" /></LinearLayout>
代码分解
设置Adapter
ExpandableListView expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview); expandablelistview.setAdapter(new MyExpandableListViewAdapter());
设置条目点击事件
expandablelistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(ExpandableListViewActivity.this, "onChildClick" + childPosition, Toast.LENGTH_SHORT).show(); return true; } });
设置长按事件
expandablelistview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { String content = ""; if ((int) view.getTag(R.layout.content_expandable_list_view) == -1) { content = "父类第" + view.getTag(R.layout.content_expandable_list_view_child) + "项" + "被长按了"; } else { content = "父类第" + view.getTag(R.layout.content_expandable_list_view) + "项" + "中的" + "子类第" + view.getTag(R.layout.content_expandable_list_view_child) + "项" + "被长按了"; } Toast.makeText(ExpandableListViewActivity.this, content, Toast.LENGTH_SHORT).show(); return true; }});
获得父布局
// 获得父项显示的view @Override public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) { if (view == null) { LayoutInflater inflater = (LayoutInflater) ExpandableListViewActivity .this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.content_expandable_list_view, null); } view.setTag(R.layout.content_expandable_list_view, parentPos); view.setTag(R.layout.content_expandable_list_view_child, -1); TextView text = (TextView) view.findViewById(R.id.parent_title); text.setText(parentList[parentPos]); return view; }
获得子布局
// 获得子项显示的view @Override public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) { if (view == null) { LayoutInflater inflater = (LayoutInflater) ExpandableListViewActivity .this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.content_expandable_list_view_child, null); } view.setTag(R.layout.content_expandable_list_view, parentPos); view.setTag(R.layout.content_expandable_list_view_child, childPos); TextView text = (TextView) view.findViewById(R.id.child_title); text.setText(dataset.get(parentList[parentPos]).get(childPos)); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(ExpandableListViewActivity.this, "点到了内置的textview", Toast.LENGTH_SHORT).show(); } }); return view; }
设置子布局可选中
// 子项是否可选中,如果需要设置子项的点击事件,需要返回true @Override public boolean isChildSelectable(int i, int i1) { return true; }
所有代码
package com.sainthigh.newwebview;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class ExpandableListViewActivity extends AppCompatActivity { public static void startExpandableListViewActivity(Context ctx) { Intent intent = new Intent(ctx, ExpandableListViewActivity.class); ctx.startActivity(intent); } private Map<String, List<String>> dataset = new HashMap<>(); private String[] parentList = new String[]{"first", "second", "third"}; private List<String> childrenList1 = new ArrayList<>(); private List<String> childrenList2 = new ArrayList<>(); private List<String> childrenList3 = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable_list_view); initialData(); ExpandableListView expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview); expandablelistview.setAdapter(new MyExpandableListViewAdapter()); expandablelistview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ExpandableListViewActivity.this, "onItemClickListener--" + position, Toast.LENGTH_SHORT).show(); } }); expandablelistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(ExpandableListViewActivity.this, "onChildClick" + childPosition, Toast.LENGTH_SHORT).show(); return true; } }); expandablelistview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { String content = ""; if ((int) view.getTag(R.layout.content_expandable_list_view) == -1) { content = "父类第" + view.getTag(R.layout.content_expandable_list_view_child) + "项" + "被长按了"; } else { content = "父类第" + view.getTag(R.layout.content_expandable_list_view) + "项" + "中的" + "子类第" + view.getTag(R.layout.content_expandable_list_view_child) + "项" + "被长按了"; } Toast.makeText(ExpandableListViewActivity.this, content, Toast.LENGTH_SHORT).show(); return true; } }); } private void initialData() { childrenList1.add(parentList[0] + "-" + "first"); childrenList1.add(parentList[0] + "-" + "second"); childrenList1.add(parentList[0] + "-" + "third"); childrenList2.add(parentList[1] + "-" + "first"); childrenList2.add(parentList[1] + "-" + "second"); childrenList2.add(parentList[1] + "-" + "third"); childrenList3.add(parentList[2] + "-" + "first"); childrenList3.add(parentList[2] + "-" + "second"); childrenList3.add(parentList[2] + "-" + "third"); dataset.put(parentList[0], childrenList1); dataset.put(parentList[1], childrenList2); dataset.put(parentList[2], childrenList3); } private class MyExpandableListViewAdapter extends BaseExpandableListAdapter { // 获得某个父项的某个子项 @Override public Object getChild(int parentPos, int childPos) { return dataset.get(parentList[parentPos]).get(childPos); } // 获得父项的数量 @Override public int getGroupCount() { return dataset.size(); } // 获得某个父项的子项数目 @Override public int getChildrenCount(int parentPos) { return dataset.get(parentList[parentPos]).size(); } // 获得某个父项 @Override public Object getGroup(int parentPos) { return dataset.get(parentList[parentPos]); } // 获得某个父项的id @Override public long getGroupId(int parentPos) { return parentPos; } // 获得某个父项的某个子项的id @Override public long getChildId(int parentPos, int childPos) { return childPos; } // 按函数的名字来理解应该是是否具有稳定的id,这个方法目前一直都是返回false,没有去改动过 @Override public boolean hasStableIds() { return false; } // 获得父项显示的view @Override public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) { if (view == null) { LayoutInflater inflater = (LayoutInflater) ExpandableListViewActivity .this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.content_expandable_list_view, null); } view.setTag(R.layout.content_expandable_list_view, parentPos); view.setTag(R.layout.content_expandable_list_view_child, -1); TextView text = (TextView) view.findViewById(R.id.parent_title); text.setText(parentList[parentPos]); return view; } // 获得子项显示的view @Override public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) { if (view == null) { LayoutInflater inflater = (LayoutInflater) ExpandableListViewActivity .this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.content_expandable_list_view_child, null); } view.setTag(R.layout.content_expandable_list_view, parentPos); view.setTag(R.layout.content_expandable_list_view_child, childPos); TextView text = (TextView) view.findViewById(R.id.child_title); text.setText(dataset.get(parentList[parentPos]).get(childPos)); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(ExpandableListViewActivity.this, "点到了内置的textview", Toast.LENGTH_SHORT).show(); } }); return view; } // 子项是否可选中,如果需要设置子项的点击事件,需要返回true @Override public boolean isChildSelectable(int i, int i1) { return true; } }}
0 0
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android-ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- android之 ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Mac下对ShadowsocksX进行Hook,实现自动更新账号
- java.lang.NoClassDefFoundError与aspectjrt、aspectjweaver的联系
- C++语言程序设计笔记(2)
- 微信公众号
- CodeForces 604 B.More Cowbell(贪心)
- android-ExpandableListView的使用
- 路径规划(京东2016实习生真题)
- Spring学习总结——Spring实现AOP的多种方式
- faster rcnn训练自己的数据集demo和训练过程error总结
- YARN源代码解读
- PHP 模块生命周期
- session_set_cookie_params()
- 动态规划入门-Triangle
- Android开发之自定义圆形的ImageView的实现