java - 为什么我的视图在重新加载时会改变颜色?
问题描述
我正在开发一个 android 应用程序,并且需要为其使用 recyclerview。我希望它的背景是白色的,但由于某种原因,当我运行应用程序时,它变成了灰色。话虽如此,如果我重新加载活动,或者返回然后再次进入活动,颜色确实会变成白色。有没有人遇到过这样的问题?
这是我的项目
<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>
这是我的主要活动
<?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>
这是我的适配器
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();
}
}
解决方案
将此代码放在下面以替换您的 TripAdapter 代码。
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();
}
}
推荐阅读
- snowflake-cloud-data-platform - 用例将 json 加载为一行而不是多行,特定于数据集
- ios - 错误检查:CloudKit MacCatalyst didReceiveRemoteNotification
- algorithm - 如何避免代码质询环境中出现“超出时间限制”错误?
- ada - 将映射传递给通用过程
- python - 检查另一个数据帧的一个值
- amazon-quicksight - Quicksight 将日期解析为月份
- aws-lambda - 我应该如何诊断似乎与 AWS API Gateway 中的授权方相关的 HTTP 请求 500?
- javascript - 如何在没有内容的情况下设置滚动条大小
- java - Java中数组的通用注释
- c# - 如何在 AutoMapper 中进行聚合?