Android:ListView CheckBox 无意中被选中

发布于 2024-11-08 02:34:33 字数 423 浏览 0 评论 0原文

我有一个 ListView,它布局在标题栏下方,布局在页脚栏上方。 ListView 的每一行都包含一个 LinearLayout,并排有一个 CheckBox 和 TextView。现在,当我一次可以显示更多行时,您当然可以向下或向上滚动。但是,一旦我向下或向上滚动,第一个和/或最后一个复选框就会被自动检查。我的手指离您选择复选框的位置很远。可能是什么原因造成的? 注意我在这里使用了代码: http://www.anddev.org/checkbox_text_list___extension_of_iconified_text_tutorial-t771.html

I have a ListView which is layout below a header bar and layout above a footer bar. Each row of the ListView contains a LinearLayout with a CheckBox and TextView side by side. Now, when I have more rows that can be shown at a time, you can of course scroll down or up. However, once I scroll down or up the first and / or last CheckBox is being checked automatically. My finger is nowhere near where you would select the check box. What could be causing this?
Note I used code here: http://www.anddev.org/checkbox_text_list___extension_of_iconified_text_tutorial-t771.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2024-11-15 02:34:33

简要查看相关链接下显示的代码,我可以看到以下内容:

 public View getView(int position, View convertView, ViewGroup parent){
      CheckBoxifiedTextView btv;
      if (convertView == null) {
           btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
      } else { // Reuse/Overwrite the View passed
           // We are assuming(!) that it is castable!
           CheckBoxifiedText src = mItems.get(position);
           btv = (CheckBoxifiedTextView) convertView;
           btv.setCheckBoxState(src.getChecked());  // set checked state
           btv = (CheckBoxifiedTextView) convertView;
           btv.setText(mItems.get(position).getText());
      }
      return btv;
 }

正如您所看到的,视图在滚动期间被重用,并且复选框状态是使用方法 setCheckBoxState 设置的。
然后在 CheckBoxifiedTextView 中,您可以找到:

 public void setCheckBoxState(boolean bool)
 {
     mCheckBox.setChecked(mCheckBoxText.getChecked());
     mCheckBoxText.setChecked(true); // <-- HERE !
 } 

在第 4 行中,有 setChecked(true) 硬编码,这可能会导致问题。

Looking briefly into code presented under link in question I can see the following:

 public View getView(int position, View convertView, ViewGroup parent){
      CheckBoxifiedTextView btv;
      if (convertView == null) {
           btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
      } else { // Reuse/Overwrite the View passed
           // We are assuming(!) that it is castable!
           CheckBoxifiedText src = mItems.get(position);
           btv = (CheckBoxifiedTextView) convertView;
           btv.setCheckBoxState(src.getChecked());  // set checked state
           btv = (CheckBoxifiedTextView) convertView;
           btv.setText(mItems.get(position).getText());
      }
      return btv;
 }

As you can see the view is reused during scroll and checkbox state is set with method setCheckBoxState.
Then in CheckBoxifiedTextView you can find:

 public void setCheckBoxState(boolean bool)
 {
     mCheckBox.setChecked(mCheckBoxText.getChecked());
     mCheckBoxText.setChecked(true); // <-- HERE !
 } 

where in 4th line there is setChecked(true) hardcoded which might be causing the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文