Last active
January 29, 2020 17:00
-
-
Save pboos/4535406 to your computer and use it in GitHub Desktop.
Sample code for ViewPager + 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
<?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.ViewPager> |
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> |
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
public CharSequence getPageTitle(int position) { | |
return "Page " + (position + 1); | |
} |
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.activity_main); | |
ViewPager viewPager = (ViewPager) findViewById(R.id.pager); | |
viewPager.setAdapter(new SampleFragmentPagerAdapter()); | |
} | |
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { | |
final int PAGE_COUNT = 5; | |
public SampleFragmentPagerAdapter() { | |
super(getSupportFragmentManager()); | |
} | |
@Override | |
public int getCount() { | |
return PAGE_COUNT; | |
} | |
@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; | |
} | |
} | |
} |
Simple and concise. Just what I needed, thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have copied and pasted in my eclipse al codes tat u have mentioned above.... but i didnt get error... but it is saying "unfortunately, viewpagerexample has stopped".. cud u please help me brother?????