奇怪的行为 - 如果存在根布局,则 Recyclerview onItemClick 无法正常工作

Strange behaviour - Recyclerview onItemClick isn't working properly if Root Layout is there

问题: 我有一个 recyclerview,在那个 recyclerview 的适配器中,我只有 RadioButton,没有任何父布局,如 LinearLayoutRelativeLayout 。这次 onItemClick 工作正常。如果我将添加像 LinearLayoutRelativeLayout 这样的父布局,它不会调用 onItemClick。

我的代码如下。

FilterJobsCategoryAdapter.java

    public class FilterJobsCategoryAdapter
        extends
        RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements
        APIServiceConstants {

    private int lastSelectedPosition = -1;

    private Context mContext;
    private List<String> mItem;
    private OnItemClickListener mOnItemClickListener;

    public FilterJobsCategoryAdapter(Context context, List<String> item) {
        this.mItem = item;
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.rv_item_filter_jobs_category, parent, false);
        return new RecyclerViewHolders(layoutView);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {

        final RecyclerViewHolders viewHolder = (RecyclerViewHolders) holder;
        viewHolder.rb_filter_category.setText(mItem.get(position));

        if (lastSelectedPosition == -1 && position == 0) {

            viewHolder.rb_filter_category.setChecked(true);
            viewHolder.rb_filter_category.setClickable(false);
            viewHolder.rb_filter_category.setTextColor(mContext.getResources().getColor(R.color.colorPrimaryBlue));
            viewHolder.rb_filter_category.setBackgroundColor(mContext.getResources().getColor(R.color.colorPrimaryBlue));
            viewHolder.rb_filter_category.setTextColor(Color.WHITE);

        } else {

            if (lastSelectedPosition == position) {
                viewHolder.rb_filter_category.setChecked(true);
                viewHolder.rb_filter_category.setClickable(false);
                viewHolder.rb_filter_category.setTextColor(mContext.getResources()
                        .getColor(R.color.colorPrimaryBlue));
                viewHolder.rb_filter_category.setBackgroundColor(mContext.getResources().getColor(R.color.colorPrimaryBlue));
                viewHolder.rb_filter_category.setTextColor(Color.WHITE);

            } else {
                viewHolder.rb_filter_category.setChecked(false);
                viewHolder.rb_filter_category.setClickable(true);
                viewHolder.rb_filter_category.setTextColor(mContext.getResources().getColor(R.color.blackTextColor));
                viewHolder.rb_filter_category.setBackgroundColor(mContext.getResources()
                        .getColor(R.color.lightGreyBg80));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mItem.size() > 0 ? mItem.size() : 0;
    }

    class RecyclerViewHolders extends RecyclerView.ViewHolder {

        @BindView(R.id.rb_filter_category)
        RadioButton rb_filter_category;

        RecyclerViewHolders(View view) {
            super(view);

            ButterKnife.bind(this, view);

            view.setOnClickListener(v -> {
                lastSelectedPosition = getAdapterPosition();
                mOnItemClickListener.onClick(lastSelectedPosition);
                notifyDataSetChanged();
            });
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}

rv_item_filter_jobs_category.xml(onItemClick 不适用于此代码)

    <?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/rb_filter_category"
        style="@style/FilterCategoryRadioButtonStyle" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp1"
        android:background="@color/lightGreyBg80" />
</LinearLayout>

rv_item_filter_jobs_category.xml(onItemClick 适用于此代码)

    <?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rb_filter_category"
    style="@style/FilterCategoryRadioButtonStyle" />

style.xml

<style name="FilterCategoryRadioButtonStyle" parent="@android:style/Widget.CompoundButton">
    <item name="android:fontFamily">@font/roboto_medium</item>
    <item name="android:gravity">start|center_vertical</item>
    <item name="android:maxLines">1</item>
    <item name="android:includeFontPadding">false</item>
    <item name="android:hint">@string/str_view_by</item>
    <item name="android:padding">@dimen/dp10</item>
    <item name="android:textSize">@dimen/sp12</item>
    <item name="android:ellipsize">end</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:textColor">@color/blackTextColor</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:button">@null</item>
</style>

我不明白这是怎么回事..很奇怪!任何指导肯定会受到赞赏。谢谢

尝试在 RadioButton 上将 clickable 设置为 false。由于您实际上是在根视图上设置 onClickListener,因此 RadioButton 可能会拦截触摸事件,因此它永远不会在根视图上调用。

究竟是什么问题:在父布局中,单选按钮显示为 selected,但方法

view.setOnClickListener(v -> {
                lastSelectedPosition = getAdapterPosition();
                mOnItemClickListener.onClick(lastSelectedPosition);
                notifyDataSetChanged();
            });

叫法不对?

所以对我来说,这似乎很正常,因为视图现在是父布局,当您单击项目时,您首先 select 单选按钮。通常情况下,如果你在parent layout中放置padding,然后点击item的border,肯定可以。

只需将 onClickListener 设置为您的单选按钮即可解决问题

rb_filter_category.setOnClickListener(v -> {
        lastSelectedPosition = getAdapterPosition();
        mOnItemClickListener.onClick(lastSelectedPosition);
        notifyDataSetChanged();
    });

并在 java.

中避免变量名的蛇形大小写

编辑:为了减少调用,让 viewholder 实现 OnClickListener,并添加

lastSelectedPosition = getAdapterPosition();
                mOnItemClickListener.onClick(lastSelectedPosition);
                notifyDataSetChanged();

在onclick方法中