Android ExpandableListView使用

简介: activity_main.xml layout_parent.xml layout_child.xml MainActivity.
  1. activity_main.xml
<?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"
    tools:context="com.mazaiting.expandablelistview.MainActivity"
    >

  <ExpandableListView
      android:id="@+id/main_expandable_list_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
</RelativeLayout>
  1. layout_parent.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"
    >

  <TextView
      android:id="@+id/tv_show_parent"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_gravity="center"
      android:gravity="center" />

</LinearLayout>
  1. layout_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"
    android:orientation="vertical"
    >

  <TextView
      android:id="@+id/tv_show_child"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:layout_gravity="center"
      android:gravity="center" />

</LinearLayout>
  1. MainActivity.java

public class MainActivity extends AppCompatActivity {
  private ExpandableListView mExpandableListView = null;
  private List<String> mList = null;
  private Map<String, List<String>> mMap = null;
  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mExpandableListView = (ExpandableListView) this.findViewById(R.id.main_expandable_list_view);

    initData();

    mExpandableListView.setAdapter(new MyAdapter());

    mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
      @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
          int childPosition, long id) {
        mExpandableListView.collapseGroup(groupPosition);
        return true;
      }
    });
  }

  /**
   * 初始化数据
   */
  private void initData() {
    mList = new ArrayList<>();
    mList.add("parent1");
    mList.add("parent2");
    mList.add("parent3");

    mMap = new HashMap<>();
    List<String> list1 = new ArrayList<>();
    list1.add("child1-1");
    list1.add("child1-2");
    list1.add("child1-3");
    mMap.put(mList.get(0), list1);

    List<String> list2 = new ArrayList<>();
    list2.add("child2-1");
    list2.add("child2-2");
    list2.add("child2-3");
    mMap.put(mList.get(1), list2);

    List<String> list3 = new ArrayList<>();
    list3.add("child3-1");
    list3.add("child3-2");
    list3.add("child3-3");
    mMap.put(mList.get(2), list3);
  }


  class MyAdapter extends BaseExpandableListAdapter{
    /**获取父条目个数*/
    @Override public int getGroupCount() {
      return mList.size();
    }

    /**获取当前条目下的子条目数据*/
    @Override public int getChildrenCount(int groupPosition) {
      String string = mList.get(groupPosition);
      int size = mMap.get(string).size();
      return size;
    }

    /**获取父条目对象*/
    @Override public Object getGroup(int groupPosition) {
      return mList.get(groupPosition);
    }

    /**获取指定位置的子条目*/
    @Override public Object getChild(int groupPosition, int childPosition) {
      String string = mList.get(groupPosition);
      String child = mMap.get(string).get(childPosition);
      return child;
    }

    /**获取父条目id*/
    @Override public long getGroupId(int groupPosition) {
      return groupPosition;
    }

    /**获取子条目id*/
    @Override public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }

    /**是否稳定*/
    @Override public boolean hasStableIds() {
      return true;
    }

    /**绑定父条目布局*/
    @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {
      if (null == convertView){
        convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_parent, null);
      }
      TextView textView = (TextView) convertView.findViewById(R.id.tv_show_parent);
      textView.setText(mList.get(groupPosition));
      return convertView;
    }

    /**绑定子条目布局*/
    @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {
      if (null == convertView){
        convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_child, null);
      }
      TextView textView = (TextView) convertView.findViewById(R.id.tv_show_child);
      String string = mList.get(groupPosition);
      String result = mMap.get(string).get(childPosition);
      textView.setText(result);
      return convertView;
    }

    /**是否有子条目被选择*/
    @Override public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }

效果:

img_c8b841b195a9b242ab42f7ccab3459d5.png
效果.png
目录
相关文章
|
10月前
|
XML Java Android开发
Android ExpandableListView 使用中遇到的问题集锦
Android ExpandableListView 使用中遇到的问题集锦
|
Android开发
Android 取消 ExpandableListView 的分割线,解决ScrollView 嵌套 ExpandableListView的问题
Android 取消 ExpandableListView 的分割线,解决ScrollView 嵌套 ExpandableListView的问题
|
Android开发
Android中ExpandableListView中嵌套ListView
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/51136868 最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结。
1624 0
|
Android开发 UED
Android ExpandableListView开发简介
 Android ExpandableListView开发简介 我之前写了一些文章是关于实现带有分组、标签的“ListView”: (文章1)《类似通讯录分组的Android PinnedSectionListView,且分组标签悬停滑入滑出》文章链接:http://blog.
1079 0