Android的 - 自定义动态生成的化合物,在查看重新RecyclerView,导致业绩不佳 [英] Android - Custom dynamically-generated compound View recreated in RecyclerView, causing poor performance

查看:369
本文介绍了Android的 - 自定义动态生成的化合物,在查看重新RecyclerView,导致业绩不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的 CompoundView 延伸的LinearLayout 显示的项目 RecyclerView 。每个项目显示,其中包含多个段落和图像的文章。

CompoundView 补充的TextView 的ImageView 动态基础对连接数据CompoundView.setData(列表< D​​ataPiece>件),连接数据之前其数量是未知的。

每个 DataPiece 对象告诉 CompoundView 不管它是一段文字或图像。这里是code为 CompoundView.setData(列表< D​​ataPiece>件)

I'm using a custom CompoundView which extends LinearLayout to display items of a RecyclerView. Each item displays an article which contains multiple paragraphs and images.

The CompoundView adds TextView or ImageView dynamically based on the data attached by CompoundView.setData(List<DataPiece> pieces), the number of which is unknown before data is attached.

Each DataPiece object tells CompoundView whether it's a piece of text or an image. And here is the code for CompoundView.setData(List<DataPiece> pieces):

public void setData(List<DataPiece> pieces) {
    removeAllViews();
    for (DataPiece dataPiece : pieces) {
        switch (dataPiece.getType()) {
            case IMAGE:
                ImageView imageView = new ImageView(getContext());
                ...
                addView(imageView);
                break;
            case TEXT:
                TextView textView = new TextView(getContext());
                ...
                addView(textView);
                break;
        }
    }
}

RecyclerView.Adapter.onBindViewHolder(),数据是通过调用连接到 CompoundView MyViewHolder.compoundView.setData(...)。和 RecyclerView 创建时工作正常。


但是,对于具有多个 A CompoundView 项目的ImageView 的TextView S,当我滚动远离它,然后回滚动,滚动变得严重不畅。


我想这是因为 removeAllViews()使用setData()被调用,而 CompoundView for循环创建是再次被回收执行。但我不知道如何避免这一点。


而且我也想知道为什么滚动在 RecyclerView 即使它的回收也使用的TextView (含图片)时,总是一帆风顺


在此先感谢!

In the RecyclerView.Adapter.onBindViewHolder(), the data is attached to CompoundView by calling MyViewHolder.compoundView.setData(...). And it works fine when the RecyclerView is created.

However, for a CompoundView item with multiple ImageViews and TextViews, when I scroll away from it and then scroll back, the scroll becomes heavily unsmooth.

I guess it's because removeAllViews() in setData() is called, and the CompoundView creation for-loop is executed again by the recycler. But I don't know how to avoid this.

And I also wonder why the scroll is always smooth when using TextView(with Images) in a RecyclerView even it's recycled too.

Thanks in advance!

推荐答案

有多重考虑,可以进入决定什么最好的办法也许是。

There are multiple considerations that could go into deciding what the best approach might be.

第一,你对在回收的列表项的最大数量的想法?如果只是少数,也许你可以沟 RecyclerView 的办法,只是增加你的 CompundView 成主持的容器一个滚动型

First, do you have an idea about the maximum number of items in the recycler's list? If it is just a handful, maybe you could ditch the RecyclerView approach and just add your CompundView into a container hosted by a ScrollView.

其次 - 是每个项目的布局相当复杂(又名已经有好些 TextViews ImageViews 等。它)?如果是的话,也许你可以采取将类似于一个 ExpandableListView 的方法 - 显示摘要的每个列表项,扩大到上点击该项目的完整布局。

Secondly - is the layout of each item fairly complicated (a.k.a. are there many TextViews, ImageViews etc. in it)? If yes, maybe you could take an approach that would resemble an ExpandableListView - show a summary as each list item and expand to the full layout of the item on click.

第三 - 如果以上都不是可以接受的,你还想去目前的做法 - 不建/在装订方法增加您的看法。这样做在 onCreateViewHolder ,当系统希望你来构建你的看法(我不知道肯定,但通过你呼吁时间 onBindViewHolder 你的看法可能已经被添加到该层次结构的任何层次的变化在其容器涟漪效应 - 但不要把我的话,我真的不知道鉴于已经补充说,这只是一个假设)。你将不得不在每个项目分配不同的类型,所以在 onCreateViewHolder ,你可以配合支持数据视图类型(添加子视图的相应数量的) ;从头开始创建视图每一次 - 这样你就不需要在 removeAllViews 调用。喜欢的东西(我离开了不相关的情况适配器的部分):

Thirdly - if none of the above is acceptable and you still want to go the current approach - don't construct/add your view in the binding method. Do it in the onCreateViewHolder, when the system expects you to construct your view (I don't know for sure but by the time you're called on onBindViewHolder your view might have been already added to the hierarchy and any hierarchical change to it has a ripple effect on its containers - but don't take my word for it, I don't actually know the view is already added, it is just an assumption). You will have to assign each item a different type, so that in onCreateViewHolder you could match the view type with the supporting data (for the addition of the corresponding number of child views); create the view from scratch each time - this way you don't need to call on removeAllViews. Something like(I left out parts of the adapter that are not relevant to the case):

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<DataPiecesList> mItems;
    public RecyclerViewAdapter(ArrayList<DataPiecesList> items) {
        mItems = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CompoundView compoundView = new CompoundView();
        List<DataPiece> dataPieces = mItems.get(viewType);
        for (int i = 0; i < dataPieces.size(); i++)
        {
            // construct TextView or ImageView or whatever
            compoundView.add(child);
        }
        MyViewHolder view = new MyViewHolder(compoundView);
        return view;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        CompoundView compoundView = viewHolder.itemView;
        DataPiece dataPiece = mItems.get(i);
        for (int j = 0; j < compoundView.getChildCount(); j++)
        {
            compoundView.getChildAt(j) <- dataPiece.get(j);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ...
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }
}

这篇关于Android的 - 自定义动态生成的化合物,在查看重新RecyclerView,导致业绩不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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