8

我有FragmentSwipeRefreshLayout然後RecyclerView作爲孩子。 當Fragment被提交時,它開始與服務器通信以獲取一些數據並填充將爲RecyclerView提供服務的CustomAdapter。SwipeRefreshLayout隱藏在空的RecyclerView

用戶可以通過向下滑動或按下ActionBar上的按鈕來刷新內容。在後一種情況下,我手動撥打swipeRefreshLayout.setRefreshing(true)。所以到現在爲止沒有麻煩。

在第一次加載RecyclerView(onStart())期間手動調用刷新狀態時出現問題。 進度指示器沒有出現,我假設因爲RecyclerView仍然是空的,因爲它需要幾秒鐘來檢索所有數據...

我給你留下一些代碼。

片段代碼:

public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { 


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

     View mView = inflater.inflate(R.layout.fragment_stop, container, false); 
     mRecyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView1); 
     mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), 
      DividerItemDecoration.VERTICAL_LIST)); 
     mRecyclerView.setHasFixedSize(true); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 

     //SwipeRefreshLayout retrieved and configured 
     swipeLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_container); 
     swipeLayout.setOnRefreshListener(this); 
     swipeLayout.setColorSchemeResources(
      R.color.primaryColor, 
      R.color.cyan_500, 
      R.color.light_green_500, 
      R.color.amber_500); 

     return mView; 


    } 

    ... 
    @Override 
     public void onStart() { 
      super.onStart(); 
      executeSchedule(); //First execution and data loading. 
     } 
     ... 

    @Override 
    public void onRefresh() { 
     executeSchedule(); 
    } 
    ... 
    public void executeSchedule() { 
     new Schedule().execute(); 
    } 



    private class Schedule extends AsyncTask<Void, Void, Void> { 

     ... 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      if(!swipeLayout.isRefreshing()) { 
       swipeLayout.setRefreshing(true); //set refresh state 
      } 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
     ... 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      adap = new CustomAdapter(getActivity(), ...); 
      mRecyclerView.setAdapter(adap); 
      swipeLayout.setRefreshing(false); 
     } 

} 

XML代碼:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/section_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <android.support.v4.widget.SwipeRefreshLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/swipe_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recyclerView1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:paddingLeft="4dp" 
       android:paddingRight="4dp" 
       android:paddingBottom="2dp" 
       android:layout_marginTop="5dp" 
       android:divider="#e0e0e0" 
       tools:context=".MyFragment" /> 

     </android.support.v4.widget.SwipeRefreshLayout> 
    </RelativeLayout> 
</FrameLayout> 

什麼將是解決這種情況的最佳做法? 添加第二個虛擬SwipeRefreshLayout?立即強制一個空的適配器?

回答

11

我有一個問題SwipeRefreshLayout不能使用空的回收站,所以我創建了一個非常簡單的空適配器。

public class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return null; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    } 

    @Override 
    public int getItemCount() { 
     return 0; 
    } 
} 
相關問題