代码之家  ›  专栏  ›  技术社区  ›  Siddarth G

如何根据子位置动态更改recyclerview背景(而不是项目背景)的颜色?

  •  0
  • Siddarth G  · 技术社区  · 8 年前

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

    this image

    背景色在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"
            />
    

    这是项目资源:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/back"
    android:paddingBottom="10dp"
    android:orientation="vertical">
    
    <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:gravity="center"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/iconEntry"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:layout_marginBottom="2dp"/>
    
        <TextView
            android:id="@+id/titleEntry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:layout_marginBottom="2dp"/>
    
        <TextView
            android:id="@+id/titleSpanishEntry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:layout_marginBottom="2dp"/>
    
        <TextView
            android:id="@+id/countEntry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:textSize="10sp"/>
    
    </LinearLayout>
    </android.support.v7.widget.CardView>
    

    适配器:

        public LinearLayout back;
    
        public MyViewHolder(View view) {
            super(view);
            back = view.findViewById(R.id.back);
        }
    

    BindViewHolder上的适配器:

    holder.back.setBackgroundColor(Color.parseColor(
    EntriesItemList.get(getCategory(position))
    .getDisposalTypeCategory()
    .getColor()));
    

    现在的要求是颜色,即使没有像第一节中的空白那样的项目。

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

    2 回复  |  直到 8 年前
        1
  •  1
  •   Siddarth G    8 年前

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

        2
  •  0
  •   havabon    8 年前

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