android - FragmentPagerAdapter仅显示所有视图中的第一个片段

时间:2017-03-12 02:26:59

标签: java android android-fragments android-viewpager fragmentpageradapter

我想在ViewPager中显示片段,但它只显示视图选项卡中的第一个片段。显示的唯一片段是在getItem中的位置0返回的片段(0方法 - 此片段显示在后续视图中。

FragmentPagerAdapter:

    public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {


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

    @Override
    public Fragment getItem(int position) {

        if (position == 0) {
            return  new WorldFragment();
        } else if (position == 1) {
            return new PoliticsFragment();
        } else if (position == 2) {
            return new TechnologyFragment();
        } else if (position == 3) {
            return new ScienceFragment();
        } else if (position == 4) {
            return new SportsFragment();
        } else if (position == 5) {
            return new FoodFragment();
        } else if (position == 6) {
            return new TravelFragment();
        } else if (position == 7) {
            return new MoviesFragment();
        } else if (position == 8) {
            return new FashionFragment();
        } else {
            return new OpinionFragment();
        }
    }

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

}

片段:

public class WorldFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Story>> {

public static final String LOG_TAG = WorldFragment.class.getName();
private static final String NY_TIMES_REQUEST_URL = "https://api.nytimes.com/svc/topstories/v2/world.json?api-key=<API KEY REMOVED>;
private StoryAdapter mAdapter;
private TextView mEmptyTextView;
private View rootView;


public WorldFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    rootView = inflater.inflate(R.layout.story_list, container, false);

    mAdapter = new StoryAdapter(getActivity(), new ArrayList<Story>());

    final ListView listView = (ListView) rootView.findViewById(R.id.story_list);
    mEmptyTextView = (TextView) rootView.findViewById(R.id.empty_textview);

    listView.setEmptyView(mEmptyTextView);

    listView.setAdapter(mAdapter);



    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Story currentStory = mAdapter.getItem(position);
            String url = currentStory.getmURL();
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
        }
    });


    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (isConnected) {

        LoaderManager loaderManager = getActivity().getLoaderManager();
        loaderManager.initLoader(0, null, this);

    } else {

        View loadingIndicator = rootView.findViewById(R.id.loading_indicator);
        loadingIndicator.setVisibility(View.GONE);
        mEmptyTextView = (TextView) rootView.findViewById(R.id.empty_textview);
        mEmptyTextView.setText(R.string.no_internet_connection);
    }
    return rootView;
}


@Override
public android.content.Loader<List<Story>> onCreateLoader(int i, Bundle bundle) {

    return new StoryLoader(getActivity(), NY_TIMES_REQUEST_URL);

}

@Override
public void onLoadFinished(android.content.Loader<List<Story>> loader, List<Story> stories) {

    mAdapter.clear();

    View loadingIndicator = rootView.findViewById(R.id.loading_indicator);
    loadingIndicator.setVisibility(View.GONE);

    mEmptyTextView.setText(R.string.no_new_stories);

    if (stories != null && !stories.isEmpty()) {
        mAdapter.addAll(stories);
    }
}

@Override
public void onLoaderReset(android.content.Loader<List<Story>> loader) {
    mAdapter.clear();
}


@Override
public void onStop() {
    super.onStop();
}

}

ViewPager XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.topworldstories.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

列出XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/story_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="true" />

    <TextView
        android:id="@+id/empty_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone"
        tools:text="No new stories"/>

    <ProgressBar
        android:id="@+id/loading_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_centerInParent="true"/>


</LinearLayout>

我不确定导致这种情况的原因。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:0)

  

它只在视图选项卡中多次显示第一个片段

我看到两种可能性,两种都是复制粘贴错误。

1)您没有更改此网址(请注意world.json

private static final String NY_TIMES_REQUEST_URL = "https://api.nytimes.com/svc/topstories/v2/world.json?api-key=<API KEY REMOVED>";

2)您没有使用不同的布局。

rootView = inflater.inflate(R.layout.story_list, container, false);

但由于你的数据似乎是一致的,我猜测#1是真的。

如果您要在同一“片段布局”中显示10个不同的网址,则不需要 10个单独的片段文件。

例如,一个片段

public class NyTimesFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Story>> {

public static final String LOG_TAG = NyTimesFragment.class.getName();
    private static final String NY_TIMES_URL = "nyTimesURL";
    private StoryAdapter mAdapter;
    private TextView mEmptyTextView;
    private View rootView;

    public NyTimesFragment(String url) {
        Bundle b = new Bundle();
        b.putExtra(NY_TIMES_URL, url); // Pass URL here
        setArguments(b);
    }

    public NyTimesFragment() {
        // Required empty public constructor
    }

    @Override
    public android.content.Loader<List<Story>> onCreateLoader(int i, Bundle bundle) {
        // Load url here
        String url = getArguments().getString(NY_TIMES_URL);
        return new StoryLoader(getActivity(), url);
    }

您将任何NyTimes网址传递给

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    // List of NyTimes topics
    private String[] topics = { "world", "politics" };

    // !!! Do NOT store this in your app...  !!!
    private static final String API_KEY = "XXXXX";

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

    private String getURL(String apiKey, String topic) { 
        return String.format(
            "https://api.nytimes.com/svc/topstories/v2/%s.json?api-key=%s",
        topic, apiKey);
    }

    @Override
    public int getCount() {
        return topics.length; // Assuming each fragment goes to NyTimes
    }

    @Override
    public Fragment getItem(int position) {
        final String url = getURL(topics[position], API_KEY);

        // Call the other constructor
        return  new NyTimesFragment(url);
    } // done... no if statements. 

需要考虑使其更好一点的是Retrofit + Gson ...

答案 1 :(得分:0)

好的,这里的问题是我在initloader中为每个片段使用相同的加载器ID&amp;这就是加载相同数据的原因。对于每个片段,加载器ID必须是唯一的才能工作。