android – 如何将Horizo​​ntal Recyclerview添加为Vertical Recyclerview的标题?

Expected output image
我需要这样的输出.这是Horizo​​ntalRecyclerView作为VerticalRecyclerView的标头.

目前我正在使用NestedScrollView来实现这种设计.但我觉得滚动不顺畅.

请提出任何想法.提前致谢.

布局:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nsv_my_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="ifContentScrolls"
    android:visibility="gone"
    android:clipChildren="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="15dp">

        <LinearLayout
            android:id="@+id/ll_continue_watching"
            android:layout_width="match_parent"
            android:layout_height="132dp"
            android:visibility="gone"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="29dp"
                android:gravity="center_vertical"
                android:paddingLeft="15dp"
                android:paddingRight="15dp">

                <customview.font.TextView
                    android:id="@+id/txt_edit_remove_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/btn_profile_edit"
                    android:gravity="center"
                    android:paddingTop="2dp"
                    android:text="EDIT"
                    android:textSize="10sp" />

                <customview.font.TextView
                    android:id="@+id/txt_continue_watchng"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:text="Continue watching"
                    android:textSize="12sp" />

            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@color/horizontal_view_color" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_horizontal_view"
                android:layout_width="match_parent"
                android:layout_height="102dp"
                android:paddingBottom="15dp"
                android:paddingTop="15dp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@color/horizontal_view_color" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_vertical_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
最佳答案
很简单,你的“主要”回收者视图有两种细胞,第一种是另一种回收者视图,其余是“正常”细胞.

在“主”回收器视图的适配器上,您必须覆盖方法getItemViewType,如下所示:

 @Override
    public int getItemViewType(int position) {
        if(postition == 0) {
            return 1; //Let's say that you define that 1 is the view type for the recycler view cell
        }
        else {
           return 2; //And 2 is going to be the "normal" cells
        }
    }

然后,在onCreateViewHolder方法上,根据视图类型,您必须构建并返回正确的viewHolder,如下所示:

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        if(viewType == 1) {
             View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.your_other_recyclerview_layout, parent, false);
        //build the viewHolder
        ViewHolder vh = new YourOtherRecyclerViewHolder(v);
        return vh;
        }
        else {
            View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.your_normal_cell_layout, parent, false);
            //build the normal viewHolder
            YourNormalViewHolder vh = new YourNormalViewHolder (v);
            return vh;
        }

    }

当然,您必须实现2个扩展ViewHolder类的类,一个用于YourOtherRecyclerViewHolder,另一个用于YourNormalViewHolder.

我希望它有所帮助!

转载注明原文:android – 如何将Horizo​​ntal Recyclerview添加为Vertical Recyclerview的标题? - 代码日志