当前位置: 首页 > 知识库问答 >
问题:

背景颜色和图像洗牌滚动在回收查看Android使用组适配器

魏浩广
2023-03-14

下面是我的BindableItem类,它在使用Groupie时也充当适配器

class FilterByAthleteTypeItem(var athleteResponse: AthleteModel, var onFilterAthleteItemClick: OnFilterAthleteItemClick) : BindableItem<FilterItemLayoutBinding>() {

override fun bind(viewBinding: FilterItemLayoutBinding, position: Int) {
    ViewHolder(viewBinding, position)
    viewBinding.executePendingBindings()
    viewBinding.notifyChange()
}

override fun getLayout(): Int {
    return R.layout.filter_item_layout
}


inner class ViewHolder(var binding: FilterItemLayoutBinding, position: Int) : RecyclerView.ViewHolder(binding.root), View.OnClickListener {
    override fun onClick(p0: View?) {
        athleteResponse.isChecked = binding.playlistSwitch.isChecked
        onFilterAthleteItemClick.onFilterAthleteClicked(athleteResponse)
        notifyChanged()
    }

    init {
        val athleteModel = athleteResponse
        binding.totalItems.text = athleteModel.areelCount.toString()
        binding.playlistSwitch.isChecked = athleteModel.isChecked
        binding.sportName.text = athleteModel.athleteType
        binding.playlistSwitch.setOnClickListener(this)

        when {
            athleteModel.athleteType == "highschool" -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context, R.color.black))
            athleteModel.athleteType == "college" -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context, R.color
                            .college))
            athleteModel.athleteType == "pro" -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context, R.color.pro))
            athleteModel.athleteType == "enthusiast" -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context,
                            R.color.enthusiast))
            athleteModel.athleteType == "military" -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context,
                            R.color.text_color_9b))
            else -> binding.playerLevelImage.setBackgroundColor(
                    ContextCompat.getColor(binding.root.context,
                            R.color.white))
        }

    }

   }
  }

 interface OnFilterAthleteItemClick {
 fun onFilterAthleteClicked(athleteModel: AthleteModel)
 }

这是我在我的活动中如何使用它

    Section section = new Section();
    section.setHeader(headerItemGroupie);
    if (!Utils.isNull(athleteModelList))

        for (int i = 0; i < athleteModelList.size(); i++) {
            AthleteModel athleteModel = athleteModelList.get(i);
            athleteModel.setPosition(i);
            athleteModelList.remove(i);
            athleteModelList.add(i, athleteModel);
            section.add(new FilterByAthleteTypeItem(athleteModelList.get(i), this));
        }
    groupAdapter.add(section);

下面是我的布局项目文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

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

    <com.areel.android.customview.CustomTextView
        android:id="@+id/totalItems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="28dp"
        android:layout_weight="1.7"
        android:paddingBottom="18dp"
        android:paddingTop="18dp"
        android:textColor="@color/text_color_9b"
        android:textSize="12sp"
        app:fontPath="@string/font_avenir_heavy"
        app:letterSpacing="0.154"
        tools:text="14,932"/>

    <com.areel.android.customview.CustomTextView
        android:id="@+id/sportName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginEnd="16dp"
        android:layout_weight="1"
        android:gravity="end"
        android:textAllCaps="true"
        android:textColor="@color/black"
        android:textSize="12sp"
        app:fontPath="@string/font_avenir_heavy"
        app:letterSpacing="0.3"
        tools:text="NAME OF SPORT"/>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.8">

        <ImageView
            android:id="@+id/playerLevelImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="21dp"
            android:scaleType="fitXY"/>

        <android.support.v7.widget.AppCompatCheckBox
            android:id="@+id/playlistSwitch"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:button="@drawable/switch_on_off"/>
    </FrameLayout>

</LinearLayout>

这是我在版面中的回收视图

 <android.support.v7.widget.RecyclerView
        android:id="@+id/filter_list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/showFriendLayout"
        android:clipChildren="false"
        android:clipToPadding="false">
    </android.support.v7.widget.RecyclerView>

