Recyclerview内的Recyclerview不能滚动顺畅

时间:2016-07-27 10:13:17

标签: android android-recyclerview

我有一个垂直的Recyclerview(父级)里面有三行。每行为RecyclerView(child1),RecyclerView(child2),RecyclerView(child3),每个行都有不同类型的layoutmanager,如child1,有gridLayout manager,child2& 3有Vertical LinearLayout管理器。

因此,当我在行中设置它们时,每一行都有不同类型的适配器。但是当我滚动recyclerview时出现问题。它不是平滑的滚动,它也需要700ms到可见的第二行。

我的row_list_item布局是:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/ll_main_block"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingStart="10dp"
            android:paddingRight="10dp"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="12dp"
            android:paddingEnd="10dp"
            android:gravity="center_vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/tv_heading"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.8"
                android:gravity="center_vertical"
                android:text=""
                android:textColor="@color/dark_grey"
                android:textSize="@dimen/midium_text_size" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:id="@+id/linearLayout_viewall"
                android:visibility="gone"
                android:gravity="center_vertical|end"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_more"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:drawablePadding="5dp"
                    android:gravity="center_vertical|end"
                    android:text="@string/view_all"
                    android:textSize="@dimen/small_text_size" />

                <ImageView
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:contentDescription="@null"
                    android:src="@drawable/next" />
            </LinearLayout>
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_category_outlet"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</FrameLayout>

我正在使用multiType:

放置一些主适配器代码
@Override
public int getItemCount() {
    int size = 4;
    if (isErrorView) {
        size = 1;
    }
    return size;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;

    if (viewType == TYPE_NEARBY
            || viewType == TYPE_PEOPLE || viewType == TYPE_DEFAULT || viewType == TYPE_SERVICES) {

        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_discover_adapter, parent, false);
        RecyclerView.ViewHolder timelineViewHolder = new vHolderEverything(view);
        return timelineViewHolder;

    } else if (viewType == TYPE_ERROR) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.inflator_error_listview, parent, false);
        vHolderError timelineViewHolder = new vHolderError(view);
        return timelineViewHolder;
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    String headingText = "";
    boolean isMainBlockInvisible = false;
    vHolderEverything mVHolder;
    switch (getItemViewType(position)) {
        case TYPE_ERROR:
            break;
        case 1:
            mVHolder = (vHolderEverything) holder;
            try {
                GridLayoutManager lLayout = new GridLayoutManager(curr_activity, 2);
                mVHolder.rv_category_outlet.setNestedScrollingEnabled(false);
                mVHolder.rv_category_outlet.setHasFixedSize(true);

                int spanCount = 4; // 3 columns
                int spacing = 4; // 50px
                boolean includeEdge = false;

                mVHolder.rv_category_outlet.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
                mVHolder.rv_category_outlet.setLayoutManager(lLayout);

                if (dataForDiscovery != null) {
                    try {

                        if (position == TYPE_SERVICES) {
                            headingText = curr_activity.getResources().getString(R.string.popular_Service);

                            mVHolder.adapterObject_services.clear();
                            mVHolder.adapterObject_services.addAll(dataForDiscovery.getData().getPopularServices());
                            if (mVHolder.adapterObject_services != null && mVHolder.adapterObject_services.size() > 0) {
                               mVHolder.rv_category_outlet.setAdapter(mVHolder.discoveryPopularListAdapter);
                            } else {
                                isMainBlockInvisible = true;
                            }
                        } else {
                            isMainBlockInvisible = true;

                        }

                    } catch (Exception e) {
                        isMainBlockInvisible = true;
                    }
                } else {
                    isMainBlockInvisible = true;
                }

            } catch (Exception e) {
                isMainBlockInvisible = true;
            }

            if (isMainBlockInvisible) {
                mVHolder.ll_main_block.setVisibility(View.GONE);
            } else {
                ((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.GONE);
                mVHolder.tv_heading.setText(headingText);
                mVHolder.tv_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Activity_Search.search_initiate_view_pager.setCurrentItem(position);
                    }
                });
            }
            break;
        case 2:

            mVHolder = (vHolderEverything) holder;
            try {
                LinearLayoutManager layoutManager = new org.solovyev.android.views.llm.LinearLayoutManager(curr_activity, LinearLayoutManager.VERTICAL, false);
                mVHolder.rv_category_outlet.setLayoutManager(layoutManager);
                mVHolder.rv_category_outlet.setHasFixedSize(true);

                if (dataForDiscovery != null) {
                    try {
                        if (position == TYPE_PEOPLE) {

                            mVHolder.adapterObject.clear();
                            mVHolder.adapterObject.addAll(dataForDiscovery.getData().getPeople());
                            headingText = curr_activity.getResources().getString(R.string.follow_people_discover);
                            if (mVHolder.adapterObject != null && mVHolder.adapterObject.size() > 0) {
                                mVHolder.rv_category_outlet.setAdapter(mVHolder.adapter);
                            } else {
                                isMainBlockInvisible = true;
                            }
                        } else {
                            isMainBlockInvisible = true;

                        }

                    } catch (Exception e) {
                        isMainBlockInvisible = true;
                    }
                } else {
                    isMainBlockInvisible = true;
                }

            } catch (Exception e) {
                isMainBlockInvisible = true;
            }

            if (isMainBlockInvisible) {
                mVHolder.ll_main_block.setVisibility(View.GONE);
            } else {
                // if (headingText.equalsIgnoreCase("Services")) {
                ((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.GONE);
                // } else {
                //   ((vHolderEverything) holder).linearLayout_viewall.setVisibility(View.VISIBLE);
                //}
                mVHolder.tv_heading.setText(headingText);
                mVHolder.tv_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Activity_Search.search_initiate_view_pager.setCurrentItem(position);
                    }
                });
            }
            break;
        case TYPE_DEFAULT:
            mVHolder = (vHolderEverything) holder;
            mVHolder.ll_main_block.setVisibility(View.GONE);
            break;
        default:
            mVHolder = (vHolderEverything) holder;
            mVHolder.ll_main_block.setVisibility(View.GONE);

            break;
    }

}

