![](/img/trans.png)
[英]How to retrieve item count from RecyclerView adapter into another activity(not parent activity)?
[英]Remove RecyclerView adapter item from Activity
我有Quote List类QuoteListFragment。 我在从服务器这样的适配器中加载数据的地方
private ArrayList<Quote> quotes;
quotes = response.body();
NewQuoteAdapter adapter = new NewQuoteAdapter(getContext(), response.body());
mQuoteList.setAdapter(adapter);
和称为NewQuoteAdapter
RecyclerView Adapter
我正在使用QuoteViewFragment
详细显示Item。 我在此片段中实现了一个名为“删除”的按钮,我想给用户一个机会,从片段中删除该引号,以便当用户返回列表时,它从列表中消失。 我不知道如何实现这一目标。 让我知道是否有人可以给我解决方案。 谢谢
好吧,您有一个列表,一个适配器和一个recyclerview
ArrayList<String> myQuoteList = new ArrayList<String>();
MyCustomAdapter adapter = new MyCustomAdapter(myQuoteList);
rcvQuotes.setAdapter(adapter);
然后在删除中,您可以
private void deleteAtIndex(int index){
myQuoteList.remove(index);
adapter.notifyDataSetChanged();
}
//创建一个接口。
public interface fragmentCallback{
boolean onQuoteDeleted(Quote deleteQuote);
}
让您的活动实现该接口:
myActivity implements fragmentCallback{
public boolean onQuoteDeleted(Quote deletedQuote){
if(myQuotelist.contains(deleteQuote){
myQuoteList.remove(deleteQuote);
adapter.notifyDataSetChanged();
}
}
}
然后简单地
myFragment.setQuote(selectedQuote);
片段里面只是做:
@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);
}
}
您可以通过选择行的索引,最后一个索引或第一个索引来处理它,也可以在行项目中添加长按监听器或垃圾桶。 如何获取索引取决于您。
就是这样,仅此而已。
Arraylist.remove(index)
recycleradapter.notifydatasetchanged()
获取被点击项目的index
,例如index = 4个引用列表,例如quotesList。
quotesList.remove(index);
adapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.