Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

使用SuperAdapter.add(...)或addAll(...)更新数据异常闪退 #5

Closed
LichFaker opened this issue Apr 15, 2016 · 9 comments
Closed

Comments

@LichFaker
Copy link

使用demo中的MultipleAdapter,在获取数据后调用adapter.add(...)或adapter.addAll(...)更新数据一直报如下异常

java.lang.RuntimeException: Failed to resolve attribute at index 6
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:636)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6486)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6654)
at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1895)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1807)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:62)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:827)
at android.view.LayoutInflater.inflate(LayoutInflater.java:523)
at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
at com.lichfaker.lib.common.views.refresh.SuperAdapter.onCreate(SuperAdapter.java:47)
at com.lichfaker.lib.common.views.refresh.SuperAdapter.onCreate(SuperAdapter.java:17)
at com.lichfaker.lib.common.views.refresh.internal.BaseSuperAdapter.onCreateViewHolder(BaseSuperAdapter.java:287)
at com.lichfaker.lib.common.views.refresh.internal.BaseSuperAdapter.onCreateViewHolder(BaseSuperAdapter.java:28)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5464)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4689)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4599)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1988)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3003)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2881)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1457)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:285)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:799)
at android.view.Choreographer.doCallbacks(Choreographer.java:612)
at android.view.Choreographer.doFrame(Choreographer.java:580)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:785)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
@chenenyu
Copy link
Collaborator

你好,demo中有addAll的使用情况(具体在第4个tab的menu菜单中),看你的log应该是布局出了问题,能贴下相关代码吗

@LichFaker
Copy link
Author

我使用SingleAdapter能正常显示, 复写了IMulItemViewType接口后就报上面的异常, 然后我完全参照demo写的Adapter,只是布局不同,布局代码如下:

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


    <TextView
        android:id="@+id/deviceName"
        android:textColor="@color/device_list_text_bg"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/deviceImgLayout"
        android:layout_below="@id/deviceName"
        android:layout_marginTop="8dp"
        android:background="@drawable/green_stroke_white_solid_bg"
        android:layout_width="130dp"
        android:layout_height="100dp">

        <ImageView
            android:layout_gravity="center"
            android:id="@+id/deviceImg"
            tools:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </FrameLayout>

    <TextView
        android:id="@+id/deviceLeftLabel"
        android:layout_alignTop="@id/deviceImgLayout"
        android:layout_toRightOf="@id/deviceImgLayout"
        android:layout_marginLeft="10dp"
        android:textSize="12sp"
        android:textColor="@color/black_4a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/deviceLeftTv"
        tools:text="100%"
        android:layout_marginTop="2dp"
        android:textColor="@color/main_green"
        android:textSize="24sp"
        android:layout_alignLeft="@id/deviceLeftLabel"
        android:layout_below="@id/deviceLeftLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/deviceSpeedLabel"
        android:textSize="12sp"
        android:textColor="@color/device_list_text_bg"
        android:layout_alignLeft="@id/deviceLeftLabel"
        android:layout_below="@id/deviceLeftLabel"
        android:layout_marginTop="44dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/deviceSpeedTv"
        android:layout_toRightOf="@id/deviceSpeedLabel"
        android:layout_alignBottom="@id/deviceSpeedLabel"
        android:layout_marginLeft="6dp"
        android:textSize="14sp"
        android:textColor="@color/main_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_alignLeft="@id/deviceLeftLabel"
        android:layout_alignBottom="@id/deviceImgLayout"
        android:layout_marginBottom="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.common.views.FontIconView
            android:id="@+id/deviceSpeedLowIcon"
            android:text="@string/icon_font_speed_low"
            android:textSize="14.6sp"
            android:textColor="@color/device_list_icon_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.common.views.FontIconView
            android:id="@+id/deviceSpeedMidIcon"
            android:text="@string/icon_font_speed_mid"
            android:textSize="14.6sp"
            android:layout_marginLeft="6dp"
            android:textColor="@color/device_list_icon_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.common.views.FontIconView
            android:id="@+id/deviceSpeedHighIcon"
            android:text="@string/icon_font_speed_high"
            android:textSize="14.6sp"
            android:layout_marginLeft="6dp"
            android:textColor="@color/device_list_icon_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.common.views.FontIconView
            android:id="@+id/deviceSpeedSleepIcon"
            android:text="@string/icon_font_speed_sleep"
            android:textSize="14.6sp"
            android:textColor="@color/device_list_icon_bg"
            android:layout_marginLeft="6dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <FrameLayout
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:background="@mipmap/switch_bg"
        android:layout_width="48dp"
        android:layout_height="48dp">

        <ImageView
            android:id="@+id/deviceSwitchIcon"
            android:src="@mipmap/switch_off"
            android:layout_gravity="center"
            android:layout_marginLeft="0.5dp"
            android:layout_width="22dp"
            android:layout_height="22dp" />

    </FrameLayout>

    <ImageView
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/arrow_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

