8

FragmentSwipeRefreshLayout을 넣은 다음 RecyclerView을 자식으로 사용하고 있습니다. Fragment가 커밋되면 일부 데이터를 검색하고 RecyclerView를 제공 할 CustomAdapter를 채우기 위해 서버와 통신하기 시작합니다.빈 RecyclerView에 SwipeRefreshLayout이 표시되지 않습니다.

사용자는 동작 막대에서 아래로 스 와이프하거나 버튼을 눌러 내용을 새로 고칠 수 있습니다. 후자의 경우에는 수동으로 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을 추가 하시겠습니까? 즉시 빈 어댑터를 넣으시겠습니까?

답변

0

setRefreshing(true)으로 전화하기 전에 측정 방법을 호출해야합니다.
보기 : SwipeRefreshLayout no animation on fragment creation. 도움이 될 수도 있습니다.

+0

감사합니다. 나는 그것을 몰랐다. 'setProgressViewOffset()'을 호출하는 것보다 훨씬 좋은 해결책이라는 것을 확실히하십시오. – Emanuele

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; 
    } 
}