在NestedScrollView中使用RecyclerView时性能不佳 [英] Bad performance when use RecyclerView inside NestedScrollView

查看:199
本文介绍了在NestedScrollView中使用RecyclerView时性能不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发搜索联系人功能,在该屏幕上, NestedScrolView ( fillViewport = true )内有一个 RecyclerView . 屏幕设计:(此设计已被客户接受,我无法更改)

将当前设备的所有联系人加载到ArrayList中后,将从该数组中过滤搜索结果.
有几种情况使应用程序非常不方便:
1.当用户键入没有结果的输入时,然后用户进行清晰搜索,我必须再次显示所有结果. NestedScrollView 必须为 RecyclerView 的所有项目(例如:300个项目)呈现UI.
2.结果的数量有很多变化时(例如,从1到300个项目). NestedScrollView 必须为许多 RecyclerView

I'm developing a search contact feature, in that screen, there is a RecyclerView inside NestedScrolView (fillViewport = true). Screen design: (This design is accepted by customer, I can't change it)

After loading all contacts of current device into an ArrayList, the search results are filtered from this array.
There is several cases that make the app very laggy:
1. When user type an input that have no result, then user clear search, I have to show all results again. The NestedScrollView has to render UI for all items of RecyclerView (for example: 300 items).
2. When the quantity of results has many changes (for example, from 1 to 300 items). The NestedScrollView has to render UI for a lot of items of RecyclerView

我知道此设计破坏了RecyclerView的回收技术,但我无法更改.
我尝试过的:

I know this design breaks recycling technique of RecyclerView, but I can't change it.
What I tried:

recyclerView.setNestedScrollingEnabled(false);

在AndroidManifest中:

In AndroidManifest:

android:windowSoftInputMode="adjustNothing"

适配器:

public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> {

    private List<MobileContact> contacts;
    private Context context;

    public RecyclerContactAdapter() {
        contacts = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_recycler_contact, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //set data for view
    }

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

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAlphabetHeader;
        private CircleImageView civAvatar;
        private TextView tvContactName;
        private TextView tvStatus;
        private CheckBox cbInvited;
        private RelativeLayout rlAlphabetHeader;
        private RelativeLayout rlContainer;

        protected ViewHolder(View itemView) {
            super(itemView);
            tvAlphabetHeader = itemView.findViewById(R.id.item_recycler_contact_tv_alphabet_header);
            civAvatar = itemView.findViewById(R.id.item_recycler_contact_civ_avatar);
            tvContactName = itemView.findViewById(R.id.item_recycler_contact_tv_name);
            tvStatus = itemView.findViewById(R.id.item_recycler_contact_tv_status);
            cbInvited = itemView.findViewById(R.id.item_recycler_contact_cb_contact);
            rlAlphabetHeader =  itemView.findViewById(R.id.item_recycler_contact_rl_alphabet);
            rlContainer = itemView.findViewById(R.id.item_recycler_contact_rl_contact);
        }
    }

    public void addAll(List<MobileContact> mobileContacts) {
        this.contacts.clear();
        this.contacts.addAll(mobileContacts);
        notifyDataSetChanged();
    }

    public void add(MobileContact mobileContact) {
        this.contacts.add(mobileContact);
    }

    public List<MobileContact> getContacts() {
        return this.contacts;
    }

}

推荐答案

您使用的RecyclerView错误.与其将RecyclerView放在NestedScrollView中,还不如将您的页眉"和搜索框"放在RecyclerView中,作为不同的视图类型.

You are using RecyclerView incorrectly. Instead of putting your RecyclerView inside NestedScrollView put your "Header" and "Search box" inside RecyclerView as different view types.

这个答案是一个很好的例子.

This answer is a good example.

这篇关于在NestedScrollView中使用RecyclerView时性能不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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