回收视图膨胀不同的行:- 绑定数据时出现异常

2023-12-22


我正在研究Recyceview具有不同的项目通货膨胀。当我NOT将数据绑定到onBindViewHolder的方法RecycleView比它不崩溃..
但是当我绑定里面的数据时onBindViewHolder比我得到的Exception,请检查我的代码并让我知道我哪里做错了。

package com.tv.practise.adapter;

  /**
    Created by Ravindra Kushwaha on 10/10/16.
   */

public class RecycleDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context mContext;
private ArrayList<RecycleBen> data;

public static class SimpleText extends RecyclerView.ViewHolder {
    TextView first_data_tv;

    public SimpleText(View v) {
        super(v);
        this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
    }
}

public   class SimpleImage extends  RecyclerView.ViewHolder {
    ImageView second_data_iv;

    public SimpleImage(View v) {
        super(v);
        this.second_data_iv = (ImageView) v.findViewById(R.id.second_data_iv);
    }
}

public  class SimpleImageWithText extends  RecyclerView.ViewHolder {
    TextView third_data_tv;
    ImageView third_iv;

    public SimpleImageWithText(View v) {
        super(v);
        this.third_data_tv = (TextView) v.findViewById(R.id.third_data_tv);
        this.third_iv = (ImageView) v.findViewById(R.id.third_iv);
    }
}

public RecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
    this.mContext = mContext;
    this.data = data;
}

@Override
public  RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    int listViewItemType = getItemViewType(viewType);
    View itemView;
    if(listViewItemType==1)
    {

        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycle_first_item, parent, false);

        return new SimpleText(itemView

        );

    }
    else if(listViewItemType==2)
    {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycle_fsecond_item, parent, false);
        return new SimpleImage(itemView);
    }
    else
    {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycle_third_item, parent, false);
        return new SimpleImageWithText(itemView);
    }



}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    RecycleBen bean = data.get(position);

    if(holder.getItemViewType()==1)
    {
           /////HERE I AM GETTING THE EXCEPTION WHILE BINDIND DATA
        ((SimpleText)holder).first_data_tv.setText(bean.getName());

    }
    else if(holder.getItemViewType()==2)
    {

    }
    else {
          /////HERE I AM GETTING THE EXCEPTION WHILE BINDIND DATA((SimpleImageWithText)holder).third_data_tv.setText(bean.getName());
    }

}




@Override
public int getItemViewType(int position) {
    return Integer.parseInt(data.get(position).getType_row());
}

@Override
public int getItemCount() {
    return data.size();
  }
}

例外当我绑定时,数据如下:-

3758-3758/com.tv.practise E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tv.practise, PID: 3758
    java.lang.ClassCastException: com.tv.practise.adapter.RecycleDataAdapter$SimpleImageWithText cannot be cast to com.tv.practise.adapter.RecycleDataAdapter$SimpleText
            at com.tv.practise.adapter.RecycleDataAdapter.onBindViewHolder(RecycleDataAdapter.java:101)
            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
            at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
            at android.view.View.measure(View.java:18804)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
            at android.view.View.measure(View.java:18804)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
            at android.view.View.measure(View.java:18804)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
            at android.view.View.measure(View.java:18804)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
            at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
            at android.view.View.measure(View.java:18804)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:606)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5443)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

@Joshua缩短了我的问题..
对于其他用户,我在这里发布的是用于膨胀不同行的代码(3 rows) in the RecycleView
它工作正常,检查以下代码行:-我在这里发布我的完整代码,请检查:-

Here RecyleClass.java是我的主要课程

package com.tv.practise.recycleview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.tv.practise.R;

import java.util.ArrayList;

/**
 * Created by Ravindra Kushwaha on 10/10/16.
 */
