本文介绍了回收视图膨胀不同的行:- 绑定数据时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在研究具有不同物品通胀的 Recyceview.当我 NOTRecycleViewonBindViewHolder 方法上绑定数据时,它不会崩溃..
但是当我在 onBindViewHolder 中绑定数据而不是 Exception 时,请检查我的代码并让我知道我哪里做错了.


I am working on the Recyceview with different item inflation. When i am NOT binding the data on onBindViewHolder method of RecycleView than it Does not crash..
But when i am binding the data inside the onBindViewHolder than i am getting the Exception, Please check my code and let me know where am i doing wrong.

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

异常,当我绑定数据时,如下:-

Exception while i am binding the data are as follow:-

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)

推荐答案

原因是onCreateViewHolder(ViewGroup parent, int viewType).参数是视图类型而不是位置.使用 int listViewItemType = getItemViewType(viewType) 是不正确的,因为 position 应该传递给 getItemViewType.

The reason isonCreateViewHolder(ViewGroup parent, int viewType). The parameter is view type but not position. Using int listViewItemType = getItemViewType(viewType) is incorrect because position should be passed to getItemViewType.

简而言之,您应该直接使用 viewType 并删除 onCreateViewHolder 中的 listViewItemType.

In short, you should use viewType directly and remove listViewItemType in onCreateViewHolder.

这篇关于回收视图膨胀不同的行:- 绑定数据时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:29