admin 发表于 2017-6-29 10:52:46

【转】RecyclerView实现瀑布流遇到的各种问题

来源 :http://blog.csdn.net/windows771053651/article/details/51596744

功能:图片瀑布流问题1:如何保持已显示过的imageview的尺寸解决方法:定义一个HashMap<Integer, Float> indexMap = new HashMap<Integer, Float>();用来保存已显示过的ImageView尺寸,显示时直接取其比例即可代码:onBindItemView(),调用resizeItemView(itemViewHolder.frontCoverImage, getScaleType(position));
private float getScaleType(int position) {
            if (!indexMap.containsKey(position)) {
                        float scaleType;
                        if (hasHeader()) {
                                if (position == 1) {
                                        scaleType = SIZE_SCALE_01;
                                } else if (position == 2) {
                                        scaleType = SIZE_SCALE_02;
                                } else {
                                        scaleType = Utils.getRandomInt() % 2 == 0 ? SIZE_SCALE_01 : SIZE_SCALE_02;
                                }
                        } else {
                                if (position == 0) {
                                        scaleType = SIZE_SCALE_01;
                                } else if (position == 1) {
                                        scaleType = SIZE_SCALE_02;
                                } else {
                                        scaleType = Utils.getRandomInt() % 2 == 0 ? SIZE_SCALE_01 : SIZE_SCALE_02;
                                }
                        }
                        indexMap.put(position, scaleType);
                }
           
            return indexMap.get(position);
    }

        private void resizeItemView(ImageView frontCoverImage, float scaleType) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) frontCoverImage.getLayoutParams();
                params.width = screenWidth / 2;
                params.height = (int) (params.width / scaleType) - Utils.dp2px(context, 8);
                frontCoverImage.setLayoutParams(params);
        }<pre name="code" class="java">private void resizeItemView(ImageView frontCoverImage, float scaleType) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) frontCoverImage.getLayoutParams();
                params.width = screenWidth / 2;
                params.height = (int) (params.width / scaleType) - Utils.dp2px(context, 8);
                frontCoverImage.setLayoutParams(params);
        }以上解决了滑动过程中的item移动,但是RecyclerView滑动到顶部时仍会出现移动问题,这是由于item重用,并且要保持满屏造成的问题2:RecyclerView滑动到顶部时仍会出现移动问题解决方法:
final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                //RecyclerView滑动过程中不断请求layout的Request,不断调整item见的间隙,并且是在item尺寸显示前预处理,因此解决RecyclerView滑动到顶部时仍会出现移动问题
                layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
      mRecyclerView.setLayoutManager(layoutManager);
      mRecyclerView.addItemDecoration(new DividerGridItemDecoration(getContext()));
      mRecyclerView.setPadding(0, 0, 0, 0);
      mRecyclerView.addOnScrollListener(new OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                super.onScrollStateChanged(recyclerView, newState);
                                layoutManager.invalidateSpanAssignments();
                        }
                });以上解决了item移动的各种问题问题3:瀑布流加载更多用notifyDataSetChanged()刷新图片闪烁原因:notifyDataSetChanged()会导致整个itemview刷新,已经测试:相同position刷新其itemview是不同的对象,例如,刷新后,position为12的用了position为13的itemview,再次刷新时,又用了position为10的itemview,这样次position上对应的itemview的ImageView就会在重设size时发生闪烁,此现象是可以用肉眼看到的。解决方法:用notifyItemRangeInserted()进行局部刷新,这样原先的itemview就不会重绘
页: [1]
查看完整版本: 【转】RecyclerView实现瀑布流遇到的各种问题