RecyclerView with GridView

2019-09-19 13:48发布

问题:

Once I write a ListView demo, the ListView's items have some different types.

item1:

Text

Pic Pic Pic

Pic Pic Pic

item2:

Text

Pic Pic

Pic Pic

item3:

Text

Pic Pic Pic

Pic Pic Pic

Pic Pic Pic

...

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.

回答1:

use

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


回答2:

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();


    }
}

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>