android RecyclerView瀑布流错乱 顶部留白的解决方案

RecyclerView瀑布流错乱 顶部留白的解决方案

解决方案方案分为三步:

  1. layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE)

  2. 实际上解决错乱与留白的问题 主要原因在于加载更多数据之后 使用的是notifyDataSetChanged()该方法将刷新一整个Recycleview的数据;所以解决方案将 notifyDataSetChanged()替换成 notifyItemRangeInserted(int positionStart, int itemCount)进行新增数据局部刷新。

  3. 根据图片宽高设置布局ImageView的LayoutParams

ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (width != -1 && height != -1) {
            layoutParams.height = viewWidth * height / width;
            layoutParams.width = viewWidth;
        } else {
            layoutParams.height = viewWidth;
            layoutParams.width = viewWidth;
        }
        simpleDraweeView.setLayoutParams(layoutParams);

解决顶部留白:

reGemeType.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //防止第一行到顶部有空白区域
                mWrapContentGridLayoutManager.invalidateSpanAssignments();
                
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });

你可能感兴趣的:(Android基础)