简体   繁体   中英

Recyclerview refreshing list displaying new list on top of old list

My activity consists of recyclerview whose data i am setting on response of an api call.I have added below code inside the success response of an api call.The problem is when i refresh the list on clicking on a button then new list is displayed on top of old list.As a result when i click on list item multiple activities open as the event is also taken by the list behind the current list.Why is this happening?I just want that my list should refresh with new list on clicking on refresh button.How can i resolve this?

 @Override
    public void onSuccess(ResponseData responseString) {
     mAdapter = new RowAapter(responseString.getdatd(), mContext);
    mList.setAdapter(mAdapter);
     mAdapter.notifyDataSetChanged();
    mList.addOnItemTouchListener(new RecyclerItemClickListener(mContext, new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {

                            startActivity(...);
                        }
                    }));

You can initialize your adapter and assign it to recyclerView in OnCreate

private List<ResponseData> responseData = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail2);
    mAdapter = new RowAapter(responseData , mContext); 
    recyclerView = ((RecyclerView)rootView.findViewById(R.id.recyclerView));
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(mAdapter);
}

@Override
public void onSuccess(ResponseData responseString) {
  responseData.addAll(responseString.getdatd());
  mAdapter.notifyDataSetChanged();
  mList.addOnItemTouchListener(new RecyclerItemClickListener(mContext, new        RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                    startActivity(...);
                }
            }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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