2016-09-17 51 views
0

我的活動包括recyclerview,其數據我設置爲api調用的響應。我在api調用的成功響應中添加了以下代碼。問題是當我點擊一個按鈕刷新列表,然後新的列表顯示在舊列表頂部。結果當我點擊列表項目時,多個活動打開,因爲事件也是由當前列表後面的列表拍攝的。爲什麼會發生這種情況?我只想讓我的列表在點擊刷新按鈕時刷新新列表。如何解決這個問題?Recyclerview刷新列表顯示在舊列表頂部的新列表

@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(...); 
         } 
        })); 
+0

明確適配器項目分配給recyclerView。添加所有新項目 - >通知 – XxGoliathusxX

回答

0

可以初始化適配器並在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(...); 
       } 
      }));