Search code examples
androidandroid-recyclerviewandroid-viewpager2

BUG Some row in recyclerview not clickable inside view pager in tablet


I have a layout with a view pager and consist of 2 fragments with recycler view enter image description here

The first four-row is clickable, but the other is not clickable, it's happen in tablet mode. In Phone mode, everything is working fine.

The strange thing is if the data is short(1 letter only), everything is clickable, but the data is long(more than 3 letters) then it's not clickable from row 4. The FAB button is working. I have already tried using physical tablet and emulated tablet.

I am using View Pager 2

Is this a bug? And how to solve this?

Edited, My RecyclerView Adapter code

private List<Craft> craftList;
private RecyclerViewClickListener clickListener;
private Context mContext;
public int selectedItem = -1;

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView name ;
    public View container;
    private RecyclerViewClickListener mListener;

    public MyViewHolder(View view, RecyclerViewClickListener listener) {
        super(view);
        mListener = listener;
        name = (TextView) view.findViewById(R.id.name);
        container = view.findViewById(R.id.container);

        container.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        selectedItem = getBindingAdapterPosition();
        if(selectedItem!= RecyclerView.NO_POSITION){
            mListener.OnButtonClick(v, getBindingAdapterPosition());
            notifyDataSetChanged();
        }
    }
}

public interface RecyclerViewClickListener {
    void OnButtonClick(View view, int position);
}

public CraftAdapter(List<Craft> craftList, RecyclerViewClickListener listener) {
    this.craftList = craftList;
    clickListener = listener;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.craft_item, parent, false);
    mContext = parent.getContext();
    return new MyViewHolder(itemView,clickListener);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Craft craft = craftList.get(position);
    holder.name.setText(craft.getName());
    if(mContext.getResources().getBoolean(R.bool.is_tablet)){
        if(selectedItem == position){
            holder.container.setBackgroundResource(R.color.light_blue);
        }
        else{
            holder.container.setBackgroundResource(R.color.white);
        }
    }
}

@Override
public int getItemCount() {
    return craftList.size();
}

Solution

  • After wasting so much time. I finally found the root cause.

    The cause is the layout using LinearLayout with weight. The weight make it not working, I think it's a bug. But I can solve it using constraintlayout

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:id="@+id/coordinator_layout">
    
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <fragment android:name="CraftFragment"
                android:id="@+id/list_fragment"
                android:layout_weight="2" -->>This is the root cause
                android:layout_width="0dp" -->> This is the root cause
                android:layout_height="match_parent"
                />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/dark_grey2"/>
            <FrameLayout
                android:id="@+id/detail_fragment"
                android:layout_weight="3"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
    
        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    So I change it to:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:id="@+id/coordinator_layout">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment android:name="CraftFragment"
            android:id="@+id/list_fragment"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/detail_fragment"
            app:layout_constraintHorizontal_weight="2"/>
    
    
    
        <FrameLayout
            android:id="@+id/detail_fragment"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toRightOf="@+id/list_fragment"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintHorizontal_weight="3"/>
    
        <View
            android:id="@+id/divider"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/dark_grey2"
            app:layout_constraintLeft_toRightOf="@id/list_fragment"
            app:layout_constraintRight_toRightOf="@id/list_fragment"/>
    </androidx.constraintlayout.widget.ConstraintLayout>