Recyclerview item click not always work and sometimes can work but with more more click (don't know how many click)

3.2k views Asked by At

Recyclerview item click not always work and sometimes can work but with more more click (don't know how many click).If just click once that can't clicked.

Note few days ago I tried to click once and it's worked, but now when I tried to running again using AS it's not work, even though I not modified that file

My code

public class RecyclerViewAdapterRiwayat extends RecyclerView.Adapter<RecyclerViewAdapterRiwayat.ViewHolder> {

    Context context;
    private static final String TAG = RecyclerViewAdapterRiwayat.class.getSimpleName();

    List<GetDataAdapterRiwayat> getDataAdapter;

    ImageLoader imageLoader1;
    String FIXURL = "http://192.168.1.101/AndroidFileUpload/";
    String url = FIXURL + "uploads/";
    String StatusRiwayat;


    public RecyclerViewAdapterRiwayat(List<GetDataAdapterRiwayat> getDataAdapter, Context context) {

        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items_riwayat, parent, false);

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {

        final GetDataAdapterRiwayat getDataAdapter1 = getDataAdapter.get(position);

        imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

        imageLoader1.get(url+getDataAdapter1.getFotoSetelahRiwayat(),
                ImageLoader.getImageListener(
                        Viewholder.networkImageView,//Server Image
                        R.mipmap.ic_launcher,//Before loading server image the default showing image.
                        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                )
        );
        Viewholder.networkImageView.setImageUrl(url+getDataAdapter1.getFotoSetelahRiwayat(), imageLoader1);


        Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());

                Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView ImageTitleNameView;
        public NetworkImageView networkImageView;

        public ViewHolder(View itemView) {

            super(itemView);
            ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item_riwayat);

            networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView2);
        }


    }
}
6

There are 6 answers

1
Pushpendra On

Try this:

   class ViewHolder extends RecyclerView.ViewHolder{

            public TextView ImageTitleNameView;
            public NetworkImageView networkImageView;

            public ViewHolder(View itemView) {

                super(itemView);
                ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item_riwayat);

                networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView2);


                itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                int position = getLayoutPosition(); // use this to get item from list
                    Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());

                    Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
                    context.startActivity(intent);
                }
            });
        }
3
Gaurav On

Your code

Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());

                Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
                context.startActivity(intent);
            }
        });

creating new listener every time to the itemView , Instead implement OnClickListener or pass null before assigning new Listener

 Viewholder.itemView.setOnClickListener(null);
Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());

                Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
                context.startActivity(intent);
            }
        });

Or add listener in ViewHolder onCreateViewHolder()

v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                int position = viewHolder.getAdapterPosition(); // use this to get item from list
                    Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());

                    Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
                    context.startActivity(intent);
                }
            });
0
Simon On

That's probably due to the parent's onTouch method which intercepts the touch event. You need to check if there is a parent view like SwipeLayout.

0
kunwar97 On

Add these to your parent element of R.layout.recyclerview_items_riwayat

android:clickable="false"
android:focusable="false"
0
PieterAelse On

If you only have the issue after scrolling / flinging the RecyclerView, it's a known bug in the SupportLibrary

After a user scrolls, they cannot click on an item in a RecyclerView. (AOSP issue 66996774)

Issuetracker ID 66996774 and also 69823266 because it's still not fixed in 27.0.1. Even though Google states it has fixed it.

Most important: the fix here (by Chris Banes, Googler) works perfectly: https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2

0
Saeid Z On

Add this to child views of item layout (Not root element only childs!!) (R.layout.recyclerview_items_riwayat):

android:clickable="false"
android:focusable="false"