Android completes the sliding effect by adding ListView to the ViewPage (similar to the QQ sliding interface) _android

Source: Internet
Author: User

FileName: page.xml

Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

<listview
Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>

</RelativeLayout>

FileName: listviewitem.xml

Copy Code code as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<textview
Android:id= "@+id/textview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Hello"
Android:textcolor= "#00ff00"
/>
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_margin= "5DP"
/>
</LinearLayout>


The second layout file above is available only to Simpleadapter

Copy Code code as follows:

Package com.example.learnpager2;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.os.Parcelable;
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.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import android.view.ViewParent;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.RelativeLayout;
Import Android.widget.SimpleAdapter;

Public class Pageractivity extends activity {
    viewpager viewpager;
    arraylist<view> pagesarraylist;//Add listview and pass to Mypageradapter as media
    String[] STRs;
    string[] strs2;
    ListView ListView;
    Button button1;
    Button button2;
    Button Button3;
    @Override
    protected void onCreate (Bundle savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_pager);
        iniparams ();
        iniviews ();

}
private void Iniparams () {
Pagesarraylist=new arraylist<view> ();
Strs=new string[]{"A", "B", "C", "A", "B", "C", "A", "B", "C"};
Strs2=new string[]{"1", "2", "3", "1", "2", "3", "1", "2", "3"};

}
private void Iniviews () {
Viewpager= (Viewpager) Findviewbyid (R.id.viewpager);
The first page of the Viewpager ListView in the 1th page
Layoutinflater Layoutinflater=getlayoutinflater ();

        listview= (listView)
                 (Layoutinflater.inflate (r.layout.page,null). Findviewbyid ( R.id.listview));
        arrayadapter<string> arrrayadapter=new arrayadapter< String> (this,android. R.LAYOUT.SIMPLE_LIST_ITEM_1,STRS);
        Listview.setadapter (arrrayadapter);
        Pagesarraylist.add (listView);


       //Viewpager 2nd page ListView
         listview= (listView)
                 (Layoutinflater.inflate (r.layout.page,null). Findviewbyid (R.id.listview));
        arrayadapter<string> arrrayadapter2=new arrayadapter< String> (this,android. R.LAYOUT.SIMPLE_LIST_ITEM_1,STRS2);
        Listview.setadapter (arrrayAdapter2);
        Pagesarraylist.add (listView);

       //Third face Viewpager 3rd page ListView
         simpleadapter simpleadapter3=new Simpleadapter (This,getdatasforlistview (),
                 r.layout.listviewitem,new string[ ]{"title", "Image"},new Int[]{r.id.textview,r.id.imageview});
        listview= (listView)
                 (layoutinflater.inflate (r.layout.page, null). Findviewbyid (R.id.listview));
        Listview.setadapter (SIMPLEADAPTER3);
        Pagesarraylist.add (listView);


Viewpager.setadapter (New Mypageradapter (pagesarraylist));
Viewpager.setonpagechangelistener (New Myonpagechangelistener ());
Viewpager.setcurrentitem (0);

button1= (Button) Findviewbyid (R.id.button1);
Button2= (Button) Findviewbyid (R.id.button2);
button3= (Button) Findviewbyid (R.id.button3);
}
Public list<map<string, Object>> Getdatasforlistview () {
LOG.E ("3", "");
List<map<string, object>> listmaps=new arraylist<map<string,object>> ();

String[] strings=new string[]{"Picture 1", "Picture 2", "Picture 3"};
Int[] Images=new INT[]{R.DRAWABLE.P1,R.DRAWABLE.P1,R.DRAWABLE.P1};

for (int i=0;i<strings.length;i++) {
Listmaps.add (Listviewitemfactory.generate (new object[]{strings[i],images[0]));
}
return listmaps;
}
Static Class listviewitemfactory{
Static map<string,object> Generate (object[] obj) {
Map<string,object> map=new hashmap<string, object> ();
Map.put ("title", Obj[0]);
Map.put ("image", obj[1]);
return map;
}
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.pager, menu);
return true;
}
public class Mypageradapter extends Pageradapter {
Public list<view> mlistviews;

Public Mypageradapter (list<view> mlistviews) {
This.mlistviews = Mlistviews;
}

@Override
public void Destroyitem (View arg0, int arg1, Object arg2) {

LOG.D ("Destroyitem", "" +arg0+ "" +arg1);
((Viewpager) arg0). Removeview (Mlistviews.get (arg1));
}
@Override
public int GetCount () {
return Mlistviews.size ();
}

@Override
Public Object Instantiateitem (View arg0, int arg1) {
LOG.D ("Instantiateitem", "" +arg0+ "" +arg1);
try {
if (Mlistviews.get (arg1). GetParent () ==null)
((Viewpager) arg0). AddView (Mlistviews.get (arg1), 0);
else{
It's hard to understand that the newly added view automatically binds a parent class, because a son view cannot be related to two parent classes, so you have to unbind
Failure to do so would otherwise produce Viewpager java.lang.IllegalStateException:The specified child already has a parent. You are must call Removeview () on the child ' s parent.
Another method is Viewpager.setoffscreenpagelimit (3); This method does not judge whether parent is already present, but the superfluous ListView cannot be destroy
((ViewGroup) Mlistviews.get (arg1). GetParent ()). Removeview (Mlistviews.get (arg1));

((Viewpager) arg0). AddView (Mlistviews.get (arg1), 0);
}
catch (Exception e) {
TODO auto-generated Catch block
LOG.D ("parent=", "" "+mlistviews.get (arg1). GetParent ());
E.printstacktrace ();
}
Return Mlistviews.get (ARG1);
}

@Override
public boolean isviewfromobject (View arg0, Object arg1) {
return arg0 = = (arg1);
}

@Override
public void Restorestate (parcelable arg0, ClassLoader arg1) {
}

@Override
Public parcelable SaveState () {
return null;
}

@Override
public void Startupdate (View arg0) {
}
}
Class Myonpagechangelistener implements onpagechangelistener{

@Override
public void onpagescrollstatechanged (int state) {
TODO auto-generated Method Stub

}

@Override
public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {
TODO auto-generated Method Stub

}
Color Precolor;
@Override
public void onpageselected (int position) {
TODO auto-generated Method Stub
LOG.D ("page", "pos=" +position);
Switch (position)
{
Case 0://button1.setbackgroundcolor (0x00ff00);
Case 1://Button1.setbackgroundcolor (0xff0000);
Case 2:
}

}

}
}

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.