Giter Site home page Giter Site logo

Comments (9)

LichFaker avatar LichFaker commented on May 20, 2024 4

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

from superadapter.

chenenyu avatar chenenyu commented on May 20, 2024

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

from superadapter.

LichFaker avatar LichFaker commented on May 20, 2024

我使用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>

from superadapter.

chenenyu avatar chenenyu commented on May 20, 2024

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

from superadapter.

LichFaker avatar LichFaker commented on May 20, 2024

我觉得不是这个问题, 因为我只要使用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;
                }
            };
        }

    }

from superadapter.

chenenyu avatar chenenyu commented on May 20, 2024

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

from superadapter.

LichFaker avatar LichFaker commented on May 20, 2024

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

from superadapter.

chenenyu avatar chenenyu commented on May 20, 2024

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

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

from superadapter.

chenenyu avatar chenenyu commented on May 20, 2024

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

from superadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.