简体   繁体   English

如何使用翻新或凌空删除回收者视图项目?

[英]how to delete the recycler view item using retrofit or volley?

I want to delete the recyclerview item using retrofit or volley network library. 我想使用改型或排球网络库删除recyclerview项目。 Please post the code or example? 请发布代码或示例?

Firstable you do not have to remove the items on the RecyclerView. 首先,您不必删除RecyclerView上的项目。 The main idea is provide the List of item, set it in the Adapter and load again the RecyclerView on the UI. 主要思想是提供项目列表,在适配器中进行设置,然后再次在UI上加载RecyclerView。 If you have a RestCall using Retrofit all you have to do is: 如果您有一个使用Retrofit的RestCall,您要做的就是:

Create an Update method and call it every time you need to show the changes on the UI. 创建一个Update方法,并在每次需要在UI上显示更改时调用它。 This method will do the RetrofitCall and it will give the List of items you need to pass to the adapter using a custom listener with the return onSuccess. 此方法将执行RetrofitCall,并将使用返回onSuccess的自定义侦听器给出您需要传递到适配器的项目列表。

private RecyclerAdapter updateRecycler() {
    getItemsFromApi(new RestApiListener() {
        @Override
        public void onSuccess(List<Items> items) {
            recyclerAdapter = new RecyclerAdapter(this, items);
            recyclerView.setAdapter(recyclerAdapter);
        }

        @Override
        public void onError(String message) {
            showError(message);
        }
    });
}

Here is how to do the CallBack in Retrofit with the listener 这是与侦听器一起进行翻新的回调方法

public Callback<Items> getItemsFromApi(final RestApiListener listener) {
    return new Callback<Items>() {
        @Override
        public void onResponse(Call<Items> call, Response<Items> response) {
            switch (response.code()) {
                case 200:
                    listener.onSuccess(response.body().getItems());
                    break;
                default:
                    listener.onError("Error " + response.message());
                    break;
            }
        }

        @Override
        public void onFailure(Call<Items> call, Throwable t) {
            listener.onError(t.getMessage().toString());
        }
    };
}

public interface RestApiListener  {
    void onSuccess(List<Items> items);

    void onError(String s);
}

Also here is an example code . 这也是示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM