gpt4 book ai didi

android - 单选按钮在分段 ListView 滚动时更改状态

转载 作者:行者123 更新时间:2023-11-29 18:02:23 25 4
gpt4 key购买 nike

我在我的应用程序中使用 Sectioned ListViewListView 行中,我有 TextViewRadioGroup.因此,当我正常单击单选按钮时,它会被单击并且工作正常。 但是当我单击并 ScrollView 时,所选 View 也会得到消失并变得不受控制。

这是我正在使用的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_bg" >

<TextView
android:id="@+id/tvname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLines="1"
android:padding="10dp"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/check_present"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:onClick="onPresentClick" />

<RadioButton
android:id="@+id/check_absent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/check_present"
android:onClick="onPresentClick" />
</RadioGroup>

</RelativeLayout>

这是我为 Sectioned ListView 使用的适配器....

这是我更新的代码....

public class NamesAdapter extends ArrayAdapter<Item> {

private ArrayList<Item> items;
private LayoutInflater vi;
private Item objItem;
private ArrayList<Items> presentSelected = new ArrayList<Items>();;
private ArrayList<Items> absentSelected = new ArrayList<Items>();;
private Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
ViewHolderSectionName holderSection;
ViewHolderName holderName;
Items item;

public NamesAdapter(Context context, ArrayList<Item> items) {
super(context, 0, items);

this.items = items;

vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

objItem = items.get(position);

if (objItem.isSectionItem()) {
ItemsSections si = (ItemsSections) objItem;

if (convertView == null
|| !convertView.getTag().equals(holderSection)) {
convertView = vi.inflate(R.layout.alphabet_separator, null);

holderSection = new ViewHolderSectionName();
convertView.setTag(holderSection);
} else {
holderSection = (ViewHolderSectionName) convertView.getTag();
}

holderSection.section = (TextView) convertView
.findViewById(R.id.alphabet_letter);

holderSection.section
.setText(String.valueOf(si.getSectionLetter()));

} else {
Items ei = (Items) objItem;

if (convertView == null || !convertView.getTag().equals(holderName)) {
convertView = vi.inflate(R.layout.row, null);

holderName = new ViewHolderName();

holderName.name = (TextView) convertView
.findViewById(R.id.tvname);

holderName.radioGroup = (RadioGroup) convertView
.findViewById(R.id.radioGroup1);
holderName.checkAbsent = (RadioButton) convertView
.findViewById(R.id.check_absent);
holderName.checkPresent = (RadioButton) convertView
.findViewById(R.id.check_present);

Boolean present = map.get(position);

Log.e("Names Adapter", "is Selected " + present);

if (present != null && present.booleanValue()) {
// set present checkbox selected
holderName.checkPresent.setSelected(true);
} else {
holderName.checkAbsent.setSelected(true);
// set absent checkbox selected
}

holderName.radioGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.check_absent:
item = (Items) holderName.checkAbsent
.getTag();
absentSelected.add(item);
map.put(position, true);
break;

case R.id.check_present:
item = (Items) holderName.checkPresent
.getTag();
presentSelected.add(item);
map.put(position, true);
break;

default:
break;
}
}
});

/*
* holderName.checkAbsent .setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override public void onCheckedChanged( CompoundButton
* buttonView, boolean isChecked) { // TODO Auto-generated
* method stub holderName.checkAbsent.setSelected(isChecked); //
* selected1.add();
*
* } });
*
* holderName.checkPresent .setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override public void onCheckedChanged( CompoundButton
* buttonView, boolean isChecked) { // TODO Auto-generated
* method stub holderName.checkAbsent.setSelected(isChecked); //
* selected2.add();
*
* } });
*/
convertView.setTag(holderName);

} else {
holderName = (ViewHolderName) convertView.getTag();
}

if (holderName.name != null)
holderName.name.setText(ei.getName());
convertView.setOnCreateContextMenuListener(null);
}
return convertView;
}

public static class ViewHolderName {
public TextView name;
public RadioGroup radioGroup;
public RadioButton checkPresent;
public RadioButton checkAbsent;
}

public static class ViewHolderSectionName {
public TextView section;
}

}

最佳答案

您应该维护/保留“选中”单选按钮的状态,以使其在上下滚动时保持选中状态,因为在向上/向下滚动 ListView 时,列表适配器会重新创建每个列表项/ View 。

您应该通过在单选按钮上设置监听器来保留选中的单选按钮的位置/索引。

您可以使用 hashMap 将列表项保存为 KEY 并将“已检查”状态保存为值。当用户选中该框时,将键值添加到 map ,当用户取消选中该框而不是从 map 中删除键时。

在渲染 ListView 中使用此 map 。代码如下:

private Map<Integer, Boolean> map =  new HashMap<Integer, Boolean>();    

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

Boolean present = map.get(position);
if(present != null && present.booleanValue()){
// set present checkbox selected
} else {
// set absent checkbox selected
}

holderName.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.check_absent:
map.put(position, false)
break;
case R.id.check_present:
map.put(position, true)
break;
}
}
});
}

关于android - 单选按钮在分段 ListView 滚动时更改状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com