从适配器中删除recyclerview项目

时间:2017-04-10 20:48:35

标签: android android-fragments android-recyclerview android-volley notifydatasetchanged

我有两个标签(片段),NewOrders和FinishedOrders,我通过Volley请求填充订单,现在New Orders选项卡中的每个项目都有SwipeLayout,显示可点击的文本视图,订单完成,并将其移动到其他选项卡(后端的东西..),我完美地工作,

问题是当我点击完成后,一旦请求成功发送,Recyclerview就不会更新,我必须进行pull-to-refresh以便更新..!它似乎很容易解决,但问题是处理在适配器内部的onBindView方法内完成的swipelayout监听器。!!根据我使用的库(我猜),这是唯一可以访问它的地方..!另一方面,刷新和填充列表发生在NewOrder选项卡片段中..! 那么如何在点击后将项目从列表中删除并更新..?!

任何想法..!?

我的适配器类+ ViewHolder

  

注意:由于SwipeLayout库的接口

,因此需要在适配器中实现的方法
 public class OrdersDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    implements SwipeAdapterInterface, SwipeItemMangerInterface {

protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);

public Context context;
ArrayList<OrderPresenter> orders;


public OrdersDataAdapter(ArrayList<OrderPresenter> orders, Context context) {
    this.orders = orders;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_card, parent, false);

    return new NewOrderVH(v);

}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    final OrderPresenter order = this.orders.get(position);
    final NewOrderVH vh1 = (NewOrderVH) holder;

    vh1.setData(orders.get(position));
    mItemManger.bindView(vh1.itemView, position);
    vh1.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
    vh1.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, 
    vh1.swipeLayout.findViewById(R.id.bottom_wrapper));


    if (order.isFinished()) {
        vh1.swipeLayout.setSwipeEnabled(false);
        vh1.setBadge("DONE");              
        vh1.setBadgeColor(order.getBadgeColor());

    } else {

        vh1.finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // get the clicked item position?
                final int position = vh1.getAdapterPosition();

          // these responsible for the request which make the order finished
                OrderPresenter order = orders.get(position);
                OrderRepository.setOrderFinURL(order.getID());
                OrderRepository.FinishOrder(order.getID(), context);

        /*the commented three lines below didn't help with the problem*/ 
                 // notifyItemChanged(position);
                 // notifyItemRemoved(position);
                 // notifyDataSetChanged();*/

               order.setStatus(order.getStatusText(Order.FINISHED));

            }
        });
    }


}


@Override
public int getItemCount() {
    return orders.size();
}


public class NewOrderVH extends RecyclerView.ViewHolder {

    SwipeLayout swipeLayout;
    private TextView finish;
    private CardView orderCard;
    TextView Badge;
    private ImageView cusPic;
    private TextView cusName;
    private TextView CusAdress;
    private TextView vendorsNum;
    private TextView itemsNum;
    private TextView time;
    private TextView emptyView;


    public NewOrderVH(View itemView) {
        super(itemView);

        Badge = (TextView) itemView.findViewById(R.id.badge);
        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
        finish = (TextView) itemView.findViewById(R.id.finish);
        orderCard = (CardView) itemView.findViewById(R.id.OrderCard);
        cusPic = (ImageView) itemView.findViewById(R.id.cusPic);
        cusName = (TextView) itemView.findViewById(R.id.cusName);
        CusAdress = (TextView) itemView.findViewById(R.id.CusAdress);
        vendorsNum = (TextView) itemView.findViewById(R.id.vendorsNum);
        itemsNum = (TextView) itemView.findViewById(R.id.itemsNum);
        time = (TextView) itemView.findViewById(R.id.time);
        emptyView = (TextView) itemView.findViewById(R.id.empty_view);


        orderCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), OrderDetails.class);
                v.getContext().startActivity(intent);
            }
        });
    }


    public void setData(final OrderPresenter data) {

        time.setText(data.getOrderTime());
        cusName.setText(data.getFullName());
        vendorsNum.setText(data.getVendorsCount());
        itemsNum.setText(data.getItemsCount());
        CusAdress.setText(data.getFullAddress());
        Picasso.with(context).load(data.getCustomerPicture()).into(cusPic);

    }

    public void setBadgeColor(int drawable) {
        this.Badge.setBackgroundResource(drawable);
    }

    public void setBadge(String badge) {
        this.Badge.setText(badge);
    }


}


@Override

public int getSwipeLayoutResourceId(int position) {

    return R.id.swipe;
}

@Override
public void openItem(int position) {

}

@Override
public void closeItem(int position) {

}

@Override
public void closeAllExcept(SwipeLayout layout) {

}

@Override
public void closeAllItems() {

}

@Override
public List<Integer> getOpenItems() {
    return null;
}

@Override
public List<SwipeLayout> getOpenLayouts() {
    return null;
}

@Override
public void removeShownLayouts(SwipeLayout layout) {

}

@Override
public boolean isOpen(int position) {
    return false;
}

@Override
public Attributes.Mode getMode() {
    return null;
}

@Override
public void setMode(Attributes.Mode mode) {

}


  }

我的NewOrder片段

  

注意:FinishedOrders选项卡(片段)与新订单的作用相同,但过滤当前的完成状态。

public class NewOrdersTab extends Fragment {

RecyclerView recyclerView;
OrdersDataAdapter adapter;
private SwipeRefreshLayout swiperefresh;
private TextView emptyView;
ArrayList<OrderPresenter> modelData;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.new_orders_tab_frag, container, false);

    modelData = new ArrayList<>();
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    swiperefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);

    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    swiperefresh.setColorSchemeResources(R.color.colorPrimary, R.color.color_error, R.color.colorInfo);
    adapter = new OrdersDataAdapter(modelData, getActivity());
    emptyView = (TextView) rootView.findViewById(R.id.empty_view);

    recyclerView.setAdapter(adapter);
    adapter.setMode(Attributes.Mode.Single);

    OrderRepository.fetchOrders("awaiting-shipment", getActivity(), new DataFetch() {
        @Override
        public void onResponse(ArrayList<OrderPresenter> data) {
            swiperefresh.setRefreshing(true);

            if (data.size() != 0) {
                swiperefresh.setRefreshing(true);

                emptyView.setVisibility(View.GONE);
                modelData.clear();
                modelData.addAll(data);
                adapter.notifyDataSetChanged();

            } else {

                emptyView.setVisibility(View.VISIBLE);
                emptyView.setText(getString(R.string.No_New_Orders));
            }
            swiperefresh.setRefreshing(false);

        }
    });

    return rootView;
}

  }

1 个答案:

答案 0 :(得分:2)

我想通了,我在发出请求后刚刚添加了这两行......!

 orders.remove(position);
 notifyItemRemoved(position);
 //notifyDataSetChanged(position);