@chenenyu
Copy link
Collaborator

你的adapter中的holder使用到的ID必须在布局文件中存在哦,如果方便,贴下adapter相关的代码。

@LichFaker
Copy link
Author

LichFaker commented Apr 16, 2016

我觉得不是这个问题, 因为我只要使用SuperAdapter(Context context, List<T> items, int layoutResId)这个构造函数就能正常显示; 如果别的代码不改, 将布局文件及onBind替换成demo中的也能正常显示,下面是Adapter的代码:

static class SingleAdapter extends SuperAdapter<DeviceInfo> {

        Context context;

        public SingleAdapter(Context context, List<DeviceInfo> items, int resId) {
            super(context, items, resId);
            this.context = context;
        }

        @Override
        public void onBind(SuperViewHolder holder, int viewType, int position, DeviceInfo item) {
            if (viewType == 0) {
                holder.setText(R.id.deviceName, item.nickName);
                ImageView imageView = holder.getView(R.id.deviceImg);
                Glide.with(context).load(item.picUrl).into(imageView);
                holder.setText(R.id.deviceLeftTv, "95%");
                holder.setText(R.id.deviceSpeedTv, "中速");
            }
        }

    }
static class SingleAdapter extends SuperAdapter<DeviceInfo> {

        Context context;

        public SingleAdapter(Context context, List<DeviceInfo> items, IMulItemViewType<DeviceInfo> mulItemViewType) {
            super(context, items, mulItemViewType);
            this.context = context;
        }

        @Override
        public void onBind(SuperViewHolder holder, int viewType, int position, DeviceInfo item) {
//            if (viewType == 0) {
            holder.setText(R.id.deviceName, item.nickName);
            ImageView imageView = holder.getView(R.id.deviceImg);
            Glide.with(context).load(item.picUrl).into(imageView);
            holder.setText(R.id.deviceLeftTv, "95%");
            holder.setText(R.id.deviceSpeedTv, "中速");
//            }
        }

        @Override
        protected IMulItemViewType<DeviceInfo> offerMultiItemViewType() {
            return new IMulItemViewType<DeviceInfo>() {
                @Override
                public int getViewTypeCount() {
                    return 2;
                }

                @Override
                public int getItemViewType(int position, DeviceInfo info) {
                    if (position % 2 == 0) {
                        return 0;
                    }
                    return 1;
                }

                @Override
                public int getLayoutId(int viewType) {
                    if (viewType == 0) {
                        return R.layout.main_device_list_item;
                    }
                    return R.layout.main_device_list_item;
                }
            };
        }

    }

@chenenyu
Copy link
Collaborator

你的两个布局文件是一样的啊? 你可以尝试检查一下Adapter所在的Activity或者Fragment类的布局文件,我觉得跟这个库的关系不大,参见stackoverflow

@LichFaker
Copy link
Author

开始没有重写offerMultiItemViewType(),报了一样的异常,还以为是布局的问题,给改成一样了, 现已解决, thx。
PS: 为何在使用时既要传IMulItemViewType, 又要在offerMultiItemViewType()覆盖掉IMulItemViewType呢?

@chenenyu
Copy link
Collaborator

chenenyu commented Apr 16, 2016

PS: 为何在使用时既要传IMulItemViewType, 又要在offerMultiItemViewType()覆盖掉IMulItemViewType呢?

这两个只要提供一个就行了,不用都写。如果在adapter中override了,在创建adapter的时候传个null就好了。 还有你不用在自己的adapter维持一个context的引用,SuperAdapter内部提供了一个mContext变量和一个getContext()供你调用。 : )

@LichFaker
Copy link
Author

刚重新试了下, 确实如你所说, 之前应该是我使用的问题,看来我得好好看下源码了, 非常感谢

@chenenyu
Copy link
Collaborator

不用客气,有什么问题请联系我 : ) 基本使用 看一遍应该就能愉快地玩耍了。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants