How to tell RecyclerView to start at specific item position

前端 未结 6 2176
难免孤独
难免孤独 2021-02-20 10:43

I want my RecyclerView with LinearLayoutManager to show up with scroll position at specific item after adapter got updated. (not first/last position) Means the at first (re-)lay

相关标签:
6条回答
  • 2021-02-20 11:20

    None of the methods above worked for me. I did the following using ViewTreeObserver that is triggered once its children have been added/changed visibility.

    recyclerView.apply {
       adapter = ...
       layoutManager = ...
    
       val itemCount = adapter?.itemCount ?: 0
       if(itemCount > 1) {
          viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
          override fun onGlobalLayout() {
             viewTreeObserver.removeOnGlobalLayoutListener(this)
             (layoutManager as? LinearLayoutManager)?.scrollToPosition(#PositionToStartAt)
          }
       }
    }
    

    Go ahead and set #PositionToStartAt to a specific value. You can also ensure that the RecyclerView initial position setting gets triggered once a specific number of children have been laid out to ensure it is set correctly.

    if(recyclerView.childCount() > #PositionCheck) {
       viewTreeObserver.removeOnGlobalLayoutListener(this)
       (layoutManager as? LinearLayoutManager)?.scrollToPosition(#PositionToStartAt)
    }
    
    0 讨论(0)
  • 2021-02-20 11:21

    If your only motive is to start the recyclerView from a specific position without any scroll-like animation I'll suggest using StaggeredGridLayoutManager

    val staggeredGridLayoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//or VERTICAL
                staggeredGridLayoutManager.scrollToPosition(specificPosition)
    
    recyclerView.apply{
        layoutManager = staggeredGridLayoutManager
    }
    
    0 讨论(0)
  • 2021-02-20 11:27

    You can try this, it will scroll to a position you want:

    rv.getLayoutManager().scrollToPosition(positionInTheAdapter).
    
    0 讨论(0)
  • 2021-02-20 11:27

    If you want to scroll to a specific position and that position is the adapter's position, then you can use StaggeredGridLayoutManager scrollToPosition

    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
           staggeredGridLayoutManager.scrollToPosition(10);
           recyclerView.setLayoutManager(staggeredGridLayoutManager);
    

    If I understand the question, you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView's item position.

    You can only achieve this through the LayoutManager.

    rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter);
    
    0 讨论(0)
  • 2021-02-20 11:36

    Another contribution to a long running question...

    As mentioned, layoutManager.scrollToPosition/WithOffset() does work to get the RecyclerView positioned, but timing this can be tricky. For example with variable length item views the RecyclerView has to work hard to get all the prior item views measured.

    The approach below simply delays telling the RecyclerView about the prior items, then calls notifyItemRangeInserted(0, offset). This enabled the view to appear in the right place, with no visible scrolling at the start, and ready to scroll back.

    private List<Bitmap> bitmaps = new ArrayList<>();
    
    ...
    
    private class ViewAdapter extends RecyclerView.Adapter<ViewHolder> {
        private volatile int offset;
        private boolean offsetCancelled;
    
        ViewAdapter(int initialOffset) {
            this.offset = initialOffset;
            this.offsetCancelled = initialOffset > 0;
        }
    
        @Override
        public int getItemCount() {
            return bitmaps.size() - offset;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(MyActivity.this);  // Or getContext() from a Fragment
    
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(lp);
    
            return new ViewHolder(imageView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.imageView.setImageBitmap(bitmaps.get(position + offset));
    
            if (!offsetCancelled) {
                offsetCancelled = true;
                recyclerView.post(() -> {
                    int previousOffset = offset;
                    offset = 0;
                    notifyItemRangeInserted(0, previousOffset);
                    Log.i(TAG, "notifyItemRangeInserted(0, " + previousOffset + ")");
                });
            }
        }
    }
    
    private class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageView;
    
        ViewHolder(@NonNull ImageView itemView) {
            super(itemView);
            this.imageView = itemView;
        }
    }
    

    For context, this is a full example, based around ImageView's and Bitmap's. The key bit is the use of the offset in getItemCount() and onBindViewHolder().

    0 讨论(0)
  • 2021-02-20 11:39

    I found a solution myself:

    I extended the LayoutManager:

      class MyLayoutManager extends LinearLayoutManager {
    
        private int mPendingTargetPos = -1;
        private int mPendingPosOffset = -1;
    
        @Override
        public void onLayoutChildren(Recycler recycler, State state) {
            if (mPendingTargetPos != -1 && state.getItemCount() > 0) {
                /*
                Data is present now, we can set the real scroll position
                */
                scrollToPositionWithOffset(mPendingTargetPos, mPendingPosOffset);
                mPendingTargetPos = -1;
                mPendingPosOffset = -1;
            }
            super.onLayoutChildren(recycler, state);
        }
    
        @Override
        public void onRestoreInstanceState(Parcelable state) {
            /*
            May be needed depending on your implementation.
    
            Ignore target start position if InstanceState is available (page existed before already, keep position that user scrolled to)
             */
            mPendingTargetPos = -1;
            mPendingPosOffset = -1;
            super.onRestoreInstanceState(state);
        }
    
        /**
         * Sets a start position that will be used <b>as soon as data is available</b>.
         * May be used if your Adapter starts with itemCount=0 (async data loading) but you need to
         * set the start position already at this time. As soon as itemCount > 0,
         * it will set the scrollPosition, so that given itemPosition is visible.
         * @param position
         * @param offset
         */
        public void setTargetStartPos(int position, int offset) {
            mPendingTargetPos = position;
            mPendingPosOffset = offset;
        }
    }
    

    It stores may target position. If onLayoutChildren is called by RecyclerView, it checks if itemCount is already > 0. If true, it calls scrollToPositionWithOffset().

    So I can tell immediately what position should be visible, but it will not be told to LayoutManager before position exists in Adapter.

    0 讨论(0)
提交回复
热议问题