
文件名:page.xml
复制代码 代码如下:
<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>
文件名:ListvIEwitem.xml
复制代码 代码如下:
<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>
以上的第二个布局文件 只提供给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<VIEw> 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<VIEw>();
strs=new String[]{"a","b","c","a","c"};
strs2=new String[]{"1","2","3","1","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<String> arrrayAdapter=new ArrayAdapter<String>(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<String> arrrayAdapter2=new ArrayAdapter<String>(this,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<Map<String,Object>> getDatasForListVIEw(){
Log.e("3","");
List<Map<String,Object>> ListMaps=new ArrayList<Map<String,Object>>();
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<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{
// 很难理解新添加进来的vIEw会自动绑定一个父类,由于一个儿子vIEw不能与两个父类相关,所以得解绑
//不这样做否则会产生 vIEwpager java.lang.IllegalStateException: The specifIEd child already has a parent. You must call removeVIEw() on the child's parent first.
// 还有一种方法是vIEwPager.setoffscreenPagelimit(3); 这种方法不用判断parent 是不是已经存在,但多余的ListvIEw不能被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:
}
}
}
}
以上是内存溢出为你收集整理的android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)全部内容,希望文章能够帮你解决android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)