Android ViewPager usage

Source: Internet
Author: User


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/J0tTKtc/WtuC49tKzw + a7rLav0Ke5 + release + CjwvcD4KPHA + release = "brush: java;"> public class TvControlActivity extends Activity {private TvControlActivity tvControlActivity = this; // private View view1, private View view2, private View view3, and private ArrayList Views; // switch between private ViewPager viewPager by sliding multiple pages; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_ TV _control); viewPager = (ViewPager) findViewById (R. id. main_viewpager); viewPager. setOnPageChangeListener (null); initView ();/*** this is a method for rewriting PagerAdapter. We can also do this */PagerAdapter paAdapter = new PagerAdapter () {@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 = arg1;} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn views. size () ;}// destroy @ Overridepublic void destroyItem (ViewGroup container, int position, Object object) {// TODO Auto-generated method stubcontainer. removeView (views. get (position);} // @ Override // public void destroyItem (View container, int position, Object object) {// TODO Auto-generated method stub // (ViewPager) container ). removeView (views. get (position); //} @ Overridepublic int getItemPosition (Object object) {// TODO Auto-generated method stubreturn super. getItemPosition (object);} // instantiate @ Overridepublic Object instantiateItem (ViewGroup container, int position) {// TODO Auto-generated method stubcontainer. addView (views. get (position); return views. get (position);} // @ Override // public Object instantiateItem (View container, int position) {// TODO Auto-generated method stub // (ViewPager) container ). addView (views. get (position); // return views. get (position); //}; viewPager. setAdapter (paAdapter);}/*** load the page card to be displayed */private void initView () {// TODO Auto-generated method stubLayoutInflater inflater = getLayoutInflater (). from (this); view1 = inflater. inflate (R. layout. TV _control, null); view2 = inflater. inflate (R. layout. TV _control_number, null); // load the View to be displayed by page into the array: views = new ArrayList (); Views. add (view1); views. add (view2 );}}You must rewrite the following four methods when implementing the adapter. You can also rewrite other methods.

instantiateItem(ViewGroup container, int position)getItemPosition(Object object)destroyItem(ViewGroup container, int position,Object object)isViewFromObject(View arg0, Object arg1)


Note that the layout file must be written in this way.
A widget is used to display the sliding left and right interfaces. If an xml layout file is not loaded, the widget does not display the content of android: id = "@ + id/main_viewpager" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center">

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: info-contact@alibabacloud.com 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.