android-ExpandableListView的使用

介绍

含有Expandable功能的ListView

看图

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

获得父布局

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

}

猜你喜欢

转载自blog.csdn.net/AdrianAndroid/article/details/70255433