我创建了一个显示默认布局的RecyclerView列表。然后我在列表中添加一个新项目,并更新布局以显示新项目(CardView)。然后我导航回到之前的活动。当我返回到RecyclerView活动时,我返回到通用的默认列表,我的新CardView项目没有显示。
我在Activity中有正常的onCreate(Bundle savedInstanceState)代码。似乎适配器在返回时只创建一个新的RecyclerView列表,而不是返回到先前创建的列表。我在这里缺少什么?
的活动:
public class ListContactsActivity extends AppCompatActivity {
private ListContactsAdapter mContactsAdapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerviewlist);
final List<Contact> mContacts;
mContacts = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mContactsAdapter = new ListContactsAdapter(this, mContacts);
mContactsAdapter.setOnItemTapListener(new ListContactsAdapter.OnItemTapListener() {
@Override
public void onItemTap(Contact contact, int position) {
mContactsAdapter.removeItem(contact, position);
}
});
mRecyclerView.setAdapter(mContactsAdapter);
...
适配器:
public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView cardBlankText2;
public ContactViewHolder(View itemView) {
super(itemView);
cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnItemTapListener != null) {
Contact contact = mContacts.get(getLayoutPosition());
mOnItemTapListener.onItemTap(contact, getLayoutPosition());
}
}
}
private Context mContext;
private LayoutInflater mLayoutInflater;
private List<Contact> mContacts;
private List<ListItem> mItems;
private OnItemTapListener mOnItemTapListener;
public ListContactsAdapter(Context context, List<Contact> contacts) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContacts = contacts;
mItems = buildItemsList();
}
public void setOnItemTapListener(OnItemTapListener listener) {
mOnItemTapListener = listener;
}
private List<ListItem> buildItemsList() {
List<ListItem> items = new ArrayList<>();
if (mContacts.size() > 0) {
for (Contact contact : mContacts) {
items.add(new ContactItem(contact));
}
} else {
// when R. list is first created or when the number of cards
// is deleted until there are zero, show the defaultcard_layout
// and "Click the + above to get started".
for (int i=0; i<1; i++) {
items.add(new EmptyItem());
}
}
return items;
}
public void addItem(Contact contact) {
if (mContacts.size()==0) {
// if list is empty
// remove empty cards first
mItems.clear();
notifyDataSetChanged();
}
// add item on the top of the list and scroll to the top position
mContacts.add(contact);
mItems.add(new ContactItem(contact));
notifyItemInserted(0);
}
public void removeItem(Contact contact, int position) {
mContacts.remove(contact);
if (mContacts.size()==0) {
// if no more contacts in list,
// rebuild from scratch
mItems.clear();
mItems.addAll(buildItemsList());
notifyDataSetChanged();
} else {
// else remove one item
mItems.remove(position);
notifyItemRemoved(position);
}
}
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public int getItemViewType(int position) {
return mItems.get(position).getType();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ListItem.EMPTY_TYPE) {
View itemView = mLayoutInflater.inflate(R.layout.defaultcard_layout, parent, false);
return new EmptyViewHolder(itemView);
} else {
View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
return new ContactViewHolder(itemView);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
int type = getItemViewType(position);
if (type == ListItem.CONTACT_TYPE) {
ContactItem item = (ContactItem) mItems.get(position);
final Contact contact = item.getContact();
ContactViewHolder holder = (ContactViewHolder) viewHolder;
holder.cardBlankText2.setText(contact.getName() + " " + contact.getSurname());
}
}
}
答案 0 :(得分:3)
当活动通过向后导航从堆栈中弹出时,它不会保留其任何视图或状态。活动对象及其所有视图和状态都将被销毁。如果你需要以某种方式保存活动正在做什么并且需要恢复它,你需要编写代码来做到这一点。