[英]Remove RecyclerView adapter item from Activity
I have Quote List class QuoteListFragment. 我有Quote List类QuoteListFragment。 where I am loading data in adapter from server like this
我在从服务器这样的适配器中加载数据的地方
private ArrayList<Quote> quotes;
quotes = response.body();
NewQuoteAdapter adapter = new NewQuoteAdapter(getContext(), response.body());
mQuoteList.setAdapter(adapter);
and RecyclerView Adapter called NewQuoteAdapter
和称为
NewQuoteAdapter
RecyclerView Adapter
I am displaying Item in details with QuoteViewFragment
. 我正在使用
QuoteViewFragment
详细显示Item。 I have implemented a button called "delete" in this fragment and I want to give the user a chance to delete that quote from fragment so When the user goes back to the list, it disappears from the list. 我在此片段中实现了一个名为“删除”的按钮,我想给用户一个机会,从片段中删除该引号,以便当用户返回列表时,它从列表中消失。 I have no idea how to achieve this.
我不知道如何实现这一目标。 Let me know if someone can give me a solution for this.
让我知道是否有人可以给我解决方案。 Thanks
谢谢
well you have a list, and adapter, and a recyclerview 好吧,您有一个列表,一个适配器和一个recyclerview
ArrayList<String> myQuoteList = new ArrayList<String>();
MyCustomAdapter adapter = new MyCustomAdapter(myQuoteList);
rcvQuotes.setAdapter(adapter);
Then in your delete you can just do 然后在删除中,您可以
private void deleteAtIndex(int index){
myQuoteList.remove(index);
adapter.notifyDataSetChanged();
}
//make a interface. //创建一个接口。
public interface fragmentCallback{
boolean onQuoteDeleted(Quote deleteQuote);
}
have your activity implement the interface: 让您的活动实现该接口:
myActivity implements fragmentCallback{
public boolean onQuoteDeleted(Quote deletedQuote){
if(myQuotelist.contains(deleteQuote){
myQuoteList.remove(deleteQuote);
adapter.notifyDataSetChanged();
}
}
}
then in fragment simpley 然后简单地
myFragment.setQuote(selectedQuote);
inside of fragment just do: 片段里面只是做:
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mFragmentCallback = (IFragmentCallback) context;
}catch (Exception ex){
A35Log.e(TAG, "Parent Context does not implement fragmentCallback");
}
}
public void setQuote(Quote showQuote){
mSelectedQuote = showQuote;
}
btnDelete_onClick(){
if(mFragmentCallback != null){
mFragmentCallback.onDeleteQuote(mSelectedQuote);
}
}
you can handle it by selected index of the row, or by last index, or first index, or you can add a long touch listener or trash can to the row item. 您可以通过选择行的索引,最后一个索引或第一个索引来处理它,也可以在行项目中添加长按监听器或垃圾桶。 how you get the index is up to you.
如何获取索引取决于您。
That's it, that is all there is to it. 就是这样,仅此而已。
Arraylist.remove(index)
recycleradapter.notifydatasetchanged()
Get the index
of clicked item eg index = 4 list of quote eg quotesList. 获取被点击项目的
index
,例如index = 4个引用列表,例如quotesList。
quotesList.remove(index);
adapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.