0

一旦我写了一个ListView演示,ListView的项目有一些不同的类型。

项目1:

文本

图片 图片 图片

图片 图片 图片

项目2:

文本

图片 图片

图片 图片

第 3 项:

文本

图片 图片 图片

图片 图片 图片

图片 图片 图片

...

所以我override和它工作得很好getViewTypeCount()getItemViewType()但现在我改用RecyclerView. 有更好的解决方案吗?请帮助我。

4

2 回答 2

0

利用

getItemCount() {
//pojo class array size
 return mDataset.size();
}
于 2015-09-30T08:01:48.867 回答
0

这是示例,您可以使用文本视图和非滚动网格视图创建项目视图

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>
于 2015-09-30T08:49:58.343 回答