简体   繁体   中英

RecyclerView scroll to position of child view's child

I want to scroll to the position in the picture: 需求

Here is my code but there is NullPointerException

public class ViewMenu extends LinearLayout {
    protected Handler mHandler;
    @BindView(R.id.recycler)
    protected RecyclerView mRecycler;
    //...

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this);
        mHandler = new Handler();
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        //...
        Message msg=new Message();
        msg.what=1;
        Bundle bundle=new Bundle();
        bundle.putInt("targetPosition",3);//assume this is a valid position
        bundle.putInt("targetChild",2);//assume this is a valid child
        msg.setData(bundle);
        handler.sendMessage(msg);
    }
    class Handler extends android.os.Handler {
        @Override
        public void handleMessage(Message msg) {
            int targetPosition=msg.getData().getInt("targetPosition");
            int targetChild=msg.getData().getInt("targetChild");
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRecycler.getLayoutManager();
            ViewGroup targetViewGroup = (ViewGroup) layoutManager.findViewByPosition(targetPosition);//targetViewGroup becomes null
            View targetView = targetViewGroup.getChildAt(targetChild);//NullPointerException
            layoutManager.scrollToPositionWithOffset(targetPosition, targetView.getLeft());
        }
    }
}

I think the problem is that when the targetViewGroup is invisible findViewByPosition returns null. Can anyone find a better way to do that?

Finally I solved it myself. The cause while I guess, is when you call layoutManager.scrollToPositionWithOffset() method, a scroll event is sent to the handler. The recycler view will not do a real scroll until the looper got the message. So the proper way is use handler.post() after calling scrollToPositionWithOffset().

LinearLayoutManager layoutManager = (LinearLayoutManager) mRecycler.getLayoutManager();
//scroll to make the target page visible
if (page < layoutManager.findFirstVisibleItemPosition())
    //if the target page is in left and invisible, post: scroll to show one pixel in the screen left
    layoutManager.scrollToPositionWithOffset(page + 1, 1);
else if (page > layoutManager.findLastVisibleItemPosition())
    //if the target page is in right and invisible, post: scroll to show one pixel in the screen right
    layoutManager.scrollToPositionWithOffset(page, mRecycler.getWidth() - mRecycler.getPaddingLeft() - mRecycler.getPaddingRight() - 1);
//post: scroll to candidate
getHandler().post(() -> {
    ViewGroup pageView = (ViewGroup) mRecycler.findViewHolderForAdapterPosition(page).itemView;
    View candView = pageView.getChildAt(cand);
        layoutManager.scrollToPositionWithOffset(page, left ? candView.getLeft() : candView.getRight());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM