与回收视图内动态视图的问题 [英] Problems with dynamic views inside recycler view

查看:221
本文介绍了与回收视图内动态视图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用回收站视图来显示产品清单,其中包含图像的网格布局。网格布局动态添加到列表项内的回收观点适配器onBindViewHolder的方法。现在的问题是,在网格布局视图每次滚动重新创建。我不希望这些意见要在滚动重建。如何处理它?

这里是code段

  @覆盖
    公共无效onBindViewHolder(最终PersonViewHolder personViewHolder,最终int i)以{                网格布局feedGrid =新的网格布局(背景);
                LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(dipToPixels(context,HomePage.SCREEN_WIDTH),dipToPixels(context,HomePage.SCREEN_WIDTH));
                feedGrid.setLayoutParams(的LayoutParams);
                feedGrid.setColumnCount(1);                imgArr =新ImageView的[NUM]
                imgArr [0] =新ImageView的(背景);
                GridLayout.Spec行= GridLayout.spec(0,1);
                GridLayout.Spec列跨度= GridLayout.spec(0,1);
                GridLayout.LayoutParams gridLayoutParam =新GridLayout.LayoutParams(行,合并单元格);
                gridLayoutParam.setGravity(Gravity.FILL);                    Picasso.with(上下文).load(urlArr.get(0))。误差(mDrawable).placeholder(mDrawable).into(imgArr [0],新回调(){
                        @覆盖
                        公共无效的onSuccess(){
                            personViewHolder.feedGridLayout.setVisibility(View.VISIBLE);
                            personViewHolder.loadImg.setVisibility(View.GONE);
                        }                        @覆盖
                        公共无效onerror的(){
                            personViewHolder.feedGridLayout.setVisibility(View.GONE);
                            personViewHolder.loadImg.setVisibility(View.VISIBLE);                        }                    });                feedGrid.addView(imgArr [0],gridLayoutParam);                personViewHolder.feedGridLayout.removeAllViews();                personViewHolder.feedGridLayout.addView(feedGrid);}


解决方案

这是我如何解决一个非常类似的<一个href=\"http://stackoverflow.com/questions/31164051/recyclerview-with-multiple-dynamic-views-being-inflated-inside-row/31178567\">problem.

在您的适配器您覆盖onViewRecycled并删除网格布局的意见有,而不是onBindViewHolder。

  @覆盖
    公共无效onViewRecycled(PersonViewHolder personViewHolder){
        super.onViewRecycled(personViewHolder);
        personViewHolder.feedGridLayout.removeAllViews();
    }

删除 personViewHolder.feedGridLayout.removeAllViews(); 从onBindViewHolder

让我知道能否解决。

I'm using recycler view to show list of items which contains grid layout of images. The grid layout is added dynamically to the list item inside "onBindViewHolder" method on recycler view adapter. Now problem is that the grid layout views are recreated on every scroll. I don't want those views to be recreated on scroll. How to deal with it??

Here is the code snippet

 @Override
    public void onBindViewHolder(final PersonViewHolder personViewHolder, final int i) {

                GridLayout feedGrid = new GridLayout(context);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dipToPixels(context,HomePage.SCREEN_WIDTH),dipToPixels(context,HomePage.SCREEN_WIDTH));
                feedGrid.setLayoutParams(layoutParams);
                feedGrid.setColumnCount(1);

                imgArr = new ImageView[num];
                imgArr[0] = new ImageView(context);
                GridLayout.Spec row = GridLayout.spec(0, 1);
                GridLayout.Spec colspan = GridLayout.spec(0, 1);
                GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row, colspan);
                gridLayoutParam.setGravity(Gravity.FILL);

                    Picasso.with(context).load(urlArr.get(0)).error(mDrawable).placeholder(mDrawable).into(imgArr[0], new Callback() {
                        @Override
                        public void onSuccess() {
                            personViewHolder.feedGridLayout.setVisibility(View.VISIBLE);
                            personViewHolder.loadImg.setVisibility(View.GONE);
                        }

                        @Override
                        public void onError() {
                            personViewHolder.feedGridLayout.setVisibility(View.GONE);
                            personViewHolder.loadImg.setVisibility(View.VISIBLE);

                        }

                    });



                feedGrid.addView(imgArr[0], gridLayoutParam);

                personViewHolder.feedGridLayout.removeAllViews();

                personViewHolder.feedGridLayout.addView(feedGrid);

}

解决方案

This is how I solved a very similar problem.

In your adapter you override onViewRecycled and remove the views of your gridLayout there instead of onBindViewHolder.

@Override
    public void onViewRecycled(PersonViewHolder personViewHolder) {
        super.onViewRecycled(personViewHolder);
        personViewHolder.feedGridLayout.removeAllViews();
    }

Remove personViewHolder.feedGridLayout.removeAllViews(); from your onBindViewHolder.

Let me know if that fixes it.

这篇关于与回收视图内动态视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