【问题标题】:RecyclerView with GridView带有 GridView 的 RecyclerView
【发布时间】:2015-09-30 07:48:32
【问题描述】:

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

项目1:

文字

图片图片图片

图片图片图片

项目2:

文字

图片图片

图片图片

项目3:

文字

图片图片图片

图片图片图片

图片图片图片

...

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

【问题讨论】:

    标签: android android-listview android-recyclerview listview-adapter


    【解决方案1】:

    使用

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

    【讨论】:

    • 如果你喜欢,请点赞,调用你的 pojo 类数组并获取它的实例并按大小计算 private ArrayList mDataset;在您的适配器类中
    • 你可能误解了我的意思。
    • 你能解释一下你的代码吗? StackOverflow 上通常不接受纯代码答案,因此可能会被删除。
    • @RajeshJadav 他提出的问题是如何根据我通过的问题从列表中获取项目
    【解决方案2】:

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

    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>
    

    【讨论】:

    • 谢谢,但是 NonScrollGridView 回收时效果不好。所以我扩展了 LinearLayout。
    • @EgosZhang 你的意思是你不希望物品被回收?那么你使用 recyclerview 的意义何在?
    • NonScrollGridView extends GridView 你的代码在滚动时不能正常工作。
    • @EgosZhang 好的,我明白你的意思了。如果您有任何其他问题,请告诉我。
    猜你喜欢
    • 2016-05-14
    • 1970-01-01
    • 2016-02-12
    • 2020-09-14
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多