ViewPage Lesson 3 custom viewpage
Step 1:
Create four xml files: page01.xml, page02.xml, page03.xml, and page04.xml
Four sub-control interfaces as viewpage containers
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#8A2BE2" android:orientation="vertical"> <textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="page01" android:textsize="40sp"> </textview></linearlayout>Step 2: add the viewpage control and four buttons to the layout file activity_main.
Create a selector file for each button and use android: drawableTop = "@ drawable/page01_radiobutton_drawable" For details, see:
<relativelayout 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" tools:context=".MainActivity"> <android.support.v4.view.viewpager android:id="@+id/vp_viewpage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/rg_tab"> </android.support.v4.view.viewpager> <radiogroup android:id="@+id/rg_tab" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:orientation="horizontal" android:paddingbottom="3dp" android:paddingtop="3dp"> <radiobutton android:id="@+id/btn_Home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawabletop="@drawable/page01_radiobutton_drawable" android:gravity="center" android:text="HOME"> <radiobutton android:id="@+id/btn_Time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawabletop="@drawable/page02_radiobutton_drawable" android:gravity="center" android:text="Time"> <radiobutton android:id="@+id/btn_Message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawabletop="@drawable/page03_radiobutton_drawable" android:gravity="center" android:text="Message"> <radiobutton android:id="@+id/btn_Settings" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawabletop="@drawable/page04_radiobutton_drawable" android:gravity="center" android:text="Settings"> </radiobutton></radiobutton></radiobutton></radiobutton></radiogroup></relativelayout>
Step 3: Process in MainActivity:
Package com. example. viewpage_test; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. support. v4.widget. searchViewCompat. onCloseListenerCompat; import android. text. layout; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. radioGroup; import android. widget. radioGroup. onCheckedChangeListener; public class MainActivity extends Activity {private ViewPager viewpager; private List
Children; private List
Titles; private RadioGroup tab; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); viewpager = (ViewPager) findViewById (R. id. vp_viewpage); tab = (RadioGroup) findViewById (R. id. rg_tab); // The set of interfaces hosted by viewpage children = new ArrayList
(); Children. add (LayoutInflater. from (this ). inflate (R. layout. page01, null); children. add (getLayoutInflater (). inflate (R. layout. page02, null); children. add (getLayoutInflater (). inflate (R. layout. page03, null); children. add (getLayoutInflater (). inflate (R. layout. page04, null); // Title set of each interface titles = new ArrayList
(); Titles. add ("page01"); titles. add ("page02"); titles. add ("page03"); titles. add ("page04"); // add the adapter in the first step. Use pageAdapter to manage the viewpager of the child View object. setAdapter (new InnerPagerAdapter (); // Add a listener to the tab, listen to the click event, and obtain the button to be clicked. index the listener Based on the button location, set the tab of the page to be displayed on the viewpage. setOnCheckedChangeListener (new InnerOnCheckedChangeListener (); // Add a listener to the ViewPage and listen to it. When the viewpage slides, the page switches to the selected page to obtain the location index of the page, set the selection status of the tab button according to this position. setOnPageChangeListener (new listener ();} private class InnerOnPageChangeListener implements OnPageChangeListener {@ Overridepublic void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {// when the page is scrolled} @ Overridepublic void onPageSelected (int position) {// when this interface is selected, the listener event switch (position) {case 0: tab. check (R. id. btn_Home); break; case 1: tab. check (R. id. btn_Time); break; case 2: tab. check (R. id. btn_Message); break; case 3: tab. check (R. id. btn_Settings); break; default: break; }}@ Overridepublic void onPageScrollStateChanged (int state) {// when the rolling status changes} private class implements OnCheckedChangeListener {@ Overridepublic void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (checkedId) {case R. id. btn_Home: viewpager. setCurrentItem (0); // set the break of the currently displayed interface; case R. id. btn_Time: viewpager. setCurrentItem (1); break; case R. id. btn_Message: viewpager. setCurrentItem (2); break; case R. id. btn_Settings: viewpager. setCurrentItem (3); break; default: break ;}} public class InnerPagerAdapter extends PagerAdapter {@ Overridepublic CharSequence getPageTitle (int position) {// TODO Auto-generated method stubreturn titles. get (position) ;}@ Overridepublic int getCount () {// get the number of child-level la s return children. size (); // returns the number of neutron-level la s in the viewpage container.} @ Overridepublic boolean isViewFromObject (View view, Object object) {// determine whether a View object is the object Currently added to the ViewPager container. return view = object ;} // The following two methods are the non-abstract methods that must be implemented in PageAdapter @ Overridepublic Object instantiateItem (ViewGroup container, int position) {// instantiate the View object that needs to be displayed at the position specified in the ViewPager container. View = children. get (position); container. addView (view); return view ;}@ Overridepublic void destroyItem (ViewGroup container, int position, Object object) {// remove the View Object container at the specified position from ViewPager. removeView (children. get (position ));}}}