listview的使用-ExpandableListView

本文详细介绍Android中ExpandableListView的使用方法,包括布局配置、适配器编写、点击事件监听及只展开一个组的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码:

http://download.youkuaiyun.com/detail/sinat_31057219/9735884

参考:Android中ExpandableListView的使用(一)

ExpandableListView的使用demo : Ablexq/MyHelpCenter

知识点:

ExpandableListView的使用

public class ExpandableListView extends ListView {...}

效果图:

Markdown

布局:

  <ExpandableListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="10dp"
            android:childDivider="#c8c7cc"
            android:divider="@null"
            android:dividerHeight="1px"
            android:gravity="center"
            android:groupIndicator="@null"
            android:listSelector="#00000000"
            android:scrollbars="none" />

代码

设置适配器:一次传递主目录和子目录的数据源。

adapter = new ExpandableListAdapter(this, list, child_text_array);

expandableListView.setAdapter(adapter);

点击事件:

expandableListView.setOnGroupClickListener(new OnGroupClickListener() {...}

expandableListView.setOnChildClickListener(new OnChildClickListener() {...}

详细如下:

public class ExpandableListViewActivity extends Activity {

    private ExpandableListView expandableListView;

    private ExpandableListAdapter adapter;

    private List<Map<String, Object>> list;
    private String[][] child_text_array;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_listview);

        init();
        initModle();
        setListener();
    }

    private void init() {
        expandableListView = (ExpandableListView) findViewById(R.id.list);

        child_text_array = Model.EXPANDABLE_MORELIST_TXT;
    }

    private void setListener() {
        expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                return false;
            }
        });

        expandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(),
                        child_text_array[groupPosition][childPosition],
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void initModle() {
        list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < Model.EXPANDABLE_LISTVIEW_TXT.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", Model.EXPANDABLE_LISTVIEW_IMG[i]);
            map.put("txt", Model.EXPANDABLE_LISTVIEW_TXT[i]);
            list.add(map);
        }
        adapter = new ExpandableListAdapter(this, list, child_text_array);
        expandableListView.setAdapter(adapter);
    }

}

adapter:

继承自BaseExpandableListAdapter
实现
getGroupCount、getChildrenCount、
getGroup、getChild、
getGroupId、getChildId、
getGroupView、getChildView
等方法。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private String[][] child_text_array;
    private Context context;

    private List<Map<String, Object>> list;

    public ExpandableListAdapter(Context context,
            List<Map<String, Object>> list, String[][] child_text_array) {
        this.context = context;
        this.list = list;
        this.child_text_array = child_text_array;
    }

    /**
     * 获取一级标签总数
     */
    @Override
    public int getGroupCount() {
        return list.size();
    }

    /**
     * 获取一级标签下二级标签的总数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return child_text_array[groupPosition].length;
    }

    /**
     * 获取一级标签内容
     */
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition).get("txt");
    }

    /**
     * 获取一级标签下二级标签的内容
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child_text_array[groupPosition][childPosition];
    }

    /**
     * 获取一级标签的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) {
        convertView = (LinearLayout) LinearLayout.inflate(context,
                R.layout.item_group_layout, null);

        ImageView group_icon = (ImageView) convertView
                .findViewById(R.id.img_icon);
        TextView group_title = (TextView) convertView
                .findViewById(R.id.group_title);
        if (isExpanded) {
            group_title.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.group_down, 0);//设置是否打开的标志
        } else {
            group_title.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.group_up, 0);//textView的drawableRight
        }
        group_icon.setImageResource(Integer.parseInt(list.get(groupPosition)
                .get("img").toString()));
        group_title.setText(list.get(groupPosition).get("txt").toString());

        return convertView;
    }

    /**
     * 对一级标签下的二级标签进行设置
     */
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        convertView = (RelativeLayout) RelativeLayout.inflate(context,
                R.layout.item_child_layout, null);
        TextView child_text = (TextView) convertView
                .findViewById(R.id.child_text);

        child_text.setText(child_text_array[groupPosition][childPosition]);

        return convertView;
    }

    /**
     * 当选择子节点的时候,调用该方法
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

ExpandableListView只打开一个组,关闭其他组:

当我们在使用ExpandableListView的时候,打开一个组的时候,要关闭其他组;需要用到setOnGroupExpandListener来进行设置:

mExpandListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {  
            @Override  
            public void onGroupExpand(int groupPosition) {  
                int count = mExpandListView.getExpandableListAdapter().getGroupCount();  
                for(int j = 0; j < count; j++){  
                    if(j != groupPosition){  
                        mExpandListView.collapseGroup(j);  
                    }  
                }  
            }  
        });  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值