2016-10-13 10 views
0

RecyclerViewに追加しようとしています。それ、どうやったら出来るの?これは私が持っているコードですが、それは動作していない:スワイプを実装してRecyclerViewで更新する

public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
         Bundle savedInstanceState) { 
    View consultantView = inflater.inflate(R.layout.fragment_consultant, container, false); 

    swipeContainer = (SwipeRefreshLayout) consultantView.findViewById(R.id.consultant_recyclerview); 
    retrieveConsultantList(); 
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      swipeContainer.setRefreshing(false); 
      retrieveConsultantList(); 
      practiceSpinner.setAdapter(consultantListAdapter); 
      consultantListAdapter.notifyDataSetChanged(); 
     } 
    }); 
} 

そして、私のリサイクルコード:

private void setUpConsultantRecyclerView(List<Consultant> consultantList) { 
    ConsultantRecylerViewAdapter consultantRecylerViewAdapter = new ConsultantRecylerViewAdapter(getContext(), consultantList); 
    consultantRecyclerView.setAdapter(consultantRecylerViewAdapter); 

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); 
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    consultantRecyclerView.setLayoutManager(linearLayoutManager); 

    consultantRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
} 

私はこれを理解助けてください。

+0

を助けることができますか?、これは変更してください –

+0

'retrieveConsultantList()'は何をしますか?何がうまくいきませんか?それは更新されませんか?それとも、爽やかなアニメーションが表示されないのですか? – Bryan

+0

retrieveConsultant()はサーバーから自分のリストを読み込みます。どのデータもロードしていない – Fuluza

答えて

2

うまくいけば、リストを更新した後まで、 `` setRefreshing(偽)の移動についてのあなたにどのように

//move adapter to global variable 
ConsultantRecylerViewAdapter consultantRecylerViewAdapter ; 

public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
        Bundle savedInstanceState) { 
View consultantView = inflater.inflate(R.layout.fragment_consultant, container, false); 
    swipeContainer = (SwipeRefreshLayout) consultantView.findViewById(R.id.consultant_recyclerview); 

    //move to here 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); 
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    consultantRecyclerView.setLayoutManager(linearLayoutManager); 

    //declare list first 
    consultantList = new ArrayList<>(); 
    consultantRecylerViewAdapter = new ConsultantRecylerViewAdapter(getContext(), consultantList); 
    consultantRecyclerView.setAdapter(consultantRecylerViewAdapter); 

    retrieveConsultantList(); 

    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      swipeContainer.setRefreshing(false); 
      retrieveConsultantList(); 
      practiceSpinner.setAdapter(consultantListAdapter); 
      consultantListAdapter.notifyDataSetChanged(); 
     } 
    }); 
} 

private void retrieveConsultantList(){ 
    //put this code when finish load data from server 
    setUpConsultantRecyclerView(consultantListFromServer) 
} 

//this to refresh your RecyclerView 
private void setUpConsultantRecyclerView(List<Consultant> consultantList) { 
    //clear old list 
    consultantList.clear(); 
    //add new collection to list 
    consultantList.addAll(consultantList); 
    //refresh adapter 
    consultantRecyclerView.notifyDataSetChanged(); 
}