【问题标题】:Failed to highlight a selected item on RecyclerView无法在 RecyclerView 上突出显示所选项目
【发布时间】:2016-06-14 04:17:45
【问题描述】:

我有一个使用 Fragments 拆分视图屏幕的主详细信息应用程序。主屏幕是一个包含 RecyclerView 的列表。这是布局 XML。

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

      <TextView
            android:layout_width="match_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Page Title" />

      <android.support.v7.widget.RecyclerView
             android:id="@+id/page_list"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />                

</LinearLayout>

项目行布局xml如下。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:id="@+id/item_layout">

<TextView
    android:id="@+id/contact_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    />

<Button
    android:id="@+id/message_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textSize="10sp"
    />
</LinearLayout> 

在适配器类上,我创建了一个变量来保存所选项目的位置

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
     private int selectedPos;

     @Override
     public void onBindViewHolder(ListViewHolder holder, int position) {
       holder.itemView.setSelected(selectedPos == position);
       holder.bind(mData.get(position));
     }

     class ListViewHolder extends RecyclerView.ViewHolder {
         public ListViewHolder(View itemView) {
             super(itemView);
         }
         public void bind(ListEntity entity) {
           //UI rendering
         }
         @OnClick(R.id.layout_campaign)
         public void onItemClicked(View view) {
            notifyItemChanged(selectedPos);
            selectedPos = getLayoutPosition();
            notifyItemChanged(selectedPos);
         }
}

当我单击列表中的某个项目时,该列表项目未突出显示。看了很多关于这个问题的类似帖子,但无法让它工作。我错过了什么吗?

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    如果您想在用户按下行时突出显示,那么只需在行的父布局的背景中使用选择器。 row_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
          <item android:state_selected="true"
               android:drawable="@color/red" /> <!-- pressed -->
         <item android:state_pressed="true"
               android:drawable="@color/red" /> <!-- pressed -->
        <item android:drawable="@color/gray" /> <!-- default -->
    </selector>
    

    将此xml放入drawable并设置为父布局的背景。

    希望它会起作用:)

    【讨论】:

    • 感谢您的回复!如何保留包含波纹动画并使用高亮逻辑的默认行选择器?
    • 要突出显示,只需在行对象中放置一个标志。如果用户单击该行,则将该标志标记为真。所以现在使用这个变量你可以控制你的行的背景,如果它是真的,这意味着它被选中并通过你选择的颜色更改背景(在 onBind 中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多