ExpandableListview控件使用---可展开的列表组件

        ExpandableListView可以展开的列表组件,以前没有接触过这个,但在最近的项目中涉及到,所以记录下来,以后也一定要养成记录的好习惯。

ExpandableListView介绍

        是listview的子类,在普通的listview的基础上进行拓展,把应用的列表项分为几组,每组又包含多个列表项。该控件所显示的列表项由ExpandableListviewAdapter来提供。

ExpandableListview的使用

 1.在布局文件中布局:

<ExpandableListView
    android:id="@+id/elv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:scrollbars="none"/>

2.在Activity中进行使用

elv=(ExpandableListView) findViewById(R.id.elv);
elv.setAdapter(adapter2);//设置适配器
elv.setGroupIndicator(null);//取消下拉的箭头
elv.setDivider(null);   //取消分割线
//将所有项设置成默认展开

int groupCount = list.size();

for (int i=0; i<groupCount; i++) {

    elv.expandGroup(i);//关键是该方法

}

3.设置ExpandableListView的适配器

package com.gth.findlove.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gth.findlove.R;
import com.gth.findlove.bean.group.PengyouquanBean;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * Created by Administrator on 2017/5/9.
 */

public class DongtaiAdapter2 extends BaseExpandableListAdapter {
    private List<PengyouquanBean.SReturnDataBean> groupList;//组的数量
    private List<PengyouquanBean.SReturnDataBean.SStateModelsBean> childlist;//孩子的数量
    private LayoutInflater inflater;
    private Context context;

    public DongtaiAdapter2(List<PengyouquanBean.SReturnDataBean> groupList,Context context) {
        this.context=context;
        this.groupList=groupList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();//获取组的数量
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList.get(groupPosition).sStateModels.size();//获取每个组下孩子的数量
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);//获取组对应的数据对象
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).sStateModels.get(childPosition);//获取孩子的数据对象
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;//获取组id
    }

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

    @Override
    public boolean hasStableIds() {
        return true;//表示孩子是否和组ID是跨基础数据的更改稳定
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        convertView = inflater.inflate(R.layout.item_fu, null);


        TextView tv_date = (TextView) convertView.findViewById(R.id.tv_date);

        Long time=  groupList.get(groupPosition).sStateDate;
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
        String d = format.format(time);
        String date=d.substring(5);//截取时间字段
        tv_date.setText(date);
        return convertView;//自定义组UI

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


        convertView = inflater.inflate(R.layout.item_friendquan1, null);

        //找控件
        TextView  tv_content = (TextView) convertView.findViewById(R.id.tv_content);
        TextView tv_num = (TextView) convertView.findViewById(R.id.tv_num);
        ImageView  iv_photo = (ImageView) convertView.findViewById(R.id.iv_photo);




        /*赋值操作*/

        /*发布的内容*/
        tv_content.setText(groupList.get(groupPosition).sStateModels.get(childPosition).sStateContent+"");

        /*图片*/
        String picPath= groupList.get(groupPosition).sStateModels.get(childPosition).sStateImgUrls.get(0);
        Log.i("###","==="+picPath);

        if(!"".equals(picPath)&&picPath!=null){
            Picasso.with(context).load(picPath).placeholder(R.drawable.img_load).config(Bitmap.Config.RGB_565).error(R.drawable.img_erro).fit().centerCrop().into(iv_photo);
        }

        /*一共几张图片*/

        tv_num.setText(groupList.get(groupPosition).sStateModels.get(childPosition).sStateImgUrls.size()+"");

        return convertView;


    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;//孩子在指定的位置是否可选的
    }
}

4 ExpandableListview的点击事件

在activity中:
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        String stateID=  list.get(groupPosition).sStateModels.get(childPosition).sStateID;
        Intent intent=new Intent(DongtaiActivity.this,DongtaiDetailActivity.class);
        intent.putExtra("sStateID",stateID);
        intent.putExtra("DongtaiFlag",getIntent().getStringExtra("DongtaiFlag"));//从个人中心进来还是》》
         intent.putExtra("friendID", getIntent().getStringExtra("FriendID"));
        startActivity(intent);



        return false;
    }
});



5 结果预览

默认都是展开的情况:
ExpandableListview控件使用---可展开的列表组件
将子内容合上:

ExpandableListview控件使用---可展开的列表组件

一个展开 一个合上:

ExpandableListview控件使用---可展开的列表组件

6 注意事项:

如果ExpandableListview嵌套listview的时候,在适配器Adapter中的getChildCount()方法中只能return 1.