简体   繁体   English

从活动中删除RecyclerView适配器项

[英]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.

相关问题 如何从 RecyclerView 适配器检索项目计数到另一个活动(不是父活动)? - How to retrieve item count from RecyclerView adapter into another activity(not parent activity)? 从活动中获取数据到 RecyclerView 适配器? - Getting data from activity into RecyclerView adapter? 从RecyclerView适配器类调用Activity中的方法 - Calling methods in Activity from RecyclerView adapter class 从 recyclerview 适配器调用活动方法 - Call activity method from recyclerview adapter 无法从活动中调用RecyclerView适配器的方法 - Cannot call method of RecyclerView adapter from an activity 从活动到RecyclerView适配器访问变量 - Accessing a Variable from an Activity to a RecyclerView Adapter 从 recyclerview 适配器开始新活动 - Starting new activity from recyclerview adapter 如何在没有单击的情况下从recyclerview适配器中删除所需的项目? 或从清单 - How to remove desired item from recyclerview adapter without Clicked ? or from List 使用新创建的项目返回到以前的活动时更新 recyclerView 适配器 - Update recyclerView Adapter on return to previous activity with newly created item 如何将一个值从 recyclerview 适配器传递给它对应的 Activity? - How to Pass a value from recyclerview adapter to it's corresponding Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM