RecyclerView与GridView控件 [英] RecyclerView with GridView
本文介绍了RecyclerView与GridView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我写了的ListView
演示中,的ListView
的项目有几种不同的类型。
Once I write a ListView
demo, the ListView
's items have some different types.
ITEM1:
文本
产品图产品图产品图
产品图产品图产品图
项目2:
文本
产品图产品图
产品图产品图
项目3:
文本
产品图产品图产品图
产品图产品图产品图
产品图产品图产品图
...
所以我覆盖
的 getViewTypeCount()
和 getItemViewType()
。它很好地工作。
但现在我改变使用 RecyclerView
。有没有更好的解决办法吗?请帮帮我。
So I override
the getViewTypeCount()
and getItemViewType()
.It work well.
But Now I change to use RecyclerView
. Is there a better solution?Please help me.
推荐答案
这是样品,你可以创建一个TextView和非滚动gridview的项目视图
This is sample, you can create an item view with a textview and nonscrolling gridview
public class NonScrollGridView extends GridView {
public NonScrollGridView (Context context) {
super(context);
}
public NonScrollGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollGridView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Do not use the highest two bits of Integer.MAX_VALUE because they are
// reserved for the MeasureSpec mode
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
getLayoutParams().height = getMeasuredHeight();
}
}
您RecyclerView项目视图
Your RecyclerView item view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"/>
<com.example.NonScrollGridView
android:id="@+id/gridView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="@android:color/white"/>
</LinearLayout>
这篇关于RecyclerView与GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文