在最小化或打开其他应用后,我的内容正在消失,我需要再次填写回收者视图
P.S。在清单中我有
android:configChanges="orientation|keyboardHidden|screenSize"
recyclerView
<android.support.v7.widget.RecyclerView
android:id="@+id/booking_rec"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:background="#ECEFF1">
</android.support.v7.widget.RecyclerView>
适配器
public class RecAdapterBooking extends RecyclerView.Adapter<RecAdapterBooking.CustomViewHolder> {
private List<Booking> bookingList;
private Context context;
public RecAdapterBooking(List<Booking> bookingList, Context context) {
this.bookingList = bookingList;
this.context = context;
}
@Override
public RecAdapterBooking.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.booking_view_item, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecAdapterBooking.CustomViewHolder holder, int position) {
final Booking booking = bookingList.get(position);
String name = booking.getUser().getFirstName() + " " + booking.getUser().getLastName();
String date = booking.getStartTime().toString() + " - " + booking.getToTime().toString();
holder.name.setText(booking.getName());
holder.user.setText(name);
holder.dateTime.setText(date);
holder.type.setText(booking.getType().toString());
}
@Override
public int getItemCount() {
return bookingList.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView name;
protected TextView user;
protected TextView dateTime;
protected TextView type;
public CustomViewHolder(View view) {
super(view);
this.name = (TextView) view.findViewById(R.id.name);
this.user = (TextView) view.findViewById(R.id.user);
this.dateTime = (TextView) view.findViewById(R.id.date_time);
this.type=(TextView) view.findViewById(R.id.booking_type);
}
}
}
mainactivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
List<Room> roomList = new ArrayList<>();
static List<Booking> bookingList = new ArrayList<>();
static RecyclerView recyclerView;
static RecAdapterBooking recAdapterBooking;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Helper.initList(roomList);
Menu menu = navigationView.getMenu();
for (Room r : roomList) {
menu.add(r.getName());
}
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView = (RecyclerView) findViewById(R.id.booking_rec);
recyclerView.setLayoutManager(layoutManager);
final Calendar c = Calendar.getInstance();
final SimpleDateFormat df = new SimpleDateFormat("MMM-dd-yyyy");
final TextView textView = (TextView) findViewById(R.id.time_view);
setTime(c, df, textView);
ImageButton leftButton = (ImageButton) findViewById(R.id.left);
ImageButton rightButton = (ImageButton) findViewById(R.id.right);
leftButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, -1);
textView.setText(df.format(c.getTime()));
}
});
rightButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, +1);
textView.setText(df.format(c.getTime()));
}
});
if (PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getBoolean("pref", true)) {
Intent intent = new Intent(MainActivity.this, SingleRoomActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
this.finish();
}
}
public void setTime(Calendar c, SimpleDateFormat df, TextView textView) {
final String formattedDate = df.format(c.getTime());
textView.setText(formattedDate);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//settings-ზე დაჭერის activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
ArrayList<String> roomNameList = new ArrayList<>();
for (Room r : roomList) {
roomNameList.add(r.getName());
}
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
intent.putExtra("roomlist", roomNameList);
startActivity(intent);
}
if (id == R.id.action_log_out) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
//მენიუდან არჩეული ოთახის განრიგის გამოტანა
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
for (Room r : roomList) {
if (item.getTitle().equals(r.getName())) {
bookingList = r.getBookingList();
recAdapterBooking = new RecAdapterBooking(bookingList, MainActivity.this);
recyclerView.setAdapter(recAdapterBooking);
setTitle(r.getName());
break;
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
这可能是由于生命周期而发生的。当您最小化应用程序时,将在暂停时调用,然后在停止时调用。当您最大化应用程序时,将在“开始”时调用。可能是您在OnCreate上设置了内容。这样,如果您的应用在使用其他应用时被杀死,则再次调用它时,onCreate不会被调用,这样您就不会再次放置内容。
要解决此问题,您也可以在开始或恢复时调用提交列表方法。 OnStart是一种更好的方法。