我的要求是在ExpandbleListView
视图中添加CheckBox
到父项,而父项CheckBox
控制子项复选框如何获取此项?如何添加复选框任何一个给出解决方案?
ExpandableList.java - 适配器类
public class ExpandableList extends BaseExpandableListAdapter {
Context Context;
private HashMap<String, List<String>> ChildItems;
private List<String> GroupItems;
HashMap<Integer,boolean[]> checkboxData;
CheckBox checkBox'
public ExpandableList(Context mContext, List<String> mGroupItems, HashMap<String, List<String>> mChildItems
) {
Context = mContext;
ChildItems = mChildItems;
GroupItems = mGroupItems;
}
@Override
public int getGroupCount() {
return GroupItems.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return ChildItems.get(GroupItems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return GroupItems.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return ChildItems.get(GroupItems.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String)getGroup(groupPosition);
if (convertView==null)
{
LayoutInflater li = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.group_list,null);
}
TextView tv = (TextView)convertView.findViewById(R.id.groupTitle);
tv.setTypeface(null, Typeface.BOLD);
tv.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String listTitle = (String) getChild(groupPosition,childPosition);
if (convertView==null)
{
LayoutInflater layoutInflater = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.items_list,null);
}
TextView tvv = (TextView)convertView.findViewById(R.id.items_list);
tvv.setText(listTitle);
tvv.setTypeface(null,Typeface.BOLD);
CheckBox cb = (CheckBox)convertView.findViewById(R.id.listCheckbox);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
private HashMap<String, List<String>> ChildItemss;
private List<String> GroupItemss;
ExpandableList expandableList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listData();
expandableListView = (ExpandableListView)findViewById(R.id.expandblelist);
expandableList = new ExpandableList(this,GroupItemss,ChildItemss);
expandableListView.setAdapter(expandableList);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this, GroupItemss.get(groupPosition)+"Expanded", Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, final int groupPosition, final int childPosition, long id) {
ChildViewHolder ch = new ChildViewHolder();
ch.childCheckbox = (CheckBox)findViewById(R.id.listCheckbox);
ch.childCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(MainActivity.this, ChildItemss.get(childPosition)+"Expanded", Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
}
public void listData()
{
GroupItemss = new ArrayList<String>();
ChildItemss = new HashMap<String, List<String>> ();
GroupItemss.add("Cricket");
GroupItemss.add("India");
List<String> cricket = new ArrayList<>();
cricket.add("sachin");
cricket.add("dhoni");
cricket.add("yuvi");
List<String> india = new ArrayList<String>();
india.add("modi");
india.add("pawankalyan");
ChildItemss.put(GroupItemss.get(0),cricket);
ChildItemss.put(GroupItemss.get(1),india);
}
items_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/items_listlayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/colorPrimary"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:id="@+id/items_list"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listCheckbox"/>
</LinearLayout>
group_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/group_layout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="@color/colorAccent"
android:id="@+id/groupTitle"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/groupCheckbox"/>
</LinearLayout>
答案 0 :(得分:0)
试试这个我试过你。根据您的要求进行修改。
package com.deeb.adapter;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.deeb.R;
import java.util.HashMap;
import java.util.List;
/**
* Created by terril1 on 25/07/16.
*/
public class ExpandableList extends BaseExpandableListAdapter implements GroupItemClicked {
Context mContext;
private HashMap<String, List<String>> ChildItems;
private List<String> GroupItems;
HashMap<Integer, boolean[]> checkboxData;
CheckBox checkBox;
GroupItemClicked itemClicked;
boolean itemcheckedTrue;
int pos;
public ExpandableList(Context mContext, List<String> mGroupItems, HashMap<String, List<String>> mChildItems
) {
this.mContext = mContext;
ChildItems = mChildItems;
GroupItems = mGroupItems;
itemClicked = this;
}
@Override
public int getGroupCount() {
return GroupItems.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return ChildItems.get(GroupItems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return GroupItems.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return ChildItems.get(GroupItems.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.group_list, null);
holder.lblTitle = (TextView) convertView.findViewById(R.id.groupTitle);
holder.checkedItem = (CheckBox) convertView.findViewById(R.id.groupCheckbox);
holder.lblTitle.setTypeface(null, Typeface.BOLD);
holder.checkedItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
Log.e("TAG", "clicked");
itemClicked.itemclicked(groupPosition, true);
//Case 1
} else {
}
//case 2
}
});
convertView.setTag(holder);
holder.checkedItem.setTag(groupPosition);
} else {
holder = (ViewHolder) convertView.getTag();
(holder).checkedItem.getTag();
}
holder.lblTitle.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String listTitle = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_list, null);
}
TextView tvv = (TextView) convertView.findViewById(R.id.items_list);
tvv.setText(listTitle);
tvv.setTypeface(null, Typeface.BOLD);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.listCheckbox);
if (itemcheckedTrue) {
cb.setEnabled(true);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void itemclicked(int position, boolean status) {
itemcheckedTrue = status;
pos = position;
}
class ViewHolder {
TextView lblTitle;
CheckBox checkedItem;
}
}
interface GroupItemClicked {
void itemclicked(int position, boolean status);
}