android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)
本文导语: 文件名:page.xml 代码如下: 文件名:listviewitem.xml 代码如下: 以上的第二个布局文件 只提供给simpleAdapter 代码如下:package com.example.learnpager2; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java....
文件名:page.xml
文件名:listviewitem.xml
以上的第二个布局文件 只提供给simpleAdapter
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 pagesArrayList;// 添加listview 然后传给MyPagerAdapter 充当媒介
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();
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);
//第一个页面的viewpager第1个page里的listview
LayoutInflater layoutInflater=getLayoutInflater();
listView=(ListView)
(layoutInflater.inflate(R.layout.page,null).findViewById(R.id.listview));
ArrayAdapter arrrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,strs);
listView.setAdapter(arrrayAdapter);
pagesArrayList.add(listView);
//第个页面的viewpager第2个page里的listview
listView=(ListView)
(layoutInflater.inflate(R.layout.page,null).findViewById(R.id.listview));
ArrayAdapter arrrayAdapter2=new ArrayAdapter(this,android.R.layout.simple_list_item_1,strs2);
listView.setAdapter(arrrayAdapter2);
pagesArrayList.add(listView);
//第三个面的viewpager第3个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 getDatasForListView(){
Log.e("3", "");
List listMaps=new ArrayList();
String[] strings=new String[]{"图片1","图片2","图片3"};
int[] images=new int[]{R.drawable.p1,R.drawable.p1,R.drawable.p1};
for(int i=0;i