现在,当我滚动背景颜色和图像开始洗牌意味着背景颜色的各自位置不保持适当,而滚动的主要问题是最后一个项目有一个图像作为背景,当我滚动图像重叠其他背景和洗牌发生但现在有办法改变图像颜色,所以我需要更强大的解决方案!!

我在这里添加屏幕截图第一个,我所做的

我检查物品并滚动回收站视图,然后最后一张图像像下图一样洗牌它的位置。第五张图像也在向下洗牌


共有1个答案

汪安宁
2023-03-14

这里的库的作者。您发布的代码中有几个问题。

首先,您不应该调用执行PendingBindings通知更改。在绑定模型对象时,您没有使用数据绑定的功能,因此这两个功能都是不必要的。

其次,Groupie的重点(尤其是数据绑定)是,您不应该创建自己的ViewHolder。事实上,你的没有做任何事情。您可以将ViewHolder类中的所有代码移动到FilterByAthleteTypeItem.bind()中。

最后,你的图像加载错误或重复的问题是RecyclerViews中非常常见的问题。你没有发布你的图像加载代码,但我猜它是异步加载图像——也就是说它们加载到某个视图中,不管它是否被回收——和/或未能从重用的视图所有者中清除旧图像。我建议你完全避免使用像毕加索或Glide这样的图像加载库来处理这个问题。

希望有帮助!

 类似资料:
  • 问题内容: 我想知道,当我向下或向上滚动时,我的图像和布局的颜色会乱七八糟,我使用recyclerview创建了cardview。并设置图片(在点击时更改颜色,以了解其用户喜欢的项目),并将setbackgroundcolor(随机选择)设置为父版面,以使Cardview更具吸引力。但是当我滚动1.图像更改位置的图像时,2.布局背景自动更改颜色。 我在这里发布适配器的代码。 } 问题答案: 我已经

  • 问题内容: 假设我要在CSS中渲染箭头,箭头应具有头部,尾部和灵活的宽度,以便可以包含文本。我当然可以创建多个div来获得所需的内容,但是如何在CSS3中完成呢? 我可以使用多个背景图片: 的HTML: 这给了我一个带有箭头和尾巴的透明中间部分。似乎不可能在中间部分指定颜色。 仅使用一张背景图像,您可以执行以下操作: 我知道这在很多方面都是可行的,但是背景颜色属性是否真的从速记定义中丢失了? 问题

  • 实际上,我尝试在一个类中设置imageView ColorFilter,该类定义了适配器avec recycle view。为此,我在我的适配器中定义了一个ImageView ArrayList,它从viewholder中存储了我的ImageView,我还在我的适配器中用一个带有position参数的自定义方法来获取它们。但它并不真的工作,当我点击我的项目,颜色过滤器是随机适用于我的recycle

  • 问题内容: 是否可以在Internet选项的“高级”选项卡中不启用“打印背景色和图像”的情况下打印背景图像? 我认为可以使用没有“背景图像”的替代方法…使用div标签和绝对位置可以模拟背景图像的相同效果吗?我也想在页面上重复背景图像。 问题答案: 打印背景图像(在标记中指定为背景图像的那些图像)的能力完全取决于最终用户,您无法从代码中以编程方式控制此功能。Firefox的一个插件提供了JavaSc

  • 在超文本标记语言中,我什么时候使用颜色,背景颜色和背景标签有什么区别? 有什么区别?

  • 我刚开始使用角,但确实练习了超文本标记语言,JS 我已经在全局样式表中的超文本标记语言标签下设置了背景图像,它会以正确的方式出现并填充宽度,但是我无法解决如何滚动的问题图像。 这就是中的内容: 我尝试添加,但这只是在页面的一侧添加了一个滚动条,但不允许我滚动。 我需要添加什么才能获得滚动查看整个背景图像的年龄?