Android applications using Viewpager to achieve similar QQ interface switching effect _android

Source: Internet
Author: User
Tags object object stub

These days in the study Viewpager, simply write how to use Viewpager to achieve similar QQ "recent contacts, friends, group" interface switch (do not know if they are implemented in this way).
The Viewpager has been added to the ANDROID-SDK, in the exact position of%android_sdk_home%\android-compatibility\v4,%android_sdk_home% It's your android-sdk-windows catalogue.

OK, here's a picture of the effect:

Step one: Create a new project, mine is Viewpager
Step two: Import jar packs
Specifically as follows:
Right Key engineering--properties--Click on the left Java build path--Click the right libraries tag-click on the right Add External jars--Select the jar package described above and save

Step three: Write a main interface, add the Android.support.v4.view.ViewPager component

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" fill_parent " 
  android:o" rientation= "vertical" > 
 
  <textview 
    android:layout_width= "Fill_parent" 
    Wrap_content " 
    android:text=" @string/hello "/> 
   
   
  <android.support.v4.view.viewpager 
    android:id= "@+id/mviewpager" 
    android:layout_width= "fill_parent" 
    android:layout_height= "wrap_content" 
    /> 
 
   
</LinearLayout> 

Step four: Write three interfaces to add in
The interface is very simple, casually written
Interface 1

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" Fill_parent " 
  android: Background= "#ff0000" 
  android:orientation= "vertical" > 
 
  <button 
    android:layout_width= "Fill_" Parent " 
    android:layout_height=" wrap_content " 
    android:text=" layout1 "/> 
 
</LinearLayout> 

Interface 2

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" Fill_parent " 
  android: Background= "#00ff00" 
  android:orientation= "vertical" > 
 
  <textview 
    android:layout_width= "fill _parent " 
    android:layout_height=" wrap_content " 
    android:text=" Layout2 "/> 
 
</LinearLayout>

Interface 3

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" Fill_parent " 
  android: Background= "#0000ff" 
  android:orientation= "vertical" > 
 
  <textview 
    android:layout_width= "Fill_" Parent " 
    android:layout_height=" wrap_content " 
    android:text=" Layout3 "/> 
 
</LinearLayout> 

Step five: Write the main class, my name is viewpageractivity

Package Com.carter.viewpager; 
Import java.util.ArrayList; 
Import java.util.List; 
 
Import Java.util.zip.Inflater; 
Import android.app.Activity; 
Import Android.app.ActivityGroup; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.support.v4.view.PagerAdapter; 
Import Android.support.v4.view.ViewPager; 
Import Android.support.v4.view.ViewPager.OnPageChangeListener; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
 
