【问题标题】:How do a custom listview item with a radiobutton and single choice如何使用单选按钮和单选自定义列表视图项
【发布时间】:2011-12-08 05:51:46
【问题描述】:

我有这个列表视图项的布局:

<?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>

我希望我的用户只选择一项,如何控制?例如,他选择第二个项目,然后所有其他项目保持未选中状态,当他选择其他项目时,所有其他项目保持未选中状态。

解决方案:

在 ListView 项目布局中使用这个类:

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();
        }
    }
}

现在您只需要在 Layout Item 中使用 CheckedTextView:

<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 将控制选择模式,例如:

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

【问题讨论】:

  • 你找到解决办法了吗?你能分享一下吗?谢谢。
  • 请分享你的解决方案..

标签: android listview radio-button


【解决方案1】:

它对我有用

第 1 步:

在自定义适配器文件中声明这个

int selectedIndex = -1;

第二步:单行自定义布局

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

第三步:

  <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 步:在自定义适配器中创建接口

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

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

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

第 6 步:最后一步将 Selected Index 设置为接口

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


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

            }
        });

【讨论】:

    【解决方案2】:

    你可以通过以下想法得到你的解决方案

    http://www.androidpeople.com/android-custom-listview-tutorial-part-1

    您必须为您的 CustomListView 编写自定义适配器。希望这对您有用 享受

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2013-05-08
      • 2013-01-14
      • 2019-02-16
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多