【问题标题】:recyclerview onBind not working properly when Layouts are nested嵌套布局时,recyclerview onBind 无法正常工作
【发布时间】:2018-04-01 08:26:16
【问题描述】:

我试图实现一个简单的 RecyclerView 示例,其中项目交替着色 https://i.stack.imgur.com/BFmJi.jpg 效果很好
如果没有 Framelayout,我们将获得清晰的着色代码,如第一张图片所示 即物品以特定图案着色 然而 添加 FrameLayout 部分后,我遇到颜色代码不正确https://i.stack.imgur.com/phKNT.jpg的问题

从技术上讲,添加 FrameLayout 部分后发生了什么变化

这是 RecyclerView 类 onBindView 代码:

@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, int position) {
    if(values.contains(position+1)){
        holder.textView1.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent));
        System.out.println("Color Check : "+values + " "+position);
    }
    String value = MyData[position];
    holder.textView1.setText(value);
}

这是 RecyclerView xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <FrameLayout
        android:layout_width="match_parent"
        android:elevation="16dp"
        android:layout_margin="16dp"
        android:id="@+id/frame1"
        android:background="#fff"
        android:layout_height="200dp">
        <TextView
            android:layout_width="match_parent"
            android:id="@+id/textView1"
            android:text="Sample Items"
            android:layout_height="wrap_content"
             />
    </FrameLayout>
</LinearLayout>

编辑 1:

我需要的逻辑是项目 1、3、6、10、15 应该着色(FrameLayout 或 textView)
逻辑工作正常,直到在 RecyclerView 帖子的 xml 布局中引入 FrameLayout
上色的项目是 1,3,6,8,10,13,15

【问题讨论】:

  • 能否提供您的预期结果。添加 FrameLayout 后,我​​在图像中看不到任何问题。你想让整张卡片都有颜色吗?
  • 如果您希望孔项目被着色,您应该将颜色设置为 FrameLayout,而不是 TextView...
  • 没有特别需要为 FrameLayout 着色,我也试过了,主要担心的是逻辑没有被复制,加上 Framelayout,Recycler 视图的行为有些奇怪跨度>
  • 当您说“颜色代码不正确”时,您的确切意思是什么?您是否希望 textview 后面的整个框架都被着色?....然后按照@HedShafran 的建议...并通过编辑您的问题来详细说明您的评论,“Recycler 视图的行为有些奇怪”
  • @nobalG 我已经添加了编辑,问题不在于 framelayout 或 textview 的着色,我都尝试过,但是着色的项目顺序是问题

标签: android android-recyclerview android-framelayout


【解决方案1】:

我没有明白你的逻辑,但如果它在你使用 RecyclerView 作为简单列表而不是“卡片”时工作,可能是因为当你在列表中使用大量大小时,onBindViewHolder 只是打印所有内容而没有回收recyclerview 将尝试重用内容。

你可以试试这个:

holder.setIsRecyclable(false);

【讨论】:

  • 像魅力一样工作,但我仍然很好奇发生了什么,这是否意味着代码不会在 RecyclerView 的情况下利用缓存
【解决方案2】:

对我有用的另一件事是需要为 if 设置条件,然后为其余选项设置 else 条件 我认为它比制作 holder.setIsRecyclable(false); 更好的解决方案;

@Override
    public void onBindViewHolder(DataAdapter.ViewHolder holder, int position) {
        //holder.setIsRecyclable(false);
        if(values.contains(position+1)){
            holder.textView1.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent));
        }else
        {
            holder.textView1.setBackgroundColor(ContextCompat.getColor(context,R.color.white));
        }
        String value = MyData[position];
        holder.textView1.setText(value);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多