Android嵌套的scrollview中包含Recycler视图会导致渲染延迟

时间:2017-06-22 06:23:21

标签: android android-recyclerview android-nestedscrollview

我创建了一个布局,其中我使用带有视图的循环器和嵌套的scrollview中的其他几个视图。我理解的是,如果我将循环器视图放在嵌套的scrollview中,那么渲染列表项需要时间。当我检查内部适配器时,它会多次调用onBindholder。如果我删除嵌套的滚动视图,它工作正常。代码如下:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <include
        --- ToolBar View ---/>
    <include
         --- Search View ---/>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            --- Some views --- 

                <android.support.v7.widget.RecyclerView 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_marginLeft="@dimen/margin_5"
                    android:layout_marginRight="@dimen/margin_5"
                    android:splitMotionEvents="false"/>
            </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

我的适配器看起来像:

    package sample.example.adapters;

public class CustomAdapter extends RecyclerView.Adapter  {

    private LayoutInflater mInflater;
    private List<Data> list;
    private  ItemClickListener itemClickListener;


    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public PopularConditionAdapter(Context context , List<Data> list) {
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.row_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        Data condition = list.get(position);
        if(condition != null && condition.getConditionTitle() != null) {
            viewHolder.conditionName.setText(condition.getConditionTitle());
            viewHolder.conditionNameInitial.setText(Tools.getUserInitial(condition.getConditionTitle()));
        }

    }

    @Override
    public int getItemCount() {
        if(popularConditionList !=null)
        {
            return popularConditionList.size();
        }else{
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
         public  View popularParentView;
        public TextView conditionNameInitial;
        public  TextView conditionName;

        public ViewHolder(View itemView) {
            super(itemView);
            conditionNameInitial = (TextView) itemView.findViewById(R.id.some_id);
            conditionName = (TextView) itemView.findViewById(R.id.test_id);
            popularParentView = itemView.findViewById(R.id.round_id);
            popularParentView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, list.get(getPosition()));
            }
        }
    }

    public interface ItemClickListener {
        void onItemClick(View view, Data condition);
    }

    public void reloadList(List<Data> dataList){
        list.clear();
        if(dataList!=null){
            Iterator<Data> iterator = dataList.iterator();
            while (iterator.hasNext()){
                List.add(iterator.next());
            }
        }
    }
}

我也打电话给notifyDataSetChanged。需要一些帮助来确定我正在做的错误。

1 个答案:

答案 0 :(得分:-1)

RecyclerView有自己的NestedScrollView所以我们需要使用此代码禁用该NestedScroll。

 mRecyclerView.setNestedScrollingEnabled(false);