public class RecyleClass extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_main);

        RecyclerView recycler_vw = (RecyclerView)findViewById(R.id.recycler_vw);


        ArrayList<RecycleBen> arrayList = new ArrayList<>();

        for (int i = 0;i<=25;i++)
        {
            RecycleBen bean = new RecycleBen();

            if(i%2==0)
            {

                bean.setType_row("1");
                bean.setName("First element");
                bean.setImage_url("http://www.androhub.com/wp-content/uploads/2015/09/staggeredrecyclerview_banner.jpg");
            }
            else if(i%3==0)
            {
                bean.setType_row("2");
                bean.setName("Second element");
                bean.setImage_url("https://i.stack.imgur.com/snB84.png");
            }
            else
            {
                bean.setType_row("3");
                bean.setName("Third element");
                bean.setImage_url("http://inducesmile.com/wp-content/uploads/2015/05/gridbanner.jpg");
            }

            arrayList.add(bean);

        }

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);

        recycler_vw.setLayoutManager(mLayoutManager);
        recycler_vw.setItemAnimator(new DefaultItemAnimator());
        recycler_vw.setAdapter(new RecycleDataAdapter(this, arrayList));

    }
}

这是我的layout为了RecyleClass.java那是recycle_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_vw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        />
</LinearLayout>

Gradles条目为Recycleview with CardView and Glide

 // CardView
    compile 'com.android.support:cardview-v7:23.4.0'
    // RecyclerView
    compile 'com.android.support:recyclerview-v7:23.4.0'

    // For the glide libraray
    compile 'com.github.bumptech.glide:glide:3.7.0'

下面是我的getter and setter类即RecycleBen.java

package com.tv.practise.recycleview;

/**
 * Created by Ravindra Kushwaha on 10/10/16.
 */
public class RecycleBen {

    private String type_row;
    private String name;
    private String image_url;


    public String getType_row() {
        return type_row;
    }

    public void setType_row(String type_row) {
        this.type_row = type_row;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }
}

最后我的适配器类是RecycleDataAdapter.java

package com.tv.practise.adapter;

/**
 * Created by Ravindra Kushwaha on 10/10/16.
 */
public class RecycleDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<RecycleBen> data;



    public  class SimpleText extends RecyclerView.ViewHolder {
        TextView first_data_tv;

        public SimpleText(View v) {
            super(v);
            this.first_data_tv = (TextView) v.findViewById(R.id.first_data_tv);
        }
    }

    public   class SimpleImage extends  RecyclerView.ViewHolder {
        ImageView second_data_iv;
        ProgressBar second_pb;

        public SimpleImage(View v) {
            super(v);
            this.second_data_iv = (ImageView) v.findViewById(R.id.second_data_iv);
            this.second_pb = (ProgressBar)v.findViewById(R.id.second_pb);
        }
    }

    public  class SimpleImageWithText extends  RecyclerView.ViewHolder {
        TextView third_data_tv;
        ImageView third_iv;
        ProgressBar third_pb;

        public SimpleImageWithText(View v) {
            super(v);
            this.third_data_tv = (TextView) v.findViewById(R.id.third_data_tv);
            this.third_iv = (ImageView) v.findViewById(R.id.third_iv);
            this.third_pb = (ProgressBar)v.findViewById(R.id.third_pb);
        }
    }

    public RecycleDataAdapter(Context mContext, ArrayList<RecycleBen> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public  RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView;
        if(viewType==1)
        {

            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recycle_first_item, parent, false);

            return new SimpleText(itemView

            );

        }
        else if(viewType==2)
        {
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recycle_fsecond_item, parent, false);
            return new SimpleImage(itemView);
        }
        else
        {
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recycle_third_item, parent, false);
            return new SimpleImageWithText(itemView);
        }



    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        RecycleBen bean = data.get(position);

        if(holder.getItemViewType()==1)
        {

            ((SimpleText)holder).first_data_tv.setText(bean.getName());

        }
        else if(holder.getItemViewType()==2)
        {
            final SimpleImage simple_holder = (SimpleImage)holder;



            simple_holder.second_pb.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(bean.getImage_url())
                    .fitCenter()
                    .crossFade()
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            if (e instanceof UnknownHostException)
                                simple_holder.second_pb.setVisibility(View.VISIBLE);

                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            simple_holder.second_pb.setVisibility(View.GONE);
                            simple_holder.second_data_iv.setVisibility(View.VISIBLE);
                            return false;
                        }
                    }).into(simple_holder.second_data_iv);;

        }
        else {

            final SimpleImageWithText third_holder = (SimpleImageWithText)holder;

            third_holder.third_data_tv.setText(bean.getName());

            third_holder.third_pb.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(bean.getImage_url())
                    .fitCenter()
                    .crossFade()
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            if (e instanceof UnknownHostException)
                                third_holder.third_pb.setVisibility(View.VISIBLE);

                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            third_holder.third_pb.setVisibility(View.GONE);
                            third_holder.third_iv.setVisibility(View.VISIBLE);
                            return false;
                        }
                    }).into(third_holder.third_iv);;



        }

    }




    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(data.get(position).getType_row());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

