Android:ViewPager04

  目录结构层次:

Android:ViewPager04

注意点:

1.MyAdapter.java一定要 extends PagerAdapter

2.MyAdapter要覆写以下几个方法:

a.public int getCount(){}

b.public boolean isViewFromObject(View arg0, Object arg1){}

c.public Object instantiateItem(ViewGroup container, int position){}

d.public void destroyItem(ViewGroup container, int position, Object object) {}//destroyItem方法一定要注意,具体注意事项见下面的具体代码的注释。

e.public CharSequence getPageTitle(int position) {}  有title的时候才需要覆盖这个方法。

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wyl.view04.MainActivity$PlaceholderFragment" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

  main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </LinearLayout>

  view01.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="易"
/>
</LinearLayout>

  view02.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华"
/>
</LinearLayout>

  view03.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kai"
/>
</LinearLayout>

  view04.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="men"
/>
</LinearLayout>

  view05.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wu"
/>
</LinearLayout>

  MainActivity.java:

package com.wyl.view04;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View; public class MainActivity extends Activity {
List<View> listview;
List<String> titlelist;
ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("================12342142=========");
View v01 = View.inflate(this, R.layout.view01, null);
View v02 = View.inflate(this, R.layout.view02, null);
View v03 = View.inflate(this, R.layout.view03, null);
View v04 = View.inflate(this, R.layout.view04, null);
//实例化数据源
listview = new ArrayList<View>();
listview.add(v01);
listview.add(v02);
listview.add(v03);
listview.add(v04); //实例化titlelist
titlelist = new ArrayList<String>();
titlelist.add("背景");
titlelist.add("设置");
titlelist.add("朋友");
titlelist.add("动态");
PagerTabStrip tab = (PagerTabStrip) findViewById(R.id.tab);
tab.setBackgroundColor(Color.YELLOW);
tab.setTabIndicatorColor(Color.GRAY);
tab.setDrawFullUnderline(false);
tab.setTextColor(Color.RED);
vp = (ViewPager) findViewById(R.id.pager);
MyAdapter adapter = new MyAdapter(listview,titlelist);
vp.setAdapter(adapter);
System.out.println("09fdjfod========");
} }

  MyAdapter.java

package com.wyl.view04;

import java.util.List;

import org.w3c.dom.ls.LSInput;

import android.graphics.Shader.TileMode;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup; public class MyAdapter extends PagerAdapter{
List<View> listview;
List<String> titlelist;
public MyAdapter(List<View> listview,List<String> titlelist){
this.listview = listview;
this.titlelist = titlelist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listview.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
} @Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(listview.get(position));
System.err.println("hahha.........");
// if(position==4){
// }
return listview.get(position);
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView(listview.get(position));//上面一行一定要注释掉,否则会报destroyItem方法没有被覆盖
} @Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
System.out.println("=====9999999999");
return titlelist.get(position);
} }

  效果图:

Android:ViewPager04

上一篇:EnjoyCSS – 在线的,先进的 CSS3 代码生成器


下一篇:MongoDB 效率