viewPager中的ListFragment不会在某些页面中显示数据,并且在某些页面中运行良好

时间:2017-10-26 08:43:34

标签: java android android-viewpager android-listfragment

我有一个ListFragment,它从互联网上获取json数据并显示在listView.And我在viewPager中重用这个片段。它加载数据并显示在前两页但仍保持在第三页中加载。大多数页面都可以工作但是中间的一些页面不起作用。我不明白的是我在所有页面上使用相同的片段但在某些页面中不起作用。我通过创建newInstance并传递url来从中获取数据来重用相同的片段。如果我提供的话更多的代码让观众不知所措,所以我想这就足够了。在ListFragments中获取并显示列表时,我应该遵循哪些规则。我做错了什么?

我的适配器:

public class FragmentPageAdapter extends FragmentPagerAdapter {

    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                Log.d("Adapter","Case 0 called");
                return new LisViewFragment2();

            case 1:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=al-jazeera-english&sortBy=top&apiKey=my_key");

            case 2:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=associated-press&sortBy=top&apiKey=my_key");

            case 3:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=my_key");

            case 4:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=cnn&sortBy=top&apiKey=my_key");

            case 5:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=my_key");

            case 6:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=independent&sortBy=top&apiKey=my_key");

            case 7:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=metro&sortBy=top&apiKey=my_key");

            case 8:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=mirror&sortBy=top&apiKey=my_key");

            case 9:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=newsweek&sortBy=top&apiKey=my_key");

            case 10:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=new-york-magazine&sortBy=top&apiKey=my_key");

            case 11:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=reddit-r-all&sortBy=top&apiKey=my_key");

            case 12:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=reuters&sortBy=top&apiKey=my_key");

            case 13:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-guardian-uk&sortBy=top&apiKey=my_key");

            case 14:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-hindu&sortBy=top&apiKey=my_key");

            case 15:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-times-of-india&sortBy=top&apiKey=my_key");

            case 16:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-new-york-times&sortBy=top&apiKey=my_key");

            case 17:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-telegraph&sortBy=top&apiKey=my_key");

            default:
                return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=usa-today&sortBy=top&apiKey=my_key");


        }

    }

    @Override
    public int getCount() {
        return 19;
    }


}

1 个答案:

答案 0 :(得分:0)

覆盖 getItemPosition 并返回POSITION_NONE

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}