Android ExpandableListView使用

  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;
    }
  }

效果:

3110861-c5f0021e2865c95a.png
效果.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值