【发布时间】: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