android 带复选框的 列表,android – 带有selectall复选框的可扩展列表视图:组itemclick和滚动错误...

我有一个ExpandableListView,在组名旁边有一个复选框,在展开时,还有带复选框的子项.

假设我有4个小组,有50个孩子.当一个组被展开时,我点击全选复选框,一切正常,所有复选框都被选中并保持其状态,如果我滚动.

但是如果我滚动直到列表中的最后一个孩子,那么滚动就会变成错误(滚动到顶部,然后触摸屏幕以停止滚动不再起作用),点击组也不再起作用,直到我再次单击selectall复选框.

点击一个子复选框什么都不做,我必须点击selectall复选框才能使一切工作.

我更改了子项的Focusable状态复选框,组复选框并尝试了很多方法,但我找不到解决方法.你知道它来自哪里吗?

public class ExpandableListAdapter extends BaseExpandableListAdapter {

LayoutInflater cinflater;

LayoutInflater ginflater;

ArrayList array;

Context context;

public ExpandableListAdapter(Context context, ArrayList array) {

super();

cinflater = LayoutInflater.from(context);

ginflater = LayoutInflater.from(context);

this.array = array;

this.context = context;

}

public class ChildViewHolder {

CheckBox contactcheckbox;

TextView name;

}

@Override

public int getChildrenCount(int groupPosition) {

// TODO Auto-generated method stub

return array.get(groupPosition).getContacts().size();

}

private OnCheckedChangeListener contactchecklistener = new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {

Contact contact = (Contact) checkboxView.getTag();

contact.setCheck(isChecked);

}

};

@Override

public View getChildView(int groupPosition, int childPosition,

boolean isLastChild, View convertView, ViewGroup parent) {

ChildViewHolder cholder;

if(convertView == null){

cholder = new ChildViewHolder();

convertView = cinflater.inflate(R.layout.childsitems, null);

cholder.name = (TextView) convertView.findViewById(R.id.childnametextview);

cholder.contactcheckbox = (CheckBox) convertView.findViewById(R.id.childcheckbox);

cholder.contactcheckbox.setOnCheckedChangeListener(contactchecklistener);

convertView.setTag(cholder);

}else{

cholder = (ChildViewHolder) convertView.getTag();

}

cholder.contactcheckbox.setTag(array.get(groupPosition).getContacts().get(childPosition));

cholder.contactcheckbox.setChecked(array.get(groupPosition).getContacts().get(childPosition).isCheck());

cholder.name.setText(array.get(groupPosition).getContacts().get(childPosition).getName());

return convertView;

}

public class GroupViewHolder {

TextView groupname;

CheckBox groupcheck;

}

@Override

public int getGroupCount() {

// TODO Auto-generated method stub

return array.size();

}

private OnCheckedChangeListener groupchecklistener = new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

int index = (Integer) buttonView.getTag();

for(int i = 0;i

array.get(index).getContacts().get(i).setCheck(isChecked);

}

array.get(index).setCheck(isChecked);

notifyDataSetChanged();

}

};

@Override

public View getGroupView(int groupPosition, boolean isExpanded,

View convertView, ViewGroup parent) {

GroupViewHolder gholder;

if(convertView==null){

gholder = new GroupViewHolder();

convertView = ginflater.inflate(R.layout.groupsitems,null);

gholder.groupname = (TextView)convertView.findViewById(R.id.groupnametextview);

gholder.groupcheckbox = (CheckBox)convertView.findViewById(R.id.groupcheckbox);

gholder.groupcheckbox.setOnCheckedChangeListener(groupchecklistener);

convertView.setTag(gholder);

}else{

gholder = (GroupViewHolder)convertView.getTag();

}

gholder.groupname.setText(array.get(groupPosition).getGroupname());

gholder.groupname.setTextSize(24);

gholder.groupname.setTextColor(Color.parseColor("#FF858585"));

gholder.groupname.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

int paddingleft = 48; // 38 dps

final float scale = getResources().getDisplayMetrics().density;

int dpvalue = (int) (paddingleft * scale + 0.5f);

gholder.groupname.setPadding(dpvalue, 16, 0, 16);

gholder.groupcheckbox.setTag(groupPosition);

gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());

if(isExpanded == true){

convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.expandedbackground));

}else{

convertView.setBackgroundColor(Color.TRANSPARENT);

}

return convertView;

}

@Override

public Object getChild(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getChildId(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return 0;

}

@Override

public Object getGroup(int groupPosition) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getGroupId(int groupPosition) {

// TODO Auto-generated method stub

return 0;

}

@Override

public boolean hasStableIds() {

// TODO Auto-generated method stub

return false;

}

@Override

public boolean isChildSelectable(int groupPosition, int childPosition) {

// TODO Auto-generated method stub

return false;

}

}

groupsitems.xml:

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/groupcheckbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_marginTop="12dp"

android:focusable="false"

android:gravity="right|center"

android:paddingRight="10dp" />

android:id="@+id/groupnametextview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_toLeftOf="@+id/groupcheckbox"

android:focusableInTouchMode="false"

android:text="Large Text"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="#FF858585"

android:textStyle="bold" />

childitems.xml:

android:id="@+id/relativeLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:layout_marginTop="8dp"

android:paddingLeft="8dp" >

android:id="@+id/childnametextview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/childcheckbox"

android:gravity="right|center"

android:paddingLeft="16dp"

android:textColor="#FFAFAFAF"

android:textSize="16sp"

android:textStyle="bold" />

android:id="@+id/childcheckbox"

android:layout_width="48dp"

android:layout_height="48dp"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true" />

解决方法:

我找到了解决方案.我没有在groupcheckbox上使用OnCheckedChangeListener,而是使用了OnClickListener,它解决了这样的问题:

gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());

gholder.groupcheckbox.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

for(int i = 0;i

array.get(groupPosition).getContacts().get(i).setCheck(gholder.groupcheckbox.isChecked());

}

array.get(groupPosition).setCheck(gholder.groupcheck.isChecked());

notifyDataSetChanged();

}

});

标签:android,android-layout

来源: https://codeday.me/bug/20190625/1287388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值