博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
可勾选的ExpandableListView
阅读量:6801 次
发布时间:2019-06-26

本文共 10392 字,大约阅读时间需要 34 分钟。

hot3.png

加上checkbox,需求大抵都是:check group, check all his children; if one' group's children all checked, group become checked. ExpandableListView的选中状态没有类似SparseBooleanArray的东东,需要自己维护一个数据结构. 开发上要求点group list item的大多数区域是expand/collapse,仅点checkbox区域是勾选,而点child list item的任意区域都是勾选效果, 可以让列表setOnChildClickListener()。注意ExpandableListAdapter的isChildSelectable()方法一定返回true。

编辑自. 给出干货:

EListAdapter.java

package com.example.checkableexpandablelistview;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.TextView;import com.example.aexpandablelist.R;public class EListAdapter extends BaseExpandableListAdapter implements ExpandableListView.OnChildClickListener {        private Context context;    private ArrayList
groups;     public EListAdapter(Context context, ArrayList
groups) {        this.context = context;        this.groups = groups;    }     public Object getChild(int groupPosition, int childPosition) {        return groups.get(groupPosition).getChildItem(childPosition);    }     public long getChildId(int groupPosition, int childPosition) {        return childPosition;    }     public int getChildrenCount(int groupPosition) {        return groups.get(groupPosition).getChildrenCount();    }     public Object getGroup(int groupPosition) {        return groups.get(groupPosition);    }     public int getGroupCount() {        return groups.size();    }     public long getGroupId(int groupPosition) {        return groupPosition;    }     public boolean hasStableIds() {        return true;    }     public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }     /** 設定 Group 資料 */    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        Group group = (Group) getGroup(groupPosition);         if (convertView == null) {            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertView = infalInflater.inflate(R.layout.group_layout, null);        }         TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);        tv.setText(group.getTitle());         // 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbGroup);        checkBox.setChecked(group.getChecked());         // 點擊 CheckBox 時,將狀態存起來        checkBox.setOnClickListener(new Group_CheckBox_Click(groupPosition));         return convertView;    }     /** 勾選 Group CheckBox 時,存 Group CheckBox 的狀態,以及改變 Child CheckBox 的狀態 */    class Group_CheckBox_Click implements OnClickListener {        private int groupPosition;         Group_CheckBox_Click(int groupPosition) {            this.groupPosition = groupPosition;        }         public void onClick(View v) {            groups.get(groupPosition).toggle();             // 將 Children 的 isChecked 全面設成跟 Group 一樣            int childrenCount = groups.get(groupPosition).getChildrenCount();            boolean groupIsChecked = groups.get(groupPosition).getChecked();            for (int i = 0; i < childrenCount; i++)                groups.get(groupPosition).getChildItem(i).setChecked(groupIsChecked);             // 注意,一定要通知 ExpandableListView 資料已經改變,ExpandableListView 會重新產生畫面            notifyDataSetChanged();        }    }     /** 設定 Children 資料 */    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        Child child = groups.get(groupPosition).getChildItem(childPosition);         if (convertView == null) {            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertView = inflater.inflate(R.layout.child_layout, null);        }         TextView tv = (TextView) convertView.findViewById(R.id.tvChild);        tv.setText(child.getFullname());         // 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbChild);        checkBox.setChecked(child.getChecked());         // 點擊 CheckBox 時,將狀態存起來        checkBox.setOnClickListener(new Child_CheckBox_Click(groupPosition, childPosition));         return convertView;    }     /** 勾選 Child CheckBox 時,存 Child CheckBox 的狀態 */    class Child_CheckBox_Click implements OnClickListener {        private int groupPosition;        private int childPosition;         Child_CheckBox_Click(int groupPosition, int childPosition) {            this.groupPosition = groupPosition;            this.childPosition = childPosition;        }         public void onClick(View v) {            handleClick(childPosition, groupPosition);        }    }        public void handleClick(int childPosition, int groupPosition) {        groups.get(groupPosition).getChildItem(childPosition).toggle();                // 檢查 Child CheckBox 是否有全部勾選,以控制 Group CheckBox        int childrenCount = groups.get(groupPosition).getChildrenCount();        boolean childrenAllIsChecked = true;        for (int i = 0; i < childrenCount; i++) {            if (!groups.get(groupPosition).getChildItem(i).getChecked())                childrenAllIsChecked = false;        }        groups.get(groupPosition).setChecked(childrenAllIsChecked);        // 注意,一定要通知 ExpandableListView 資料已經改變,ExpandableListView 會重新產生畫面        notifyDataSetChanged();    }    @Override    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {        handleClick(childPosition, groupPosition);        return true;    }    }