当我们对不同的行进行充气时Recycleview,所以我们在这里使用3 layout分别是recycle_first_item.xml ,recycle_fsecond_item.xml和最后一个recycle_third_item.xml
我一一展示了所有的布局xml,如下:-

recycle_first_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/first_data_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="25sp"
            android:text="Hello Card" />

    </LinearLayout>

recycle_fsecond_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <ProgressBar
            android:id="@+id/second_pb"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            />

        <ImageView
            android:id="@+id/second_data_iv"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:textColor="@android:color/black"
            android:layout_centerInParent="true"
            android:visibility="invisible"
            android:src="@drawable/bubble1" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

recycle_third_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/third_data_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="25sp"
            android:layout_centerInParent="true"
            android:text="Hello Card" />

        <ProgressBar
            android:id="@+id/third_pb"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:layout_below="@+id/third_data_tv"
            />

        <ImageView
            android:id="@+id/third_iv"
            android:layout_width="match_parent"
            android:layout_below="@+id/third_data_tv"
            android:layout_height="150dp"
            android:src="@drawable/bubble2"
            android:layout_centerInParent="true"
            android:visibility="invisible"
            />

    </RelativeLayout> 
</android.support.v7.widget.CardView>

And finally the result is as below :-
Result below

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

回收视图膨胀不同的行:- 绑定数据时出现异常 的相关文章

