선택 가능 한 ExpandableListView

13655 단어 androidcheckbox
게다가 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 로 돌아 갑 니 다.
편집http://www.allenj.net/?p=3644. 건어물 제시:
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<Group> groups;
 
    public EListAdapter(Context context, ArrayList<Group> 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<Child> children;
    private boolean isChecked;
 
    public Group(String id, String title) {
        this.title = title;
        children = new ArrayList<Child>();
        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<Group> groups;
    ExpandableListView listView;
    EListAdapter adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        groups = new ArrayList<Group>();
        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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</RelativeLayout>

group_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"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvGroup"
        android:layout_width="280dp"
        android:layout_height="45dip"
        android:gravity="center_vertical|left"
        android:paddingLeft="45dp"
        android:textSize="17dip"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@+id/chbGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

</LinearLayout>

child_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"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvChild"
        android:layout_width="280dp"
        android:layout_height="45dip"
        android:gravity="center_vertical"
        android:paddingLeft="45dp"
        android:textSize="17dip"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@+id/chbChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false" />

</LinearLayout>

좋은 웹페이지 즐겨찾기