Recyclerview, I can't get item view with getChildAt(position). Null object reference

In my case it,is work like a charm

View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
    View icon = viewItem.findViewById(R.id.view);

I solved finally with this (source):

recyclerViewDrawer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
            TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.textViewDrawerItemTitle);
            imageViewDrawerIcon.setColorFilter(color);
            if (Build.VERSION.SDK_INT > 15) {
                imageViewDrawerIcon.setImageAlpha(255);
            } else {
                imageViewDrawerIcon.setAlpha(255);
            }
            textViewDrawerTitle.setTextColor(color);
            RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(0).findViewById(R.id.relativeLayoutDrawerItem);
            relativeLayoutDrawerItem.setFocusableInTouchMode(true);
            // unregister listener (this is important)
            recyclerViewDrawer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

By the way, if someone have a better solution I would like to know it.


This worked for me.

recyclerView.post(new Runnable() {
  @Override
  public void run() {
    View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
    View icon = viewItem.findViewById(R.id.view);
  }
});