Import Android.view.ViewGroup; 
   
  The public class Viewpageractivity extends Activitygroup {/** * can support sliding components/Viewpager Mviewpager; 
   
  /** * Viewpager need to set the adapter to implement the add interface, * The adapter inherits from Pageradapter/Mypageradapter adapter;  
   
  /** * Used to save three interfaces/list<view> List; 
   
   
   
   
  /** * For the generation of interface * * Layoutinflater minflater; /** called the activity is a. */@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.main); 
     
    Minflater = This.getlayoutinflater (); 
    /** * Add in three interfaces to list for initializing Pageradapter/list = new arraylist<view> (); List.add (This.getlocalactivitymanager (). StartActivity ("Layout1", New Intent (Viewpageractivity.this, 
    Layout1activity.class)). Getdecorview ()); 
    List.add (minflater.inflate (R.LAYOUT.LAYOUT2, null)); 
     
    List.add (minflater.inflate (R.LAYOUT.LAYOUT3, null)); 
    Mviewpager = (Viewpager) Findviewbyid (R.id.mviewpager); 
    adapter = new Mypageradapter (list); 
     
    Mviewpager.setadapter (adapter); /** * To add Pagechangelistener for Viewpager, in the callback method can do other things, such as processing some interface tag conversion and so on, if you have a tab bar words * * * MVIEWPAGER.SETONPAGEC 
        Hangelistener (New Onpagechangelistener () {@Override public void onpageselected (int arg0) { 
      TODO auto-generated Method Stub log.i ("Carter", "onpageselected"); 
       
      }@Override public void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub @Override public void onpagescrollstatechanged (int arg0) {//TODO auto- 
     
  Generated method stub}}); 
     
    Class Mypageradapter extends Pageradapter {list<view> mlist; 
    Public Mypageradapter (list<view> list) {mlist = list; 
    @Override public int GetCount () {//TODO auto-generated a stub return mlist.size (); @Override public boolean isviewfromobject (View arg0, Object arg1) {//TODO auto-generated method s 
    Tub return arg0 = = (arg1); @Override public void Destroyitem (View container, int position, object object) {//TODO Auto-generat 
    Ed method Stub ((Viewpager) container). Removeview (Mlist.get (position)); } @Override Public Object Instantiateitem (View container, int position) {//TODO auto-generated Method Stub ((Viewpager) Conta 
      Iner). AddView (Mlist.get (position), 0); 
    return Mlist.get (position); 
      @Override public void Finishupdate (ViewGroup container) {//TODO auto-generated stub 
    Super.finishupdate (container); 

 } 
  } 
}

Code interpretation:
1, in the above rewrite the Pageradapter, in which the most important way is

@Override public 
    Object instantiateitem (View container, int position) { 
      //TODO auto-generated method Stub 
      ( (Viewpager) container). AddView (Mlist.get (position), 0); 
      return Mlist.get (position); 
    }  

Concrete why each time is addview to 0 position, I still do not know, this is also from the Internet to find a way to see.

2, in the initialization list, which contains content is a view element, in my understanding of the scope of two methods
The first is to get a view by adding the window's Getdecorview ();
The second way is to generate a view from the XML by Layoutinflater.
It should be noted that the first method requires the main class to inherit from Activitygroup in order to use Getlocalactivitymanager and subsequent methods when adding, and the activity that needs to be generated, such as the layout1activity in this example , to be declared in Androidmanifest.xml.

/** 
 * Add in three interfaces to list for initializing Pageradapter 
/list = new arraylist<view> (); 
List.add (This.getlocalactivitymanager (). StartActivity ("Layout1", New Intent (Viewpageractivity.this, Layout1activity.class)). Getdecorview ()); 
List.add (minflater.inflate (R.LAYOUT.LAYOUT2, null)); 
List.add (minflater.inflate (R.LAYOUT.LAYOUT3, null)); 

3, if only from the XML generated view to add, you can completely remove inherited activitygroup.
4, if you use the StartActivity method to start, you need to add a few of the activity, is joined together on the initialization, can be seen in the OnCreate () method log.

PS: How to solve the problem of calling notifydatasetchanged failure in Android Viewpager and Pageradapter
the specific explanation is as follows:
Google's launch of the Viewpager control in the Android 3.0SDK largely satisfies the developer's ability to move and switch around the page, which is very convenient to use. However, it was found in use that when deleting or modifying data, Pageradapter could not refresh the view just by notifydatasetchanged method notifications as Baseadapter.
The most basic way:
For a simple case of child view (for example, TextView, ImageView, etc., there is no ListView, etc.), you can add code to your own adapter:

@Override public  
int GetItemPosition (Object object) {return  
  position_none;  
}  

This will not only achieve the desired effect under normal circumstances.
The problems that exist:
This is not a bug in Pageradapter, and typically, calling the Notifydatasetchanged method lets Viewpager query all child view through the adapter GetItemPosition method, in which case , all child view locations are Position_none, indicating that none of the child view exists, Viewpager calls the Destroyitem method to destroy, rebuilds, increases system overhead, and causes logical problems in some complex situations. Especially when it comes to trying to update the content of the child view, it creates a completely unnecessary overhead.
More effective ways to:
The more plausible approach is to implement the Notifydatasetchanged function according to your own needs, for example, when you need to update only one view content, at Instantiateitem (), Use the View.settag method to add a flag, when you need to update the information, through the Findviewwithtag method to find the corresponding view to update it.

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.