判断RecyclerView的条目是否满一个屏幕

判断RecyclerView的条目是否满一个屏幕

      • 问题
      • 代码实例

问题

经常我们在变成过程中会遇到如何判断RecyclerView条目是否满屏幕的判断,比如搜索框,满屏下滑不显示,滑回来再显示等。
可参考如下代码进行实现。

代码实例

/**
 * Desc : 验证RecyclerView是否满一屏幕
 * 
 */
public class RecyclerViewTestActivity extends AppCompatActivity{
 
    private LinearLayoutManager mLinearLayoutManager;
    private SimpleTextAdapter mSimpleTextAdapter;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview);
        final RecyclerView rv = findViewById(R.id.rv);
 
        mSimpleTextAdapter = new SimpleTextAdapter(null);
        rv.setAdapter(mSimpleTextAdapter);
        mLinearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(mLinearLayoutManager);
        //下面代码是重点
        rv.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int lastCompletelyVisibleItemPosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
                        Log.e("test", "lastCompletelyVisibleItemPosition is : " + lastCompletelyVisibleItemPosition);
                        boolean b = lastCompletelyVisibleItemPosition < mSimpleTextAdapter.getItemCount() - 1;
                        if (b){
                            Log.e("test" ,"超过一屏幕,移除啦");
                            rv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }else {
                            Log.e("test" ,"没有超出超过一屏幕,继续请求");
                            mSimpleTextAdapter.addData(6);
                        }
                    }
                });
    }
}
        mView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            //用来标记是否正在向最后一个滑动
            boolean isSlidingToLast = false;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager  manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    //此方法常用作判断是否能下拉刷新,来解决滑动冲突
                    int findFirstCompletelyVisibleItemPosition = ((LinearLayoutManager)manager).findFirstCompletelyVisibleItemPosition();
//最后一个完整的可见的item位置
                    int findLastCompletelyVisibleItemPosition =  ((LinearLayoutManager)manager).findLastCompletelyVisibleItemPosition();
//最后一个可见的位置
                    int findLastVisibleItemPosition =  ((LinearLayoutManager)manager).findLastVisibleItemPosition();
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();
                    // 判断是否滚动到底部,并且是向右滚动
                    LinearLayoutManager llm = (LinearLayoutManager) mView.getLayoutManager();
                    if (! isSlidingToLast){

                        llm.scrollToPositionWithOffset(findLastVisibleItemPosition-1, 0);
                        myAdapter.notifyItemChanged(findLastVisibleItemPosition-1);
                    }else {
                        llm.scrollToPositionWithOffset(findLastVisibleItemPosition, 0);
                        myAdapter.notifyItemChanged(findLastVisibleItemPosition);
                    }
                    if (lastVisibleItem == (totalItemCount - 1) && isSlidingToLast) {
                        //加载更多功能的代码
                    }
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //dx用来判断横向滑动方向,dy用来判断纵向滑动方向
                if (dx > 0) {
                    //大于0表示正在向右滚动
                    isSlidingToLast = true;
                } else {
                    //小于等于0表示停止或向左滚动
                    isSlidingToLast = false;
                }
            }
        });

你可能感兴趣的:(学习交流,android,android,studio,java,kotlin,adb)