8

Tôi có Fragment với SwipeRefreshLayout và sau đó là RecyclerView làm trẻ em. Khi Fragment được cam kết, nó bắt đầu nói chuyện với một máy chủ để lấy một số dữ liệu và cư trú một CustomAdapter mà sẽ phục vụ RecyclerView.SwipeRefreshLayout được ẩn trên ô trống RecyclerView

Người dùng có thể làm mới nội dung bằng cách vuốt xuống hoặc nhấn một nút trên ActionBar. Trong trường hợp sau, tôi gọi theo cách thủ công swipeRefreshLayout.setRefreshing(true). Vì vậy, không có rắc rối cho đến bây giờ.

Sự cố phát sinh khi tôi gọi trạng thái làm mới theo cách thủ công trong lần tải đầu tiên của RecyclerView (onStart()). Chỉ báo tiến trình không hiển thị và tôi giả định vì RecyclerView vẫn còn trống vì yêu cầu một vài giây để truy xuất tất cả dữ liệu ...

Tôi để lại cho bạn một số mã.

đang Fragment: Mã

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> 

Điều gì sẽ là cách tốt nhất để giải quyết tình trạng này? Thêm lần giả mạo thứ hai SwipeRefreshLayout? Ngay lập tức buộc một adapter rỗng?

Trả lời

0

Bạn cần gọi phương thức đo trước khi gọi setRefreshing(true).
Hãy xem: SwipeRefreshLayout no animation on fragment creation. Có lẽ nó có thể hữu ích.

+0

Cảm ơn bạn. Tôi không biết điều đó. Để chắc chắn nó là một giải pháp tốt hơn nhiều so với gọi 'setProgressViewOffset()' – Emanuele

11

Tôi gặp sự cố với SwipeRefreshLayout không hoạt động với trình tái chế trống, vì vậy tôi đã tạo một bộ điều hợp trống rất đơn giản.

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; 
    } 
} 
Các vấn đề liên quan