随机推荐

  • 如何删除被覆盖模块上的路由?

    I added zfcUser通过 Composer 将模块添加到我的项目并在模块中覆盖它ZfcUserOverride 我想要尾部斜杠工作 所以我在覆盖的模块中添加了路线 zfcUserOverride file module confi
  • 了解多媒体计时器的奇怪行为

    我在我的应用程序 C NET 中使用多媒体计时器来提高计时器的准确性并实现 1 毫秒的计时器频率 到目前为止 我的应用程序一直运行良好 直到最近它开始表现奇怪 我试图了解我的应用程序出了什么问题 以下是采取的步骤 定时器频率设置为 1 ms
  • Clearcase 快照劫持文件:如何签出/签入更改的文件

    Clearcase 太糟糕了 看来我不能快速保存对项目的修改 我所说的快速是指不到 1 秒的时间 我找到的解决方案是使用组合clearcase git 我使用快照视图是因为我可以轻松劫持我的文件 而不必每次想要进行重构时都签出项目中的所有文
  • 使用递归算法在 OpenCV 中进行连通分量标记

    我正在尝试使用递归算法在 OpenCV 中实现连接组件标记 我不确定我实施了什么错误 算法是这样的 B 是二值图像 LB 是标记二值图像 procedure connected component B LB LB negate B labe
  • 在 C# 中测试“Ctrl”按键

    How do I test for Ctrl down in Windows Forms http en wikipedia org wiki Windows Forms C bool ctrl Control ModifierKeys K
  • 设置开发设备(????????????没有权限)

    我使用的是三星 Galaxy Nexus 手机 安卓4 0平台 我正在 Ubuntu Linux 操作系统上开发 Android 应用程序 我想直接在三星手机设备上运行我的应用程序 因此我执行了以下设置步骤 在我的项目中AndroidMan
  • Lucene不能查询null?

    我们如何构造一个查询来搜索特定字段不为空 field name 不管用 我试过field name a to z 这对于英语来说效果很好 但并不涵盖所有语言 还有其他建议吗 我发现这在某些情况下有效field 0 TO 9 a TO z
  • 选择 Rails 主机 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我即将挑选一个 Rails 主机 我think我需要一个 VPS 解决方案 因为 1 我的 Rails 应用程序有需要安装才能使其正常工作的 g
  • 在R中找出一个月的天数

    我在P有个约会 date as Date 2011 02 23 Y m d 是否可以找出该特定日期该月的天数 关于闰年 在 PHP 中 它看起来与此类似 http www php net manual en function date ph
  • javax.mail.internet.ParseException:需要“/”,已获取文件

    我在尝试发送带有文件附件的电子邮件时遇到错误 javax mail internet ParseException Expected got files at javax mail internet ContentType
  • C++,确定素数

    我昨天开始读一本关于C 的书 到目前为止 我已经写了 100 页 并用这个数字来编写我的第一个程序 我想让它找出给定的数字是否是素数 我对此有 2 个问题 我知道我的方法并不好 该程序正在检查每个数字 这使得程序变得很大 做到这一点的理想方
  • 根据颜色分割图像中的像素(Matlab)

    我正在尝试仅使用颜色信息 目前 来分割包含多个乐高积木的图像 目的是找到乐高积木 例如是绿色的 我尝试过使用 k 均值聚类 但给定的不同颜色砖块的数量各不相同 我还尝试使用 Matlab 中的以下示例website http uk math
  • 如何修复 TypeError:无法从 Express Nodemailer 读取属性“名称”

    所以我想说 我一直在寻找这个问题的答案 我也尝试控制台记录我的 req body 帖子表单 但我一直得不到定义 所以我觉得我正在丢失发送的表单中的数据 我不确定我做错了什么 所以是时候展示一些代码了 请注意 我使用 Handlebars 进
  • 用烧瓶计算唯一访客数

    我想计算唯一访问者的数量并将该数字存储在我的数据库中 我知道我可以通过以下方式获取 IP 地址 request remote addr 所以我的默认计划是在我的数据库中存储这个地址和时间戳 对于任何新访问者 我都会比较该 IP 地址是否已添
  • 我可以在 PHP 中使用多个类来扩展一个类吗?

    如果我有几个具有我需要的功能的类 但想单独存储以进行组织 我可以扩展一个类以同时拥有这两个功能吗 i e class a extends b extends c 编辑 我知道如何一次扩展一个类 但我正在寻找一种方法来立即使用多个基类扩展一个
  • Box2D 碰撞错误

    我目前在 libgx 中使用 java 中的 box2d 物理引擎 并且面临碰撞问题 问题是身体停止移动 并且在平坦区域上出现碰撞点 我的工作方式是制作多个主体 每个主体代表一个块 并且这些块是并排的 看这里的碰撞点 物体在这里不应该碰撞
  • 将 Map 转换为 json

    I have Map
  • 无法绑定到“control”,因为它不是 (myComponent) 的已知属性

    这是我的 app module ts import NgModule from angular core import BrowserModule from angular platform browser import HttpModul
  • 将 null 值表示为 xml jaxb 中的空元素

    我需要在 jaxb 中将 null 值显示为空元素 我正在使用 jaxb 的 moxy 实现 我找到了这个选项 XmlNullPolicy emptyNodeRepresentsNull true nullRepresentationFor
  • 回收视图膨胀不同的行:- 绑定数据时出现异常

    我正在研究Recyceview具有不同的项目通货膨胀 当我NOT将数据绑定到onBindViewHolder的方法RecycleView比它不崩溃 但是当我绑定里面的数据时onBindViewHolder比我得到的Exception 请检查