listView 项目上有多个 ToggleButton。单击其中一项后,其他项就会打开/关闭

发布于 2024-12-20 01:27:12 字数 1359 浏览 2 评论 0原文

我有一个包含多个 ToggleButton 项目的列表视图。当我点击其中一个时,另一个就会打开/关闭。但是,它执行与右侧 ListView 项目相关的操作。

它们在自定义适配器中进行管理,这是我的代码:

public class AdapterContacts extends BaseAdapter implements OnClickListener {

ToggleButton btnIsSending;
     .
     .
     .

   public View getView(int position, View convertView, ViewGroup viewGroup) {
    Contact entry = contactsList.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.contact_row, null);
    }
     .
     .
     .
   // isSending button
    btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
    btnIsSending.setFocusableInTouchMode(false);
    btnIsSending.setFocusable(false);
    btnIsSending.setOnClickListener(this);
    btnIsSending.setTag(entry);

    return convertView;
}

    public void onClick(View view) {
    final Contact entry = (Contact) view.getTag();
    Log.d(TAG, "entry " + entry.getPhoneNr());

    LookalizeData lookData = (LookalizeApp.getContext()).lookalizeData;
    // Toggle send button
    if( view.getId() == btnIsSending.getId()){
        if(btnIsSending.isChecked())
             ...
        else
                     ... 
      }       

还有其他按钮,它们工作正常。

有什么想法或经验可以分享吗?

I have a list view with several ToggleButton items. When I click one, another gets turned on/off. However, it executes the action related to the right ListView item.

They are managed in a custom adapter, I here is my code:

public class AdapterContacts extends BaseAdapter implements OnClickListener {

ToggleButton btnIsSending;
     .
     .
     .

   public View getView(int position, View convertView, ViewGroup viewGroup) {
    Contact entry = contactsList.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.contact_row, null);
    }
     .
     .
     .
   // isSending button
    btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
    btnIsSending.setFocusableInTouchMode(false);
    btnIsSending.setFocusable(false);
    btnIsSending.setOnClickListener(this);
    btnIsSending.setTag(entry);

    return convertView;
}

    public void onClick(View view) {
    final Contact entry = (Contact) view.getTag();
    Log.d(TAG, "entry " + entry.getPhoneNr());

    LookalizeData lookData = (LookalizeApp.getContext()).lookalizeData;
    // Toggle send button
    if( view.getId() == btnIsSending.getId()){
        if(btnIsSending.isChecked())
             ...
        else
                     ... 
      }       

There are other buttons and they work fine.

Any idea or experience to share?

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

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

发布评论

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

评论(1

阪姬 2024-12-27 01:27:13

在你的 getView 中,你必须有这样的东西(这段代码让你知道它在你的情况下如何工作):

ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();

btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
btnIsSending.setFocusableInTouchMode(false);
btnIsSending.setFocusable(false);
btnIsSending.setTag(entry);

btnIsSending.setChecked(false);

// Cursor is passed as an argument.
if(sba != null)
  if(sba.get(cursor.getPosition()))
     btnIsSending.setChecked(true);

这就是你在 Activity 方面需要的。

您已通过 xml 中的 android:choiceMode 或使用 setChoiceMode 将 ListView 设置为多选模式。

您必须删除按钮上的 onClick 侦听器。无论您在按钮的 onClick 中做什么,都必须将该逻辑添加到 ListActivtiyonListItemClick 中。

In your getView you have to something like this (this code is give you an idea how it will work in your case):

ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();

btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
btnIsSending.setFocusableInTouchMode(false);
btnIsSending.setFocusable(false);
btnIsSending.setTag(entry);

btnIsSending.setChecked(false);

// Cursor is passed as an argument.
if(sba != null)
  if(sba.get(cursor.getPosition()))
     btnIsSending.setChecked(true);

That said this what you need on the Activity side.

You have make your ListView to multi-selection mode by android:choiceMode in xml or using setChoiceMode.

You have to remove your onClick listener on buttons. Whatever you doing in onClick of button, you have to add that logic to the onListItemClick of ListActivtiy.

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