Android uses listview to complete the sliding effect (similar to the qq sliding Interface) by adding it to the viewpage)

Source: Internet
Author: User

File Name: page. xml

Copy codeThe Code is 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>

File Name: listviewitem. xml

Copy codeThe Code is 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 is provided only to simpleAdapter.

Copy codeThe Code is 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 a listview and send it to MyPagerAdapter as a 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 );
// Listview in viewpager 1st pages on the first 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 );

// The listview in the viewpager 2nd page of the first page
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 );

// The listview in the third viewpager 3rd page
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 [] {"Image 1", "Image 2", "Image 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 is hard to understand that the newly added view will automatically bind a parent class. Because a son view cannot be associated with two parent classes, you must unbind it.
// Otherwise, viewpager java. lang. IllegalStateException will be generated: The specified child already has a parent. You must call removeView () on the child's parent first.
// Another method is viewPager. setOffscreenPageLimit (3). This method does not need to determine whether the parent already exists, but the excessive 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); break;
Case 1: // button1.setBackgroundColor (0xFF0000); break;
Case 2:
}

}

}
}

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.