Simple Example of ViewPager and simple example of ViewPager

Source: Internet
Author: User

Simple Example of ViewPager and simple example of ViewPager

This example is based on the example on the official website. It is suspected of plagiarism. However, if you write it separately, you will be more impressed.

The first is mainacitiworkflow. xml:

<LinearLayout xmlns: android = "" xmlns: tools = "" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <android. support. v4.view. viewPager android: translationZ = "10dp" android: elevation = "10dp" android: id = "@ + id/view_pager" android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "1"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "1" android: gravity = "center" android: orientation = "vertical"> <Button android: id = "@ + id/go_to_first_item" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: padding = "10dp" android: text = "@ string/first" android: textAllCaps = "false"/> <Button android: id = "@ + id/go_to_last_item" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_margin = "10dp" android: padding = "10dp" android: text = "@ string/last" android: textAllCaps = "false"/> </LinearLayout>View Code

The layout is simple, that is, adding a LinearLayout to a ViewPager.

Then the layout of items in ViewPager. The adapter used in ViewPager is FragmentStatePagerAdatper.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/text" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center_vertical | center_horizontal" android: text = "@ string/hello_world" android: textAppearance = "? Android: attr/textAppearanceMedium "/> <FrameLayout android: layout_width =" match_parent "android: layout_height =" 0dp "android: layout_weight =" 1 "> <ListView android: id = "@ android: id/list" android: layout_width = "match_parent" android: layout_height = "match_parent"/> <TextView android: id = "@ android: id/empty "android: layout_width =" match_parent "android: layout_height =" match_parent "android: text =" No items. "android Oid: textAppearance = "? Android: attr/textAppearanceMedium "/> </FrameLayout> </LinearLayout>View Code

Then the MainAcitivity code is as follows:

Package com. example. crazykids. customsizeviewpager; import android. OS. bundle; import android. support. fragment; import android. support. fragmentManager; import android. support. fragmentStatePagerAdapter; import android. support. listFragment; import android. support. v4.view. viewPager; import android. support. appCompatActivity; import android. util. log; import android. view. layoutInf Later; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. button; import android. widget. listView; import android. widget. textView; public class MainActivity extends AppCompatActivity {static final int NUM_ITEMS = 10; private ViewPager mPager; private MyAdapter mAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onC Reate (savedInstanceState); setContentView (R. layout. activity_main); mAdapter = new MyAdapter (getsuppfrfragmentmanager (); mPager = (ViewPager) findViewById (R. id. view_pager); mPager. setAdapter (mAdapter); mPager. setPageMargin (10); mPager. setPageMarginDrawable (android. r. color. background_dark); Button button = (Button) findViewById (R. id. go_to_first_item); button. setOnClickListener (new View. onClickList Ener () {public void onClick (View v) {mPager. setCurrentItem (0) ;}}); button = (Button) findViewById (R. id. go_to_last_item); button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {mPager. setCurrentItem (NUM_ITEMS-1) ;}});} public static class MyAdapter extends FragmentStatePagerAdapter {@ Override public float getPageWidth (int position) {return 0.2f;} @ Override p Ublic CharSequence getPageTitle (int position) {return position + "";} public MyAdapter (FragmentManager fm) {super (fm) ;}@ Override public Fragment getItem (int position) {return MyListFragment. getInstance (position) ;}@ Override public int getCount () {return NUM_ITEMS ;}} public static class MyListFragment extends ListFragment {private int mNum; public static MyListFragment getInstance (int po Sition) {MyListFragment fragment = new MyListFragment (); Bundle args = new Bundle (); args. putInt ("position", position); fragment. setArguments (args); return fragment;} @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mNum = getArguments () = null? 1: getArguments (). getInt ("position") ;}@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. fragment_page_item, container, false); TextView text = (TextView) view. findViewById (R. id. text); text. setText ("Fragment #" + mNum); return view ;}@ Override public void onViewCreated (View view, Bundle savedInstanceState) {super. onViewCreated (view, savedInstanceState); setListAdapter (new ArrayAdapter <String> (getActivity (), android. r. layout. simple_list_item_1, MyArrays. arrays) ;}@ Override public void onListItemClick (ListView l, View v, int position, long id) {Log. I ("FragmentList", "Item clicked:" + id );}}}View Code

Fragment inherits from ListFragment. In the layout file, the ListView id must be set to android: id/list, and a textview with the id android: id/empty is displayed, when the data of the list adapter is empty,

Fragment shows the textview.

Then there is a getPageSize () method in the FragmentStatePagerAdapter. This method is used to determine the proportion of each item in viewPager to the entire screen. setting this value appropriately allows viewpager to display multiple pages at a time.

Below is:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.