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

6

10

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);
        }


    }
}
Cranage answered 26/12, 2016 at 12:23 Comment(1)
did u got any solution ?Judaica
H
3

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

Hayrack answered 28/11, 2017 at 13:10 Comment(0)
G
0

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);
                }
            });
        }
Gayden answered 26/12, 2016 at 12:38 Comment(1)
still same. must with more click then can clickedCranage
M
0

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);
                }
            });
Morrie answered 26/12, 2016 at 12:39 Comment(3)
first way is not working.. and the second way get error cannot resolve method getLayoutPosition()Cranage
put that code in onCreateViewHolder right? it's same error cannot resolve method getAdapterPositionCranage
sorry I forgot to add, getAdapterPosition() method of view holder, So use its object to callMorrie
T
0

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

android:clickable="false"
android:focusable="false"
Torrie answered 28/11, 2017 at 13:41 Comment(0)
H
0

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

android:clickable="false"
android:focusable="false"
Halvorsen answered 26/2, 2023 at 20:1 Comment(0)
D
-1

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.

Diminished answered 9/10, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.