Skip to content Skip to sidebar Skip to footer

Recyclerview Onclick Not Working Properly?

I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following: <

Solution 1:

try setting android:focusableInTouchMode="false" to your imageview. and remove android:clickable="true" or set it to false

Solution 2:

Use viewHolder.itemView like :

viewHolder.itemView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
             //your action
        });

This will enable click action on whole RecyclerView item.

Solution 3:

Solution 4:

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@id/card_view"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_gravity="center"android:layout_margin="@dimen/card_margin_grid"android:clickable="true"android:focusableInTouchMode="true"card_view:cardCornerRadius="@dimen/card_album_radius"><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@id/thumbnail"android:layout_width="fill_parent"android:layout_height="wrap_content"android:adjustViewBounds="true"android:background="?selectableItemBackgroundBorderless"android:clickable="false"android:focusableInTouchMode="false"android:scaleType="fitCenter" /><TextViewandroid:id="@id/title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/thumbnail"android:layout_margin="@dimen/album_title_padding"android:textColor="@color/album_title"android:textSize="@dimen/album_title" /></RelativeLayout></android.support.v7.widget.CardView>

Solution 5:

public class OffersRecycleViewAdapter extends RecyclerView.Adapter {

private Activity mActivity;
private List<OfferShopModel> offerShopModels = newArrayList<>();
ArrayList<String> allColors = newArrayList<String>();


publicOffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
    this.offerShopModels = offerShopModels;
    this.mActivity = activity;
}

// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);@Overridepublic ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    Viewv= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);

    returnnewItemViewHolder(v);
}

@TargetApi(Build.VERSION_CODES.M)@OverridepublicvoidonBindViewHolder(ItemViewHolder itemViewHolder, finalint position) {

    itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());

}

@OverridepublicintgetItemCount() {
    return offerShopModels.size();
}

classItemViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {
    ImageView mLinearLayoutBack;
    TextView mOffer;

    ItemViewHolder(View itemView) {
        super(itemView);
        mOffer = (TextView) itemView.findViewById(R.id.offer);        
        mOffer.setOnClickListener(this);

    }

    @OverridepublicvoidonClick(View v) {

  // call click event

    }
}

}

Post a Comment for "Recyclerview Onclick Not Working Properly?"