如何在包含ListFragments的ViewPager上实现Android SwipeRefreshLayout

时间:2015-11-06 02:41:52

标签: android android-fragments android-viewpager android-listfragment swiperefreshlayout

我有一个标签式活动,每个标签中包含一个ListFragment(Tabs with ListFragments image)。在主要布局中,我实现了一个SwipeRefreshLayout,它执行网络任务并同步两个片段所依赖的一些数据库。

我发现我可以滚动到ListFragment列表的底部,但是我无法滚动回列表顶部而不会刷新SwipeRefreshLayout。是否可以修复我的活动,以便只有在我到达列表顶部后才会刷新,这将如何实现?

我按照this教程实施了ViewPager with Lists。

我的代码 -

主XML布局 -

public class MainActivity extends ActionBarActivity implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
    // Handles ViewPager
    private MyPageAdapter fPageAdapter;
    private ViewPager fViewPager;
    private SwipeRefreshLayout fSwipeRefreshLayout;

    // Handles Tabs
    private TabHost fTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fViewPager = (ViewPager)findViewById(R.id.viewpager);

        // Tab Initialisation
        initialiseTabHost();

        // Fragments and ViewPager Initialisation
        List<Fragment> fragments = getFragments();
        fPageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        fViewPager.setAdapter(fPageAdapter);
        fViewPager.setOnPageChangeListener(MainActivity.this);

        fSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
        fSwipeRefreshLayout.setOnRefreshListener(swipeRefreshListener);
    }

    /**
     *  Swipe Refresh Listener
     */
    private SwipeRefreshLayout.OnRefreshListener swipeRefreshListener = new SwipeRefreshLayout.OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            fSwipeRefreshLayout.setRefreshing(false);

            /**
             * Calls Async Task to perform network
             * operation and update database.
             */
        }
    };


    /**
     *  Other Functions Removed
     */


    /** Fragment Construction */

    /**
     * @brief create a list of default fragments to display
     * @return List Fragments - containing default fragments
     */
    private List<Fragment> getFragments()
    {
        String[] numbers_text = new String[] { "one", "two", "three", "four",
                "five", "six", "seven", "eight", "nine", "ten", "eleven",
                "twelve", "thirteen", "fourteen", "fifteen" };

        String[] colours = new String[] { "RED", "BLACK", "BLUE", "WHITE", "GREEN", "BROWN",
                "GREY", "YELLOW", "PINK", "PURPLE", "ORANGE", "VIOLET", "INDIGO"};

        List<Fragment>fList = new ArrayList<Fragment>();

        MyListFragment f1 = MyListFragment.newInstance(numbers_text);
        MyListFragment f2 = MyListFragment.newInstance(colours);
        fList.add(f1);
        fList.add(f2);

        return fList;
    }
}

MainActivity -

{{1}}

0 个答案:

没有答案