0

I develop android application with adaptive design. For "large-land" screen configuration (i.e. for tablet) user has dual-panel interface with RecyclerView header list on the left and details information (for pointed header item) on the right.

At application start, for such configuration, it will be good to emulate user's click on the first headers list item in order to cover empty details info panel.

The question "HOW" to emulate item click for RecyclerView is very popular and has been answered: Clicking on item programmatically in RecyclerView , etc..

But I can not find the answer for the question "WHEN" I can properly call itemView.performClick() from my fragment owning the views?

My method:

public void clickItem(int position) {
  if (null == mRecyclerView)
    return;
  SingerViewHolder holder = (SingerViewHolder) mRecyclerView.findViewHolderForAdapterPosition(position);
  if (null == holder)
    return;
  holder.itemView.performClick();
}

The performClick() skips because holder is null for any position (e.g.,0). I've found the obvious answer: RecyclerView has not created its content yet to the moment when I call mAdapter.clickItem(0); Of course, I call it AFTER setting the adapter and even after getting the header list info from network with notifyDataSetChanged();

Here: Clicking on item programmatically in RecyclerView suggested just to add a small pause before call itemView.performClick() in order to give android to create RecyclerView content. Is it a correct and the only one way? I do not think so..

Not sure am I right, I've tried other workaround: How to know when the RecyclerView has finished laying down the items? But doing that:

  mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    if (ScreenConfiguration.getScreenConfigurationState() ==
        Constants.ScreenConfigurationState.TABLET_LANDSCAPE)
      /// dual-panel mode => click 1'st singer list item
      mAdapter.clickItem(1);
    }
  });

we get nulled ViewHolder anyway. Also, this listener fires many-many times and has to be removed ASAP..

(sorry for looooong prehistory)

So, the question is: is there any correct callback for RecyclerView to properly get its ViewHolder valuable?

Community
  • 1
  • 1

1 Answers1

0

You can try this

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
          recyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();

        }
    },100);
Kshitij Arora
  • 287
  • 3
  • 9