-2

I need to click a row of RecyclerView programmatically

I tried - recyclerView.findViewHolderForAdapterPosition(2).itemView.performClick()

But it is giving

null pointer exception

sometime. I tried to check with trial and error I saw in a RecyclerView which is having 5 row and if I pass value more than 1 it's start giving null pointer exception.

A J
  • 4,542
  • 5
  • 50
  • 80

1 Answers1

1

I resolved the issue. Problem was the recyclerView was not ready when I am trying click event. So I have added some delay.

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            myRecyclerView.getChildAt(locationData - 1).performClick();
            myRecyclerView.scrollToPosition(locationData - 1);
        }
    }, 500);
A J
  • 4,542
  • 5
  • 50
  • 80