带有两个单独ListView的TabLayout片段

时间:2016-02-24 22:58:21

标签: android listview android-fragments

我有一个TabLayout的Activity。对于内容,我有一个片段;这个片段包含一个ListView。我希望为两个选项卡使用相同的片段 - 但每个都有不同的ListView。完成此任务的最佳方法是什么?这些列表都包含使用其JSON API从RottenTomatoes检索的数据。

RecentReleasesFragment.java

public class RecentReleasesFragment extends Fragment {

    private ArrayAdapter<String> mMovieAdapter;
    private ArrayAdapter<String> mDVDAdapter;
    private int mPage;
    public static final String ARG_PAGE = "ARG_PAGE";

    /**
     * Constructor for the RecentReleasesFragment
     * @param page fragment is on
     * @return newly created fragment
     */
    public static RecentReleasesFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        RecentReleasesFragment fragment = new RecentReleasesFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recent_releases, container, false);

        switch(mPage) {
            case 1:
                FetchMovies taskMovie = new FetchMovies();
                taskMovie.execute();
                recentMoviesList(view);
            case 2:
                FetchDVDs taskDVD = new FetchDVDs();
                taskDVD.execute();
                recentDVDsList(view);
        }
        return view;
    }

    public void recentMoviesList(View v) {
        ListView mRecentMoviesList = (ListView) v.findViewById(R.id.listview_recent_movies);
        mMovieAdapter = new ArrayAdapter<>(
                getActivity(),
                R.layout.list_item_film,
                R.id.list_item_film,
                new ArrayList<String>());
        mRecentMoviesList.setAdapter(mMovieAdapter);
    }

    public void recentDVDsList(View v) {
        ListView mRecentDVDsList = (ListView) v.findViewById(R.id.listview_recent_movies);
        mDVDAdapter = new ArrayAdapter<>(
                getActivity(),
                R.layout.list_item_film,
                R.id.list_item_film,
                new ArrayList<String>());
        mRecentDVDsList.setAdapter(mDVDAdapter);
    }
    //adds items to mMovieAdapter
    public class FetchMovies extends AsyncTask<String, Void, String[]> { . . . 
        @Override
        protected void onPostExecute(String[] result) {
            if (result != null) {
                mMovieAdapter.clear();
                for (String movie : result) {
                    mMovieAdapter.add(movie);
                }
            }
        }
    }


    //adds items to mDVDAdapter
    public class FetchDVDs extends AsyncTask<String, Void, String[]> { . . . }
}

fragment_recent_releases.xml

<ListView android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:divider="@null"
    android:id="@+id/listview_recent_movies"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>

RecentReleasesActivity.java

public class RecentReleasesActivity extends AppCompatActivity {

    @Bind(R.id.toolbar) Toolbar toolbar;

    private SessionManager mSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent_releases);
        ButterKnife.bind(this);

        this.mSession = SessionManager.getInstance(getApplicationContext());

        toolbar.setTitle(recentReleases);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new RecentReleasesFragmentPagerAdapter(getSupportFragmentManager(),
                RecentReleasesActivity.this));

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.recent_releases_tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

activity_recent_releases.xml

<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context=".activities.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/abl_top"
            android:elevation="4dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_alignParentTop="true"
                android:minHeight="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme">
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/recent_releases_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </android.support.design.widget.AppBarLayout>

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

    <include layout="@layout/content_recent_releases" />
</android.support.design.widget.CoordinatorLayout>

0 个答案:

没有答案