gpt4 book ai didi

android - ListView 中的复选框有时不起作用

转载 作者:行者123 更新时间:2023-11-29 14:04:08 25 4
gpt4 key购买 nike

我有一个对话框,其中包含带有复选框的 ListView 和一个按钮,如果至少选中一个复选框,该按钮就会变得可见。单击该按钮可从 ListView 中删除选中的行。但是在这个对话框中,列表中的复选框有一个奇怪的行为。例如,如果列表包含四行,并且我检查了其中的一行,则无法再检查更多项目。如果我将更多项目添加到列表中,那么我可以检查多个项目,但如果我删除一些项目,剩余的项目将变得无法检查。

附言。有没有办法修复或解决它?

更新 1:我发现复选框不会变得完全不活动,如果尝试多次单击一个复选框,它可能会变为选中状态。看起来被识别为onClick区域的复选框周围的区域设置得太小了。

更新 2:我还发现,如果我从对话框初始化代码中注释这一行

getWindow().setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

然后列表中的所有复选框在对话框实例化后立即变为不可选中。

对话框布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="200dip"
android:orientation="vertical"
android:background="@android:color/white"
>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="@color/list_divider"
android:dividerHeight="2px"
android:focusable="false"
android:clickable="false"
android:choiceMode="multipleChoice"
/>
<LinearLayout
android:id="@+id/buttons_pane"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
<Button
android:id="@+id/misc_list_remove_btn"
android:layout_width="fill_parent"
android:layout_height="48dip"
android:text="@string/remove"
/>
</LinearLayout>
</LinearLayout>

项目布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="47dip"
android:orientation="horizontal"
android:background="@android:color/white"
>
<CheckBox
android:id="@+id/misc_selectable_list_item_chk"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:checked="false"
/>
</LinearLayout>

我是这样创建对话框的

public ShowSelectableListDialog (Activity context, int titleId)
{
super (context);
setContentView (R.layout.misc_selectable_list_dlg);
setTitle (titleId);
getWindow ().setLayout (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
adapter_ = setupListAdapter (getInitialValues (), context);
ListView listView = (ListView)findViewById (R.id.list_view);
listView.setAdapter (adapter_);
listView.setItemsCanFocus (false);
Button removeButton = (Button)findViewById (R.id.misc_list_remove_btn);
removeButton.setOnClickListener (this);
}

和列表中的项目

@Override
protected View instantiateView (Object ... params)
{
String title = (String)params[0];
CompoundButton.OnCheckedChangeListener checkedChangeListener =
(CompoundButton.OnCheckedChangeListener)params[1];

LayoutInflater inflater = Utils.getInflater (getContext ());
View view = inflater.inflate (R.layout.misc_selectable_list_item, null);
view.setId (title.hashCode ());
CheckBox chk = (CheckBox)view.findViewById (R.id.misc_selectable_list_item_chk);
chk.setText (title);
chk.setOnCheckedChangeListener (checkedChangeListener);
return view;
}

最佳答案

问题出现是因为我在列表适配器的项目中缓存了新创建的 View 。如果列表项不包含复选框或按钮等控件,它工作正常(至少没有明显的问题),但在某些情况下可能成为问题的原因。

避免列表项 View 重新实例化的正确方法是使用 getView 的 convertView 参数方法,在 android.widget.Adapter 中声明。

结案。

关于android - ListView 中的复选框有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078863/

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