gpt4 book ai didi

show cab 自定义适配器中的 android 复选框

转载 作者:行者123 更新时间:2023-11-30 02:55:29 24 4
gpt4 key购买 nike

我尝试使用自定义适配器实现上下文操作栏(驾驶室)。首先,我用 cab 制作了简单的 ListView 并且它起作用了。其次,我尝试使自定义适配器包含 CheckBoxTextViewCheckBox 用于 cab,但它不起作用。

public class MainActivity extends Activity {

ListView lv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<String> arr = new ArrayList<String>();
arr.add("1");
arr.add("2");
arr.add("3");
arr.add("4");
arr.add("5");
lv1 = (ListView)findViewById(R.id.listView1);
lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv1.setAdapter(new Adapters(this, R.layout.item, arr));
lv1.setMultiChoiceModeListener(new ModeCallback());
}

private class Adapters extends ArrayAdapter<String> implements OnCheckedChangeListener{

Context context;
ArrayList<String> arr;
CheckBox cb;
int position;
ViewGroup parent;

public Adapters(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
this.context = context;
this.arr = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if(v == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item, null);
}
TextView tv = (TextView)v.findViewById(R.id.tv1);
tv.setText(arr.get(position));
cb = (CheckBox)v.findViewById(R.id.cb1);
cb.setOnCheckedChangeListener(this);
this.parent = parent;
this.position = position;
return v;
}

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
((ListView)parent).setItemChecked(position, true);
}

}

private class ModeCallback implements ListView.MultiChoiceModeListener {

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
mode.setTitle("Select Items");
setSubtitle(mode);
return true;
}

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}

public void onDestroyActionMode(ActionMode mode) {
}

public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
setSubtitle(mode);
}

private void setSubtitle(ActionMode mode) {
final int checkedCount = lv1.getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + checkedCount + " items selected");
break;
}
}
}

问题是 CheckBox 没有改变。我真的很想知道这个,找不到任何类似的问题。

最佳答案

事实上,问题在于添加可聚焦元素(如CheckBoxButton)会禁用点击整个列表项的能力并且必须禁用它们的可聚焦性。您也不应该向主布局添加任何可点击的标志。

在 xml 中为您的 checkboxfocusable 设置为 false

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>

关于show cab 自定义适配器中的 android 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23362099/

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