public static class vHolderEverything extends RecyclerView.ViewHolder {
    TextView tv_heading;
    TextView tv_more;
    RecyclerView rv_category_outlet;
    LinearLayout ll_main_block, linearLayout_viewall;
    ArrayList<ReusableServices> adapterObject_services= new ArrayList<ReusableServices>();
    DiscoveryPopularListAdapter discoveryPopularListAdapter = null;
    RecyclerView.Adapter adapter = null;
    ArrayList<FollowPeopleSuggestionData> adapterObject= new ArrayList<FollowPeopleSuggestionData>();

    public vHolderEverything(View itemView) {
        super(itemView);

        this.tv_heading = (TextView) itemView.findViewById(R.id.tv_heading);
        this.tv_more = (TextView) itemView.findViewById(R.id.tv_more);
        this.rv_category_outlet = (RecyclerView) itemView.findViewById(R.id.rv_category_outlet);
        this.ll_main_block = (LinearLayout) itemView.findViewById(R.id.ll_main_block);
        this.linearLayout_viewall = (LinearLayout) itemView.findViewById(R.id.linearLayout_viewall);
        MazkaraApp.getInstance().applyTypefaceItalic(tv_more);
        MazkaraApp.getInstance().applyTypefaceBlack(tv_heading);


        try {
            if (adapterObject_services != null)
                discoveryPopularListAdapter = new DiscoveryPopularListAdapter(curr_activity, adapterObject_services);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (adapterObject != null)
                adapter = new AdapterRecycler_Search_Users(curr_activity, curr_activity, adapterObject);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

只有在第一次发生时才会发生。第二次它正常工作。

2 个答案:

答案 0 :(得分:0)

将各自的子视图适配器设置为关联的recyclerViews时,请将其嵌套滚动设置为false。这是一段摘录:

{
childRecyclerView1.setAdapter(childAdapter1);
childRecyclerView1.setNestedScrollingEnabled(false);
}

这应该平滑卷轴。

答案 1 :(得分:0)