Search code examples
androidandroid-layoutandroid-recyclerviewonitemclicklistenerandroid-radiobutton

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


Problem: I'm having a recyclerview, in adapter of that recyclerview, I have solely RadioButton without any parent layout like LinearLayout or RelativeLayout. This time onItemClick is working fine. If I will add parent layout like LinearLayout or RelativeLayout, it doesn't invoke onItemClick.

My code is like below.

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 doesn't work for this code)

    <?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 works for this code)

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

I don't understand what is going on here.. it's strange! Any guidance would definitely be appreciated. Thanks.


Solution

  • Try setting clickable to false on the RadioButton. Since you're actually setting the onClickListener on the root view it is possible that the RadioButton intercepts the touch event so it's never called on the root.