Android ViewPager instance Code 2.

Source: Internet
Author: User

I have previously written a ViewPager: content + indicator Demo;

This article continues to introduce ViewPager: content + title Demo.

Implementation:



Source code:

Layout file: activity_main:

                         
 

F1.xml (Sports News layout file ):

     
  
 

F2.xml (Entertainment News layout file ):

     
  
 

F3.xml (Military News layout file ):

     
          
           
           
       
  
 

Code file:

MainActivity:

Package com. fragmentdemo11_viewpager; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. support. v4.view. viewPager; import android. view. layoutInflater; import android. view. view; public class MainActivity extends Activity {private ViewPager viewPager; private List
 
  
Title; private List
  
   
Content; private LayoutInflater inflater; private MyViewPagerAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); viewPager = (ViewPager) findViewById (R. id. viewPager); content = new ArrayList
   
    
(); Inflater = LayoutInflater. from (this);/*** add content */content. add (inflater. inflate (R. layout. f1, null); content. add (inflater. inflate (R. layout. f2, null); content. add (inflater. inflate (R. layout. f3, null);/*** Add the title corresponding to each content */title = new ArrayList
    
     
(); Title. add ("Sports News"); title. add ("Entertainment News"); title. add ("Military News"); adapter = new MyViewPagerAdapter (this, content, title); viewPager. setAdapter (adapter );}}
    
   
  
 

ViewPager adapter:

Package com. fragmentdemo11_viewpager; import java. util. list; import android. content. context; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. view; import android. view. viewGroup;/*** ViewPager adapter */public class MyViewPagerAdapter extends PagerAdapter {private Context context; private List
  
   
Content; private List
   
    
Title; public MyViewPagerAdapter (Context context, List
    
     
Content, List
     
      
Title) {this. context = context; this. content = content; this. title = title;}/*** initialization */@ Overridepublic Object instantiateItem (ViewGroup container, int position) {(ViewPager) container ). addView (content. get (position); return content. get (position);}/*** title */@ Overridepublic CharSequence getPageTitle (int position) {return title. get (position) ;}@ Overridepublic int getCount () {return content. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}/ *** destroy */@ Overridepublic void destroyItem (ViewGroup container, int position, Object object) {(ViewPager) container ). removeView (content. get (position ));}}
     
    
   
  


Source code download:

Click to download source code


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.