SwipeRefreshLayout陷入了ViewPager内的RecyclerView

时间:2015-04-28 10:08:29

标签: android android-fragments android-recyclerview

我有4个列表:带有PagerTitleStrip的ViewPager。 (每个滑动横向显示不同的列表) 里面的片段在RecylerView周围保存一个SwipeRefreshLayout,显示相关的列表信息。

向下滑动以刷新 - 刷新可见列表(所有4个正常工作)。但前3个列表刷新圈永远不会消失,但最后一个总是正确刷新并完成隐藏刷新圈。

任何想法如何解决这个问题?

fragment_main

<android.support.v4.widget.SwipeRefreshLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/fragment_joblist"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity$PlaceholderFragment" >
 <android.support.v7.widget.RecyclerView
     android:id="@+id/list"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:scrollbars="vertical"
      />

page_list

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:textColor="#fff" />
</android.support.v4.view.ViewPager>

某些来源 - setRefreshing(false)会为所有列表启用SwipeRefreshLayout,但只会在最后的第4个列表页面上停止它。

public class PlaceholderFragment extends Fragment {
    private WeakReference<OnClickListener> mClickListener = null;
    private WeakReference<OnRefreshListener> mRefreshListener = null;

    private RecyclerView mRecyclerView;
    private JobListAdapter mJobListAdapter;
    private SwipeRefreshLayout mSwipeLayout = null;
    private RecyclerView.LayoutManager mLayoutManager;

    private int selected = -1;
    private JobType mJobType;

    public static PlaceholderFragment newInstance(int sectionNumber, JobType jobType,
            OnClickListener clickListener, OnRefreshListener refreshListener) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        fragment.mJobType = jobType;
        fragment.mClickListener = new WeakReference<OnClickListener>(clickListener);
        fragment.mRefreshListener = new WeakReference<OnRefreshListener>(refreshListener);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mSwipeLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.fragment_joblist);
        mSwipeLayout.setRefreshing(false);
        mSwipeLayout.invalidate();

        if (mRefreshListener != null && mRefreshListener.get() != null)
            this.setOnRefreshListener(mRefreshListener.get());

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);

        mLayoutManager = new LinearLayoutManager(this.getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mJobListAdapter = new JobListAdapter(getJobSubset(), mJobType, this.getActivity(),
                mClickListener.get(), (MainActivity) this.getActivity());
        mJobListAdapter.setActivity((MainActivity) this.getActivity());
        mRecyclerView.setAdapter(mJobListAdapter);
        return rootView;
    }

    public void setOnRefreshListener(OnRefreshListener refreshListener) {
        mRefreshListener = new WeakReference<OnRefreshListener>(refreshListener);
        mSwipeLayout.setOnRefreshListener(mRefreshListener.get());
    }

    public void setRefreshing(final boolean b) {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(b);
            Log.e("Place ListFrag", "refreshing null");
    /*          
            mSwipeLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeLayout.setRefreshing(b);
                    mSwipeLayout.invalidate();          
                }
            });         
    */
        }
    }

    /**
     * Refresh the list with the latest content
     */
    public void refresh() {
        if (mJobListAdapter != null) {
            mJobListAdapter.setData(getJobSubset());
            mRecyclerView.destroyDrawingCache();
            mRecyclerView.invalidate();
            mJobListAdapter.notifyDataSetChanged();
        }
        setRefreshing(false);
    }
}

1 个答案:

答案 0 :(得分:0)

首先检查一下是否刷新,然后执行setRefreshing(false)

if(mSwipeRefreshLayout.isRefreshing())
     mSwipeRefreshLayout.setRefreshing(false);