zoukankan      html  css  js  c++  java
  • 记录RecyclerView的位置并进行恢复

    //监听RecyclerView滚动状态
    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(recyclerView.getLayoutManager() != null) {
                getPositionAndOffset();
            }
        }
    });
     
    /**
     * 记录RecyclerView当前位置
     */
    private void getPositionAndOffset() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        //获取可视的第一个view
        View topView = layoutManager.getChildAt(0);
        if(topView != null) {
            //获取与该view的顶部的偏移量
            lastOffset = topView.getTop();
            //得到该View的数组位置
            lastPosition = layoutManager.getPosition(topView);
        }
    }
     
    /**
     * 让RecyclerView滚动到指定位置
     */
    private void scrollToPosition() {
        if(mRecyclerView.getLayoutManager() != null && lastPosition >= 0) {
            ((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(lastPosition, lastOffset);
        }
    }
    
  • 相关阅读:
    JDBC 精度
    AIX性能监控
    OID View
    Deci and Centi Seconds parsing in java
    SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable
    Linux SNMP oid
    MySQL 监控
    SVN 搭建
    C# 之 继承
    storm单词计数 本地运行
  • 原文地址:https://www.cnblogs.com/coderwjq/p/7360023.html
Copyright © 2011-2022 走看看