Group.java

package com.example.checkableexpandablelistview;import java.util.ArrayList;public class Group {    private String id;    private String title;    private ArrayList
children; private boolean isChecked; public Group(String id, String title) { this.title = title; children = new ArrayList
(); this.isChecked = false; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } public void toggle() { this.isChecked = !this.isChecked; } public boolean getChecked() { return this.isChecked; } public String getId() { return id; } public String getTitle() { return title; } public void addChildrenItem(Child child) { children.add(child); } public int getChildrenCount() { return children.size(); } public Child getChildItem(int index) { return children.get(index); }}

Child.java

package com.example.checkableexpandablelistview;public class Child {    private String userid;    private String fullname;    private String username;    private boolean isChecked;     public Child(String userid, String fullname, String username) {        this.userid = userid;        this.fullname = fullname;        this.username = username;        this.isChecked = false;    }     public void setChecked(boolean isChecked) {        this.isChecked = isChecked;    }     public void toggle() {        this.isChecked = !this.isChecked;    }     public boolean getChecked() {        return this.isChecked;    }     public String getUserid() {        return userid;    }     public String getFullname() {        return fullname;    }     public String getUsername() {        return username;    }}

MainActivity.java

package com.example.checkableexpandablelistview;import java.util.ArrayList;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.widget.ExpandableListView;import com.example.aexpandablelist.R;public class MainActivity extends Activity {    ArrayList
groups;    ExpandableListView listView;    EListAdapter adapter;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        groups = new ArrayList
();        getJSONObject();        listView = (ExpandableListView) findViewById(R.id.listView);        adapter = new EListAdapter(this, groups);        listView.setAdapter(adapter);        listView.setOnChildClickListener(adapter);    }    /** 解悉 JSON 字串 */    private void getJSONObject() {        String jsonStr = "{'CommunityUsersResult':[{'CommunityUsersList':[{'fullname':'a111','userid':11,'username':'a1'}"                + ",{'fullname':'b222','userid':12,'username':'b2'}],'id':1,'title':'人事部'},{'CommunityUsersList':[{'fullname':"                + "'c333','userid':13,'username':'c3'},{'fullname':'d444','userid':14,'username':'d4'},{'fullname':'e555','userid':"                + "15,'username':'e5'}],'id':2,'title':'開發部'}]}";        try {            JSONObject CommunityUsersResultObj = new JSONObject(jsonStr);            JSONArray groupList = CommunityUsersResultObj.getJSONArray("CommunityUsersResult");            for (int i = 0; i < groupList.length(); i++) {                JSONObject groupObj = (JSONObject) groupList.get(i);                Group group = new Group(groupObj.getString("id"), groupObj.getString("title"));                JSONArray childrenList = groupObj.getJSONArray("CommunityUsersList");                for (int j = 0; j < childrenList.length(); j++) {                    JSONObject childObj = (JSONObject) childrenList.get(j);                    Child child = new Child(childObj.getString("userid"), childObj.getString("fullname"),                            childObj.getString("username"));                    group.addChildrenItem(child);                }                groups.add(group);            }        } catch (JSONException e) {            Log.d("allenj", e.toString());        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

activity_main.xml

group_layout.xml

child_layout.xml

   
       

转载于:https://my.oschina.net/TedzycRs/blog/361535

你可能感兴趣的文章
IBM向认知转型 选择混合云路径
查看>>
智能安防市场的痛点到底在哪里?2016欧美消费者调查问卷解读!
查看>>
对号入座,企业物联网化的正确路径是什么?
查看>>
微软宣布免费开源的NuGet包管理器下载量已破10亿
查看>>
微软Win10免费升级通知开启“自毁”模式
查看>>
英国《数字经济法案》
查看>>
必须了解的五个光伏发电财务和税收政策
查看>>
思默特获评“用户满意服务奖”荣誉
查看>>
CYQ.DBImport 数据库反向工程及批量导数据库工具 V1.0 发布
查看>>
AT&T开发出400 GbE试验的SDN控制器
查看>>
DBA生存警示:主备环境误操作案例及防范建议
查看>>
聊天机器人并不适合每一项业务和每个人
查看>>
拼写错误影响黑客盗窃数亿美元
查看>>
真正的持续集成:分布式代码仓库和依赖
查看>>
KDD论文解读 | 想要双11抢单快?靠这个技术提速9MS
查看>>
Asp.net与Flex交互测试记录
查看>>
两招抵御APT攻击
查看>>
教师节有“假期” 网络电话传递温情祝福
查看>>
人们应将公共云与私有云的辩论抛之脑后
查看>>
RFID技术或将代替条形码和二维码
查看>>