Andoird --- RecycleView 恢复上次滚动到的位置

 m_RecyclerListViewContent.addOnScrollListener(new MyScrollListener()); // 滑动监听
 // 恢复记忆位置
 if (CMineManagerModel.getInstance().getMineContentPosition() != null) {
	resetListPosition();
 }



 /**
     * 滑动监听
     */
    public class MyScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            boolean hasStarted = newState == RecyclerView.SCROLL_STATE_DRAGGING; // 此时,RecyclerView滚动开始
            boolean hasEnded = newState == RecyclerView.SCROLL_STATE_IDLE; // 此时,RecyclerView滚动结束
            if (hasEnded && m_LayoutManager != null) {
                View topView = m_LayoutManager.getChildAt(0);
                if (topView == null) {
                    return;
                }
                m_MinePagePositionBean.setLastOffset(topView.getTop());
                m_MinePagePositionBean.setLastPosition(m_LayoutManager.getPosition(topView));
                m_MineContentPosition.put(m_ServiceName, m_MinePagePositionBean);

                Log.d(TAG, "MyScrollListener lastOffset : " + m_MinePagePositionBean.getLastOffset() + ", lastPosition : " +  m_MinePagePositionBean.getLastPosition() + " m_ServiceName : " + m_ServiceName);
                CMineManagerModel.getInstance().setMineContentPosition(m_MineContentPosition);
            }
        }
    }

  private static class PositionRunnable implements Runnable {
        private final int lastPosition;
        private final int lastOffset;
        private WeakReference<CMineContentFragment> cMineContentFragmentWeakReference;
        public PositionRunnable(CMineContentFragment cMineContentFragment, int lastPosition, int lastOffset){
           cMineContentFragmentWeakReference = new WeakReference<>(cMineContentFragment);
           this.lastPosition = lastPosition;
           this.lastOffset = lastOffset;
        }
        @Override
        public void run() {
            CMineContentFragment cMineContentFragment = cMineContentFragmentWeakReference.get();
            if(cMineContentFragment != null) {
                ((LinearLayoutManager) cMineContentFragment.m_LayoutManager).scrollToPositionWithOffset(lastPosition, lastOffset);
            }
        }
    }

    private void resetListPosition() {
        Log.d(TAG, "resetListPosition");
        Log.d(TAG, "resetListPosition lastPosition to" + m_MineContentPosition.keySet());
        if (m_MineContentPosition != null) {
            if (m_MineContentPosition.get(m_ServiceName) != null) {
                int lastOffset = m_MineContentPosition.get(m_ServiceName).getLastOffset();
                int lastPosition = m_MineContentPosition.get(m_ServiceName).getLastPosition();
                if (m_LayoutManager != null) {
                    Log.d(TAG, "resetListPosition lastPosition to" + lastPosition);
                    Log.d(TAG, "resetListPosition lastOffset to" + lastOffset);
                    PositionRunnable runnable = new PositionRunnable(this, lastPosition, lastOffset);
                    m_RecyclerListViewContent.post(runnable);
                }
            } else {
                Log.d(TAG, "resetListPosition position.get(m_ServiceName) is null");
            }
        } else {
            Log.d(TAG, "resetListPosition position is null");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁同学与Android

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值