【问题标题】:how to add radio button in recycle view?如何在recyclerview中添加单选按钮?
【发布时间】:2017-01-02 15:07:12
【问题描述】:

我想在回收视图中为每个项目添加单选按钮。谁能知道如何在回收视图中添加单选按钮?

这是我的回收站视图:

我想要这个

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
tools:context=".UserSearchActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.SearchView
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionSearch|flagNoExtractUi"
            android:theme="@style/SearchView"
            app:defaultQueryHint="@string/hint_type_name_or_email" />
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_user"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize" />
</FrameLayout>

【问题讨论】:

  • 您的 list_item xml 文件在哪里?

标签: android listview android-studio radio-button android-recyclerview


【解决方案1】:

您必须为recycler view 项目添加单独的layout xml 文件并包含radio button

例如在 recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="New RadioButton" />

</LinearLayout>

然后使用 recycler list view adapter 更新项目值。

【讨论】:

  • 添加自定义回收器视图适配器类并处理适配器类中的单选按钮可见性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2023-03-15
  • 1970-01-01
  • 2012-09-26
  • 2018-06-01
  • 2012-05-20
  • 2013-06-12
相关资源
最近更新 更多