ExpandableListView简单用法

2017-01-24  本文已影响0人  酥脆海苔饼干

ExpandableListView与ListView不同的是具有可扩展性,即具有下拉功能。但究其使用原理及思路,参考ListView的使用还是事半功倍的。其具有一些属性:

1.自身带有下拉箭头,可通过GroupIndicator属性值对其设置是否可视。当为null时,即取消自带箭头。

2.ExpandableListView本身就是一个group下包含有多个item。因此在设置Group数据时,仅仅设置单层的List<Object>集合即可;而下属item要设置成为双层的List<List<Object>>集合;对于item集合,内层为item的数据,外层为相应添加的目标group。

3.为item设置不同点击的监听。首先在自定义Adapter类中设置重写的isChildSelectable返回值为true;其次在ExpandableListView所处的类中设置onChildClickListener方法,重写该方法的onChildClick事件。

以下是比较简单的ExpandableListView例子:
MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public ExpandableListView expandableListView;
    public List<String> groupListData;
    public List<List<String>> itemListData=new ArrayList<List<String>>();
    public ExpandAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expand_list);
        initDate();
        adapter = new ExpandAdapter(this, groupListData, itemListData);
        expandableListView.setAdapter(adapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,"click item="+adapter.getChild(groupPosition,childPosition),Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }
    public void initDate(){
        groupListData=new ArrayList<String>();
        groupListData.add("test group 1");
        groupListData.add("test group 2");
        groupListData.add("test group 3");
        List<String> item=new ArrayList<String>();
        item.add("test item (1)");
        item.add("test item (1.1)");
        item.add("test item (1.1.1)");
        List<String> item2=new ArrayList<String>();
        item2.add("test item (2)");
        item2.add("test item (2.2)");
        item2.add("test item (2.2.2)");
        List<String> item3=new ArrayList<String>();
        item3.add("test item (3)");
        item3.add("test item (3.3)");
        item3.add("test item (3.3.3)");
//       for(int i=0;i<groupListData.size();i++){
//           itemListData.add(item);
//       }
        itemListData.add(item);
        itemListData.add(item2);
        itemListData.add(item3);
    }

ExpandAdapter.java类:

public class ExpandAdapter extends BaseExpandableListAdapter{
    public Context context;
    public GroupViewHolder gvh;
    public ItemViewHolder ivh;
    public List<String> groupListData;
    public List<List<String>> itemListData;
    public ExpandAdapter(Context context, List<String> groupListData,List<List<String>> itemListData){
        this.context=context;
        this.groupListData=groupListData;
        this.itemListData=itemListData;//item一定为两层list,外层存放groupId,内层存放itemId
    }
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.group_layout, null);
            gvh=new GroupViewHolder();
            gvh.groupText=(TextView) convertView.findViewById(R.id.group_text);
            convertView.setTag(gvh);
        } else{
            gvh=(GroupViewHolder)convertView.getTag();
        }
        gvh.groupText.setText(groupListData.get(groupPosition));
        return convertView;
    }
    public class GroupViewHolder{
        TextView groupText;
    }
    @Override
    public int getGroupCount() {
        return groupListData.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupListData.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, null);
            ivh=new ItemViewHolder();
            ivh.itemText=(TextView) convertView.findViewById(R.id.item_text);
            convertView.setTag(ivh);
        } else {
            ivh=(ItemViewHolder) convertView.getTag();
        }
        ivh.itemText.setText(itemListData.get(groupPosition).get(childPosition));
        return convertView;
    }
    public class ItemViewHolder{
        TextView itemText;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return itemListData.get(groupPosition).size();//先根据groupPosition获取某个group项,再获取该group的size
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {//获取某个group项的item
        return itemListData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
}

group_layout.xml与item_layout.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/group_text"/>
</LinearLayout>

expandableListView.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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.expandable.example.sm.expandablelistview.MainActivity">
    <ExpandableListView
        android:id="@+id/expand_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
上一篇 下一篇

猜你喜欢

热点阅读