Ändern Recycler Objekt-hintergrund-Farbe, indem Sie code

Möchte ich den hintergrund ändern, die Farbe von meinen RecyclerView Element. Ich denke, ich sollte in der Lage sein, es zu tun in onBindViewHolder Methode, aber ich bin nicht in der Lage, dies zu tun. Bekomme ich nur unten Rahmenfarbe des Elements geändert, aber ich wollen, ändern Sie die vollständigen Hintergrundfarbe

Hier ist, was ich will

public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {

    if (//Some Condition) {
        holder.itemView.setBackgroundColor(Color.GREY);
    }
    else {
        holder.itemView.setBackgroundColor(Color.RED);
    }
}

Ich denke, das sollte so etwas wie

Ändern Recycler Objekt-hintergrund-Farbe, indem Sie code

Was ich bekommen, ist dies

Ändern Recycler Objekt-hintergrund-Farbe, indem Sie code

Hier ist mein RecyclerView Fragment layout.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/installed_recyclerView"
        android:paddingTop="1dp"
        ></android.support.v7.widget.RecyclerView>
</LinearLayout>

Hier ist mein Artikel-layout-Datei

<?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"
android:elevation="3dp"
android:paddingBottom="1dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/post_card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="1dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Arial New"
            android:id="@+id/installed_font_name"
            android:textSize="16dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Monotype Solutions"
            android:textSize="12dp"
            android:id="@+id/installed_preview_company_name"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Preview Test"
            android:id="@+id/installed_preview_textview"
            android:textSize="30dp"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="10dp"
                android:gravity="center"
                android:id="@+id/installed_preview_Unnstall">
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_remove_circle_24dp"
                    android:background="@android:color/transparent"
                    android:tint="@android:color/holo_red_light"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Uninstall"
                    android:textSize="12dp"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="10dp"
                android:gravity="center"
                android:id="@+id/installed_preview_flip_layout"
                >

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_autorenew_24dp"
                    android:background="@android:color/transparent"
                    android:tint="@android:color/holo_blue_dark"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Flip This"
                    android:textSize="12dp"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
Können Sie post-layout-xml?
Bitte fügen Sie den xml-code wie @pawelo beantragt, so können wir etwas überprüfen.
getan
Versuchen Sie bitte meine Antwort weiter unten erwähnt wird.
Was ist die build-tool-version, die Sie verwenden?

InformationsquelleAutor Pankaj Bansal | 2016-08-25

Schreibe einen Kommentar