如何使用单选按钮和单选按钮自定义列表视图项 [英] How do a custom listview item with a radiobutton and single choice

查看:93
本文介绍了如何使用单选按钮和单选按钮自定义列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表视图项具有以下布局:

I have this layout for the listview item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@color/backgroundTela">

  <TextView  
    android:id="@+id/tvwLvItem"
    android:layout_width="0sp"
    android:layout_height="50sp"
    android:layout_weight="8"
    android:layout_gravity="center"
    android:text="Texto"
    style="@style/lvwTexto">    
  </TextView>

  <RadioButton 
    android:id="@+id/rdbDefault"
    android:layout_width="0sp"
    android:layout_height="wrap_content"
    android:layout_weight="1.2"/>
</LinearLayout>

我希望我的用户仅选择一项,如何控制呢?例如,他选择第二项,则所有其他项保持未选中状态,当他选择其他项时,所有其他项保持未选中状态.

And I want that my user select just one item, how control this? For exemple him select the second item then all other itens stay unselected, and when him select other item all other item stay unselected.

解决方案:

在ListView项布局中使用此类:

Use this class in ListView Item Layout:

public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private CheckedTextView _checkbox;

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // find checked text view
        int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i) {
            View v = getChildAt(i);
            if (v instanceof CheckedTextView) {
                _checkbox = (CheckedTextView) v;
            }
        }
    }

    @Override
    public boolean isChecked() {
        return _checkbox != null ? _checkbox.isChecked() : false;
    }

    @Override
    public void setChecked(boolean checked) {
        if (_checkbox != null) {
            _checkbox.setChecked(checked);
        }
    }

    @Override
    public void toggle() {
        if (_checkbox != null) {
            _checkbox.toggle();
        }
    }
}

现在,您只需要在布局项中使用CheckedTextView:

Now you only need CheckedTextView in your Layout Item:

<com.developer_lib.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/dimDefaultButtonMeasures"
    android:background="@drawable/list_item_background"
    android:orientation="horizontal" >

    <ImageView
        android:contentDescription="@string/cd_contato_foto"
        android:id="@+id/imvFoto"
        android:layout_width="@dimen/dimDefaultButtonMeasures"
        android:layout_height="@dimen/dimDefaultButtonMeasures"
        android:layout_gravity="center" />

    <CheckedTextView
        android:id="@+id/chlSelecao"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:padding="4dp"
        android:singleLine="true" />

最后,您的Listview将控制选择模式,例如:

Finnaly your Listview will control the choice mode, for example:

<ListView
android:id="@+id/lvwHorarios"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" />

推荐答案

对我有用的

第1步:

在自定义适配器文件中只需声明

in Custom Adapter file just declare this

int selectedIndex = -1;

第2步:单行自定义布局

Step 2:Single Row Custom Layout

   <RadioButton
    android:id="@+id/radio_list"
    android:checked="false"
    android:focusable="false"
    android:clickable="false" />

第3步:

  <ListView
            android:id="@+id/liststorecard"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            **android:descendantFocusability="beforeDescendants"
            android:choiceMode="singleChoice"**
            android:layout_weight="1"
          />

第4步:在自定义适配器中创建接口

Step 4: Create Interface in Custom Adapter

 public void setSelectedIndex(int index)
   {
        selectedIndex = index;
    }

第5步:在getview方法中编写代码

Step 5: In getview method write code

  if(selectedIndex == position)
   {
    holder.rblist.setChecked(true);
   }
  else
   {
    holder.rblist.setChecked(false);
   }

第6步:最后一步将所选索引设置为界面

Step 6: final step set Selected Index to interface

liststorecard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                storeCardAdapter.setSelectedIndex(position);
                storeCardAdapter.notifyDataSetChanged();

            }
        });

这篇关于如何使用单选按钮和单选按钮自定义列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