我有这个使用json api的工作代码,并在recyclerview中显示它们。我还添加了ItemTouchHelper以便滑动以删除操作。现在,我想显示有关新活动的已删除项目。我怎样才能做到这一点?谢谢...
我的MainActivity.java在下面:
public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelperListener {
private final String URL_API = "https://api.openbrewerydb.org/breweries";
private RecyclerView recyclerView;
private List<Item> list;
private CardListAdapter adapter;
private CoordinatorLayout rootLayout;
IMenuRequest mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mService = Common.getMenuRequest();
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Breweries");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
rootLayout = (CoordinatorLayout)findViewById(R.id.rootLayout);
list = new ArrayList<>();
adapter = new CardListAdapter(this,list);
RecyclerView.LayoutManager layoutManager = new
LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallBack
=new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new
ItemTouchHelper(itemTouchHelperCallBack).attachToRecyclerView(recyclerView);
addItem();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.show_deleted) {
Intent myintent = new Intent(MainActivity.this,
NewActivity.class);
startActivity(myintent);
return false;
}
return super.onOptionsItemSelected(item);
}
private void addItem() {
mService.getMenuList(URL_API)
.enqueue(new Callback<List<Item>>() {
@Override
public void onResponse(Call<List<Item>> call,
Response<List<Item>> response) {
list.clear();
list.addAll(response.body());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Item>> call, Throwable t) {
}
});
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int
position) {
if(viewHolder instanceof CardListAdapter.MyViewHolder){
String name = list.get(viewHolder.getAdapterPosition()).getName();
final Item deletedItem = list.get(viewHolder.getAdapterPosition());
final int deleteIndex = viewHolder.getAdapterPosition();
adapter.removeItem(deleteIndex);
Snackbar snackbar = Snackbar.make(rootLayout, name + " removed from
list!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.restoreItem(deletedItem, deleteIndex);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}
}
答案 0 :(得分:2)
您必须执行以下步骤:
1)首先,您必须像这样在Item类中实现Serializable
public class Item implements Serializable{
...
}
2)在onCreate
方法上方定义一个像下面这样的数组列表。
private List<Item> removedItems;
3)在onCreate
方法中,编写以下代码:
removedItems = new ArrayList<>();
3)在adapter.removeItem(deleteIndex)
之后写下面的代码
removedItems.add(deletedItem);
4)在此代码adapter.restoreItem(deletedItem, deleteIndex)
之后;写下面的代码
removedItems.remove(deletedItem);
5)在调用onOptionsItemSelected
方法之前在startActivity
方法中编写下面的代码
intent.putExtra("data", (ArrayList<Item>) removedItems);
6)在NewActivity
中,您可以通过以下代码访问已删除列表:
ArrayList<Item> list =(ArrayList<Item>)getIntent().getSerializableExtra("data");