0

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.

Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48

2 Answers2

1

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.

Ernest Zamelczyk
  • 2,562
  • 2
  • 18
  • 32
  • For the sake of testing I converted **RadioButton** to **TextView**. I tried to setting clickable false in XML file but facing same issue. – Maulik Dodia Aug 05 '19 at 06:32
  • I recreated your solution in my project and it's working correctly with `android:clickable="false"` on RadioButton and `android:clickable="true" android:focusable="true"` on the root view. Maybe try that? – Ernest Zamelczyk Aug 05 '19 at 06:40
  • Let me give another shot to your solution. Give me some minutes. I will let you know@ErnestZamelczyk – Maulik Dodia Aug 05 '19 at 06:44
  • Can you give me your code? I have tried the same thing you said, but is isn't working here@ErnestZamelczyk – Maulik Dodia Aug 05 '19 at 07:35
  • It's literally just a simple adapter with onClickListener on root. Maybe this line from your `onBindViewHolder` is interfering?: `viewHolder.rb_filter_category.setClickable(true);` – Ernest Zamelczyk Aug 05 '19 at 07:40
  • I'm happy to hear that. – Ernest Zamelczyk Aug 05 '19 at 08:04
0

What is exactly the problem: with a parent layout, the radiobutton is displayed as selected but the method

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

is not properly called?

So for me, it seems normal, since view is now the parent layout, and when you click item, you select the radio button firstly. Normally, if you put padding the parent layout, and click on the border of item, it must work.

Just set onClickListener to your radiobutton should solve the problem

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

And avoid snake case for variable name in java.

Edit: for less call, make the viewholder implement OnClickListener, and add

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

in the onclick method

Turvy
  • 882
  • 1
  • 8
  • 23
  • Let me check this solution. Thanks for the other advices@FlorentBreton – Maulik Dodia Aug 05 '19 at 06:37
  • Your solution worked for me. But I'm wondering what's the exact difference between `view.setOnClickListener` and `rb_filter_category.setOnClickListener` – Maulik Dodia Aug 05 '19 at 07:41
  • Don't write like that. If the viewholder implement onClickListener, it aks you to add an onClick method where you write the good code, and in the constructor, add view.setOnClickListener(this) and rb_filter_category.setOnClickListener(this) – Turvy Aug 05 '19 at 07:46
  • I understood your point, but I want to know the difference between `view.setOnClickListener` and rb_filter_category.setOnClickListener`? Actually @ErnestZamelczyk solutions worked for me, without doing major changes. Thanks for your help – Maulik Dodia Aug 05 '19 at 08:02