Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何根据子位置动态更改recyclerview背景(非项目背景)颜色?_Android_Android Recyclerview - Fatal编程技术网

Android 如何根据子位置动态更改recyclerview背景(非项目背景)颜色?

Android 如何根据子位置动态更改recyclerview背景(非项目背景)颜色?,android,android-recyclerview,Android,Android Recyclerview,我有一个类似下图的回收视图 背景色在adapter using position(适配器使用位置)中动态给定,因此,仅当存在项(卡)时,我可以为背景着色,我将linearlayout作为cardview的父项,并且我正在为线性布局着色 回收视图: <android.support.v7.widget.RecyclerView android:id="@+id/cardView" android:layout_width="match_parent"

我有一个类似下图的回收视图

背景色在adapter using position(适配器使用位置)中动态给定,因此,仅当存在项(卡)时,我可以为背景着色,我将linearlayout作为cardview的父项,并且我正在为线性布局着色

回收视图:

<android.support.v7.widget.RecyclerView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="40dp"
        style="@style/scrollbar_style"
        />
BindViewHolder上的适配器:

holder.back.setBackgroundColor(Color.parseColor(
EntriesItemList.get(getCategory(position))
.getDisposalTypeCategory()
.getColor()));
现在的要求是颜色,即使没有像第一节中的空白那样的项目


只有根据适配器位置动态更改recyclerview背景(而不是卡背景)才能做到这一点,但看起来不可能,因为两者之间没有关系。有什么建议吗?

一个解决方案是在适配器构造函数中传递RecyclerView,并在适配器onBindViewHolder方法中根据适配器位置更改RecyclerView背景。

我通过添加虚拟不可见项来确保一行中有3个项,从而解决了这个问题,因此,背景色会一直延伸到整行。

而不是为“回收者”视图的单个项目添加颜色为特定的“回收者”视图添加背景色这是我的问题。。如何根据位置以编程方式动态地为recyclerview提供颜色,它不能是单个的color@SiddarthG您是如何在每个部分中加载不同数量的项目的?这是一个gridLayoutManager回收视图,我已经在section中使用SectionEdGridRecycleServiceAdapter。这将改变整个背景,我认为不仅仅是该部分?是的,在onBindViewHolder中,只需根据适配器位置更改在构造函数中传递的recyclerView的背景。例如,您的构造函数:public YourAdapter(列出您的列表、上下文、查看您的recyclerView){this.yourList=yourList;this.context=context;this.recyclerView=yourryclerView;}然后在onBindViewHolder中:如果(yourList.get(holder.getAdapterPosition())…)recyclerView.setBackgroundColor(…);不起作用,它将使用单个颜色为整个列表着色尝试holder.itemView.setBackgroundColor();
    public LinearLayout back;

    public MyViewHolder(View view) {
        super(view);
        back = view.findViewById(R.id.back);
    }
holder.back.setBackgroundColor(Color.parseColor(
EntriesItemList.get(getCategory(position))
.getDisposalTypeCategory()
.getColor()));