【问题标题】:how to add Horizontal Recyclerview as a header for Vertical Recyclerview?如何将 Horizo​​ntal Recyclerview 添加为 Vertical Recyclerview 的标题?
【发布时间】:2016-11-16 08:48:35
【问题描述】:

我需要这样的输出。即 Horizo​​ntalRecyclerView 作为 VerticalRecyclerView 的 header。

目前我正在使用 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>

【问题讨论】:

  • 你能解释一下为什么你使用 RecyclerView 在顶部进行水平滚动吗?也许使用 TabLayout 就足够了? (developer.android.com/reference/android/support/design/widget/…)
  • 对于可滚动标签,请参阅stackoverflow.com/questions/35815615/…
  • 使用 TabHost 或 TabLayout 来实现这一点。但不要使用 RecyclerView 来实现这一点。
  • @BrunoBieri 因为作为用户,我可以从列表中删除项目。然后我可以立即撤消删除。所以我更喜欢这里的RecyclerView。
  • 不建议使用 NestedScrollView 作为 RecyclerView 的父级,因为你的列表会自动填充父级并加​​载 RecyclerView 的所有项目,结果导致列表变慢。

标签: android android-recyclerview


【解决方案1】:

非常简单,您的“主要”回收站视图有两种单元格,第一种是另一个回收站视图,其余的是“正常”单元格。

在“主”回收器视图的适配器上,您必须重写 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。

希望对你有帮助!

【讨论】:

  • 有这个想法......但我需要对此进行一些澄清。 * 我可以在这个概念中应用 recyclerview 及其适配器吗?
  • 我目前无权访问任何与此类似的代码,但您有一些包含完整示例的文档:guides.codepath.com/android/…
  • 您唯一要做的就是将图像单元替换为回收器视图单元,但概念是相同的。我希望它有所帮助。
猜你喜欢
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多