RecyclerView with GridView

陌路散爱 提交于 2019-12-12 06:03:47

问题


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>


来源:https://stackoverflow.com/questions/32860660/recyclerview-with-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!