Ä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
Was ich bekommen, ist dies
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?
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
Du musst angemeldet sein, um einen Kommentar abzugeben.
Bitte versuchen Sie, ändern Sie den code in onBindViewHolder wie unten
Ich hoffe, dies wird Ihnen helfen , Fühlen Sie sich frei zu kommentieren.
Diese Antwort könnte besser sein. ViewHolder pattern wurde eingeführt, um loszuwerden, mit findViewById() und explizite Umwandlung bei der Bindung von Daten. Diese Vorgänge werden als teuer und brauchen nicht wiederholt zu werden, auf jeden onBindViewHolder () - Aufruf
Ich bin nicht sicher, ob es besser ist, aber es ist ein viewType: stackoverflow.com/questions/26245139/...
Ich glaube nicht, es ist eine gute Idee. Ich hatte mit diesem Konzept seit langer Zeit, aber kürzlich Stand ich vor einem problem, dh, bei sehr schnellen oder langsamen scrollen, wird die Hintergrundfarbe geändert, wo es nicht haben sollte. Also ich manipuliert Ansichten über getItemViewType(). Überraschend, die änderung der Sichtbarkeit einer Sicht innerhalb eines viewHolder in onBindViewHolder funktioniert bestens. Ich weiß nicht, was verursacht diese plötzliche Verhalten.
InformationsquelleAutor Mahamadali
Denke ich, dass die Halter.itemView ist nicht CardView aber LinearLayout, das hält CardView. Probieren Sie etwas wie dieses:
InformationsquelleAutor pawelo
@pankaj wie @pawelo sagte casting kann teuer sein, manchmal ist es die beste Lösung wäre, um die transparente Farbe zu CardView in xml, wie dies
und legen Sie dann die hintergrund Farbe Halter itemView wie diese.
nun sollte es so funktionieren wie Sie sollen.
InformationsquelleAutor Velmurugan V
Ich habe versucht, die oben genannten Antworten in meinem Projekt, es funktionierte aber Android studio gab mir Fehler.
So habe ich
holder.text_container.setBackgroundResource(R.color.category_numbers);
statt und die roten Linien waren verschwunden. Ich verwendete Farbe aus meiner benutzerdefinierte Ressource".Ich hoffe, das jemand hilft.
InformationsquelleAutor rugue