Android 带有GridView的RecyclerView

Android 带有GridView的RecyclerView,android,android-listview,android-recyclerview,listview-adapter,Android,Android Listview,Android Recyclerview,Listview Adapter,一旦我编写了一个ListView演示,那么ListView的项目就有了一些不同的类型 项目1: 正文 Pic Pic Pic Pic Pic Pic 项目2: 正文 Pic Pic Pic Pic 项目3: 正文 Pic Pic Pic Pic Pic Pic Pic Pic Pic 因此,我重写getViewTypeCount()和getItemViewType(),它工作得很好。 但现在我改为使用RecyclerView。有更好的解决方案吗?请帮助我。使用 getItemCount() {

一旦我编写了一个
ListView
演示,那么
ListView
的项目就有了一些不同的类型

项目1:

正文

Pic Pic Pic

Pic Pic Pic

项目2:

正文

Pic Pic

Pic Pic

项目3:

正文

Pic Pic Pic

Pic Pic Pic

Pic Pic Pic

因此,我
重写
getViewTypeCount()
getItemViewType()
,它工作得很好。 但现在我改为使用
RecyclerView
。有更好的解决方案吗?请帮助我。

使用

getItemCount() {
//pojo class array size
 return mDataset.size();
}

这是一个示例,您可以使用textview和非滚动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项目视图

<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>


如果您喜欢,请向上投票,调用您的pojo类数组并获取其实例,然后按大小计算私有ArrayList mDataset;在你的AdapterClasses中,你可能误解了我的意思。你能解释一下你的代码吗?在StackOverflow上,仅代码的答案通常是不可接受的,因此可能会被删除。@RajeshJadav他问的问题是如何从列表中获取项目,因此基于我通过的问题THX,但当非CrollGridView循环时,这并不好。所以我扩展了线性布局。@EgosZhang你是说你不想让这些物品被回收?那么您使用recyclerview的目的是什么呢?NonScrollGridView扩展了GridView您的代码在滚动时无法正常工作。@EgosZhang好的,我明白您的意思。如果你还有其他问题,请告诉我。