Last active
October 11, 2018 12:48
-
-
Save jesselima/13629751678b3368e4ecb2f2ba6e3920 to your computer and use it in GitHub Desktop.
A simple implementation of ViewPager, Tabs and Fragments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context="com.udacity.popularmovies.activities.MainActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:visibility="visible" | |
android:orientation="vertical"> | |
<android.support.design.widget.TabLayout | |
android:id="@+id/tabs" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:tabMode="fixed" /> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/viewpager" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="@dimen/small_layout_margin" | |
android:layout_marginLeft="@dimen/default_layout_margin" | |
android:layout_marginRight="@dimen/default_layout_margin"/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.udacity.popularmovies.adapters; | |
import android.content.Context; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import com.udacity.popularmovies.R; | |
import com.udacity.popularmovies.fragments.MovieDetailsFragment; | |
import com.udacity.popularmovies.fragments.MovieReviewsFragment; | |
import com.udacity.popularmovies.fragments.MovieVideosFragment; | |
/** | |
* Created by jesse on 13/09/18. | |
* This is a part of the project adnd-popular-movies. | |
*/ | |
public class DetailsFragmentPagerAdapter extends FragmentPagerAdapter { | |
private Context mContext; | |
private static final Integer PAGE_NUMBER = 3; | |
/** | |
* Create a new {@link DetailsFragmentPagerAdapter} object. | |
* | |
* @param context is the context of the app | |
* @param fragmentManager is the fragment manager that will keep each fragment's state in the adapter | |
* across swipes. | |
*/ | |
public DetailsFragmentPagerAdapter(Context context, FragmentManager fragmentManager) { | |
super(fragmentManager); | |
mContext = context; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
if (position == 0) { | |
return new MovieDetailsFragment(); | |
} else if (position == 1){ | |
return new MovieReviewsFragment(); | |
} else { | |
return new MovieVideosFragment(); | |
} | |
} | |
@Override | |
public int getCount() { | |
return PAGE_NUMBER; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
if (position == 0){ | |
return mContext.getString(R.string.tab_details); | |
} else if (position == 1){ | |
return mContext.getString(R.string.tab_reviews); | |
} else { | |
return mContext.getString(R.string.tab_videos); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.udacity.popularmovies.activities; | |
import android.support.design.widget.TabLayout; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.udacity.popularmovies.R; | |
import com.udacity.popularmovies.adapters.DetailsFragmentPagerAdapter; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ViewPager viewPager = findViewById(R.id.viewpager); | |
DetailsFragmentPagerAdapter fragmentPagerAdapter = new DetailsFragmentPagerAdapter(this, getSupportFragmentManager()); | |
viewPager.setAdapter(fragmentPagerAdapter); | |
TabLayout tabLayout = findViewById(R.id.tabs); | |
tabLayout.setupWithViewPager(viewPager); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment