首页 > 解决方案 > Android Recycler View 中的涟漪效应不起作用

问题描述

当单击该行时,我正在尝试在我的 Recycler 视图中为整行添加涟漪效果(就像我们在列表视图中看到的那样)。

我努力了

   android:background="?android:attr/selectableItemBackground"
   android:clickable="true"
   android:focusable="true"

但是,我仍然无法获得所需的行为。即使我尝试过

   android:foreground="?android:attr/selectableItemBackground"

但还是没有起色

我的listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true">
    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <TextView
           android:id="@+id/heading"
           android:text="Title"
           android:textSize="20dp"
           android:paddingTop="5dp"
           android:textStyle="bold"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
       <TextView
           android:layout_toLeftOf="@+id/subHeading"
           android:id="@+id/size"
           android:text="Sub Title"
           android:paddingTop="3dp"
           android:paddingLeft="5dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
   </LinearLayout>
</LinearLayout>

还有我的 Recycler View 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingTop="@dimen/list_item_spacing_half"
        android:paddingBottom="@dimen/list_item_spacing_half"
        android:layout_alignParentTop="true"
        tools:context=".ListFragment"
        tools:listitem="@layout/listrow" />
    <Button
        android:id="@+id/Save"
        android:layout_width="match_parent"
        android:text="Save"
        android:layout_below="@+id/list"
        android:textAllCaps="false"
        android:layout_height="wrap_content"/>
</RelativeLayout>

这是我的适配器源代码

    private class listAdapter extends RecyclerView.Adapter<ViewHolder> {

        private final int mItemCount;
        final ArrayList<String> mTitles;
        final ArrayList<String> msTitles;
        ArrayList<RadioButton> radioButtons=new ArrayList<>();

        listAdapter(int itemCount, ArrayList<String> titles, ArrayList<String> sTitles) {
            mItemCount = itemCount;
            mtitles=titles;
            msTitles=sTitles;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.sTitle.setTag(position);
            holder.radioButton.setTag(position);
            holder.title.setTag(position);
            holder.title.setText(mTitles.get(position));
            holder.sTitle.setText(msTitles.get(position));
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ItemCheckChanged(v);
                }
            };
            holder.radioButton.setOnClickListener(listener);
            holder.sTitle.setOnClickListener(listener);
            holder.title.setOnClickListener(listener);
            if(radioButtons.size()==0){
                holder.radioButton.setChecked(true);
                selectedItem=0;
            }
            radioButtons.add(holder.radioButton);

        }

        @Override
        public int getItemCount() {
            return mItemCount;
        }

        void ItemCheckChanged(View v){
            selectedItem=(int)v.getTag();
            for(RadioButton radioButton:radioButtons){
                radioButton.setChecked(false);
            }
            radioButtons.get(selectedItem).setChecked(true);
            notifyDataSetChanged();

        }

        public int getSelectedIndex(){
            return selectedItem;
        }

    }

注意:我在 BottomSheetDialog 中使用这些所有东西可能是原因吗?

标签: androidandroid-layout

解决方案


您应该为已设置点击侦听器的视图设置背景。
如果要将侦听器设置为整行,则只需调用:
holder.itemView.setOnclickListener()并删除其余部分。


推荐阅读