I am developing an android app, and need to use a recyclerview for it. I want its background to be white, but for some reason when I run the app it turns out gray. That being said, if I reload the activity, or go back and then again to the activity, the color does turn white. Has anyone ever encountered such a problem?
This is how it looks when you first enter the app
And this is what happens if you reload the activity.
Here's my item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/trip"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@null"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/destination"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center"
android:textSize="24sp" />
<TextView
android:id="@+id/start_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:gravity="center"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@+id/destination" />
<TextView
android:id="@+id/trip_length"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center" />
</LinearLayout>
and here is my main activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView3"
android:layout_width="180dp"
android:layout_height="180dp"
android:contentDescription="@string/app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/app_logo" />
<TextView
android:id="@+id/main_name"
android:layout_width="359dp"
android:layout_height="26dp"
android:text="@string/welcome_message"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/trips"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fadingEdge="horizontal|vertical"
android:maxHeight="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my adapter
package com.alonkh2.finalproject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(ArrayList<Trip> trips) {
this.trips = trips;
}
@NonNull
@Override
public TripAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
TextView textView = holder.destination;
textView.setText(trip.getDestination());
textView = holder.startDate;
textView.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// textView = holder.endDate;
// textView.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
textView = holder.length;
textView.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
@Override
public int getItemCount() {
return trips.size();
}
}
Put this code below to replace your TripAdapter code.
package com.alonkh2.finalproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
Context context;
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(Context context, ArrayList<Trip> trips) {
this.context = context;
this.trips = trips;
}
@NonNull
@Override
public TripAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
@Override
public void onBindViewHolder(@NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
holder.destination.setText(trip.getDestination());
holder.startDate.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// holder.endDate.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
holder.length.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
@Override
public int getItemCount() {
return trips.size();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.