使用databinding 后 RecyclerView 的item 超过6条数据,出现重复

发布于 2022-09-06 01:07:32 字数 9603 浏览 32 评论 0

代码中未使用dataBinding 获取数据展示 没有问题,使用后超过6条数据重复显示的现象

item布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <data>

        <variable
            name="itemSpecialViewModel"
            type="com.jason.youtu.viewmodel.ItemSpecialViewModel" />
    </data>

    <android.support.v7.widget.CardView
        android:id="@+id/item_special_card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/colorWhite"
        app:cardCornerRadius="10dp"
        app:cardElevation="8dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorWhite"
                android:orientation="vertical">

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

                    <com.jason.youtu.widget.RatioImageView
                        android:id="@+id/item_main_production_img"
                        android:layout_width="match_parent"
                        android:layout_height="240dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@{itemSpecialViewModel.thumbrUrl}" />

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/item_main_avatar_user_img"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:layout_alignParentBottom="true"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp"
                        android:src="@{itemSpecialViewModel.avatarUrl}"
                        app:civ_border_color="@color/colorWhite"
                        app:civ_border_overlay="true"
                        app:civ_border_width="1dp" />

                </RelativeLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#fff"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <TextView
                        android:id="@+id/item_main_story"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:maxEms="22"
                        android:paddingBottom="5dp"
                        android:singleLine="true"
                        android:text="@{itemSpecialViewModel.story}"
                        android:textColor="#b3b3b3"
                        android:textSize="11sp" />
                    <!--图片故事: 向晚意不适,驱车植物园。夕阳西落去,只余秋风怜。- 雨淋大湿-->

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal"
                        android:paddingBottom="5dp">

                        <ImageView
                            android:layout_width="9dp"
                            android:layout_height="9dp"
                            android:src="@mipmap/camera"
                            android:textColor="#b3b3b3" />

                        <TextView
                            android:id="@+id/item_main_camera"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:text="@{itemSpecialViewModel.gears}"
                            android:textColor="#b3b3b3"
                            android:textSize="10sp" />

                        <ImageView
                            android:layout_width="9dp"
                            android:layout_height="9dp"
                            android:layout_marginLeft="15dp"
                            android:src="@mipmap/location" />

                        <TextView
                            android:id="@+id/item_main_location"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:text="@{itemSpecialViewModel.location}"
                            android:textColor="#b3b3b3"
                            android:textSize="10sp" />
                    </LinearLayout>


                </LinearLayout>


            </LinearLayout>


        </FrameLayout>


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

RecyclerView adapter代码:


public class SpecialViewBinder extends ItemViewBinder<Special, SpecialViewBinder.ViewHolder> {


    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        ItemSpecialBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_special,
                parent, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull Special item) {
        Log.v("SpecialViewBinder ---> item : " + item.toString());
        holder.bindModel(item); // 传递数据


    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ItemSpecialBinding binding;

        public ViewHolder(ItemSpecialBinding binding) {
            super(binding.itemSpecialCardView);
            this.binding = binding;
        }

        void bindModel(Special model) {
            if (binding.getItemSpecialViewModel() == null) {
                binding.setItemSpecialViewModel(new ItemSpecialViewModel(model));
                binding.executePendingBindings();
            } else {
                binding.getItemSpecialViewModel().setSpecial(model);
                binding.executePendingBindings();
            }
        }

    }
}

ItemSpecialViewModel 代码:

public class ItemSpecialViewModel extends Observable {


    private ObservableField<String> story;
    private ObservableField<String> location;
    private ObservableField<String> gears;
    private ObservableField<String> avatarUrl;
    private ObservableField<String> thumbrUrl;

    private Special special;


    private Context context;

    public ItemSpecialViewModel(Special special) {
        this.context = context;
        this.story = new ObservableField<>();
        this.location = new ObservableField<>();
        this.gears = new ObservableField<>();
        this.avatarUrl = new ObservableField<>();
        this.thumbrUrl = new ObservableField<>();
        this.special = special;
    }


    public Special getSpecial() {
        return special;
    }

    public void setSpecial(Special special) {
        this.special = special;
    }


    public ObservableField<String> getStory() {
        setStory();
        return story;
    }

    public void setStory() {
        this.story.set("图片故事:" + getValue(special.getStory(), "story"));
    }

    public ObservableField<String> getLocation() {
        setLocation();
        return location;
    }

    public void setLocation() {
        this.location.set(getValue(special.getStory(), "location"));
    }

    public ObservableField<String> getGears() {
        setGears();
        return gears;
    }

    public void setGears() {
        this.gears.set(getValue(special.getStory(), "gears"));
    }


    public ObservableField<String> getAvatarUrl() {
        setAvatarUrl();
        return avatarUrl;
    }

    public void setAvatarUrl() {
        this.avatarUrl.set(special.getAvatar());
    }

    public ObservableField<String> getThumbrUrl() {
        setThumbrUrl();
        return thumbrUrl;
    }

    public void setThumbrUrl() {
        this.thumbrUrl.set(special.getThumb());
    }


    @BindingAdapter({"android:src"})
    public static void loadImage(ImageView imageView, String url) {

        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.NORMAL)
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(requestOptions)
                .thumbnail(0.1f)
                .into(imageView);

    }


    public String getValue(String data, String key) {
        JSONObject js = null;
        String r = null;
        try {
            js = new JSONObject(data);
            r = js.optString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return r;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文