gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:00 24 4
gpt4 key购买 nike

Expected output image我需要这样的输出。这是 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>

最佳答案

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

在“主”回收器 View 的适配器上,您必须覆盖方法 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 方法上,根据 View 类型,您必须构建并返回正确的 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 的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40627569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com