20ViewPager demo1,2:接收ViewPager展示View的使用
Demo1
MainActivity .JAVA
package com.qf.day20_viewpager_demo1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ViewPager viewPager;
//图片的id
private int[] imags = {R.drawable.image1,R.drawable.image2,R.drawable.image3};
//ViewPager可以添加View视图
private List<ImageView> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
//数据源 View视图
list = new ArrayList<ImageView>();
//获取数据源的
for(int i=0;i<imags.length;i++){
//获得数据源
ImageView iv = new ImageView(MainActivity.this);
iv.setImageResource(imags[i]);
list.add(iv);
if(i==2){
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了第三个界面", 0).show();
}
});
}
}
MyPagerAdapter adapter = new MyPagerAdapter();
viewPager.setAdapter(adapter);
}
/**
* 自定义的PagerAdapter
* @author sxy
*
*/
private class MyPagerAdapter extends PagerAdapter{
/**
* 获取数据源中数据的数量
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
/**
* 根据指定的下标 创建ViewPager展示的item视图
* ViewGroup container:管理当ViewPager的视图组
* int position:指定的下标
*/
/**
* super.destroyItem(container, position, object);一定要删除 不然会有问题
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
//将视图添加到视图组中
container.addView(list.get(position));
return list.get(position);
}
/**
* 根据指定的下标 移除指定视图组中View 视图
* 参数1:视图组
* 参数2:指定的下标
* 参数3:instantiateItem返回的Object对象
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
//super.destroyItem(container, position, object);
//从视图组中移除指定的视图
container.removeView(list.get(position));
}
/**
* 当前ViewPager展示的VIew对象和instantiateItem返回的对象是否是同一个
*
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1 ;
}
}
}
布局文件
<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=".MainActivity" >
<!-- 包名.类名 -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Demo2
package com.qf.day20_viewpager_demo2;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private ViewPager viewPager;
// View视图的集合
private List<View> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
// 数据源
list = new ArrayList<View>();
for (int i = 1; i < 6; i++) {
// 通过文件名称 获取资源标识
int layoutRes = getResources().getIdentifier("pager_view" + i, "layout", getPackageName());
// 获取当前View视图
View view = getLayoutInflater().inflate(layoutRes, null);
if (i == 5) {
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 跳转到主程序界面
// 记录已经进入到主程序 boolean 到SharedPreference
Toast.makeText(MainActivity.this, "点击了进入按钮", 0).show();
}
});
}
list.add(view);
}
// adapter
MyPagerAdapter adapter = new MyPagerAdapter();
viewPager.setAdapter(adapter);
}
public class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position));
return list.get(position);
}
/**
* super.destroyItem(container, position, object);一定要删除 不然会有问题
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
container.removeView(list.get(position));
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
}
}
布局文件
activity_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=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
pager_view1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是第一个界面"
android:textColor="#f00"
android:textSize="20sp"
>
</TextView>
pager_view2.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是第2个界面"
android:textColor="#f00"
android:textSize="20sp"
>
</TextView>
pager_view5.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:gravity="center"
>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入"
/>
</LinearLayout>