-
-
Save blackcj/5960698 to your computer and use it in GitHub Desktop.
ViewPager and PagerTabStrip
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 ch.pboos.android.sample.viewpager; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class MainActivity extends FragmentActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
ViewPager viewPager = (ViewPager) findViewById(R.id.pager); | |
viewPager.setAdapter(new SampleFragmentPagerAdapter()); | |
} | |
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { | |
private static final String[] TITLES = new String[] { | |
"Page 1", | |
"Page 2", | |
"Page 3" | |
}; | |
public static final int NUM_TITLES = TITLES.length; | |
public SampleFragmentPagerAdapter() { | |
super(getSupportFragmentManager()); | |
} | |
@Override | |
public int getCount() { | |
return NUM_TITLES; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return TITLES[position]; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return PageFragment.create(position + 1); | |
} | |
} | |
public static class PageFragment extends Fragment { | |
public static final String ARG_PAGE = "ARG_PAGE"; | |
private int mPage; | |
public static PageFragment create(int page) { | |
Bundle args = new Bundle(); | |
args.putInt(ARG_PAGE, page); | |
PageFragment fragment = new PageFragment(); | |
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_page, container, false); | |
TextView textView = (TextView) view; | |
textView.setText("Fragment #" + mPage); | |
return view; | |
} | |
} | |
} |
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"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" /> |
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"?> | |
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/pager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<android.support.v4.view.PagerTabStrip | |
android:id="@+id/pager_header" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="top" | |
android:background="#000" | |
android:paddingBottom="4dp" | |
android:paddingTop="4dp" | |
android:textColor="#fff" /> | |
</android.support.v4.view.ViewPager> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment