当前位置: 首页 > 图灵资讯 > 技术篇> android-ExpandableListView的使用

android-ExpandableListView的使用

来源:图灵教育
时间:2023-05-30 09:31:12

介绍

Listviewew,含有Expandable功能

看图

android-ExpandableListView的使用_expandablelistview

主界面

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

获得父布局

//  获得父项显示的vieww        @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;        }

获得子布局

//  获得子项显示的vieww        @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> childrenlistt1 = 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() {        childrenlistt1.add(parentList[0] + "-" + "first");        childrenlistt1.add(parentList[0] + "-" + "second");        childrenlistt1.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;        }        //  获得父项显示的vieww        @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;        }        //  获得子项显示的vieww        @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;        }    }}