1、ViewPager基本使用
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
//viewPager组件的使用
public class MainActivity_vp1 extends AppCompatActivity {
private ViewPager viewPager;
private PagerTabStrip pagerTabStrip;
private String[] titles = {"萌王","静","朱菜","苍影"};
private ArrayList<View> views = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_vp1);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagertab);
initViews();
viewPager.setAdapter(new MyPagerAdapter());
}
private void initViews(){
views.add(getLayoutInflater().inflate(R.layout.layout1,null));
views.add(getLayoutInflater().inflate(R.layout.layout2,null));
views.add(getLayoutInflater().inflate(R.layout.layout3,null));
views.add(getLayoutInflater().inflate(R.layout.layout4,null));
//建立四个layout页面作为切换的页面
}
//适配器
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return views.size();
}
//实例化选项
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View v = views.get(position);
container.addView(v);
return v;
}
//删除现象卡
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(views.get(position));
}
//判断视图是否为返回的对象
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
//获取标题
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_vp1"
android:padding="16dp">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagertab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
2、ViewPager属性配置
pagerTabStrip.setTabIndicatorColor();//指示器的颜色
pagerTabStrip.setBackgroundColor();//背景色
pagerTabStrip.setTextColor(Color.WHITE);//字体颜色
viewPager.setCurrentItem(3);//设置默认显示的选项
package com.example.application04;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
//viewPager组件的使用
public class MainActivity_vp1 extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager viewPager;
private PagerTabStrip pagerTabStrip;
private String[] titles = {"萌王","静","朱菜","苍影"};
private ArrayList<View> views = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_vp1);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagertab);
initViews();
viewPager.setAdapter(new MyPagerAdapter());
//设置默认显示的选项
viewPager.setCurrentItem(1);
viewPager.addOnPageChangeListener(this);
}
private void initViews(){
views.add(getLayoutInflater().inflate(R.layout.layout1,null));
views.add(getLayoutInflater().inflate(R.layout.layout2,null));
views.add(getLayoutInflater().inflate(R.layout.layout3,null));
views.add(getLayoutInflater().inflate(R.layout.layout4,null));
pagerTabStrip.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
pagerTabStrip.setTabIndicatorColor(getResources().getColor(android.R.color.holo_blue_bright));
pagerTabStrip.setTextColor(Color.WHITE);
}
//切换后
@Override
public void onPageScrolled(int i, float v, int i1) {
}
//被选中时
@Override
public void onPageSelected(int i) {
Toast.makeText(this,"page---"+i,Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int i) {
}
//适配器
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return views.size();
}
//实例化选项
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View v = views.get(position);
container.addView(v);
return v;
}
//删除选项卡
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(views.get(position));
}
//判断视图是否为返回的对象
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
//获取标题
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
}
3、ViewPager程序首次启动引导案例
android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen"设置全屏
package com.example.application04;
import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity_vp2 extends Activity implements ViewPager.OnPageChangeListener{
private ViewPager viewPager;
private ArrayList<View> views = new ArrayList<>();
private ImageView[] imageViews;
private int currentIndex;//当前正在显示的卡页
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_vp2);
initViews();
initPoint();
}
private void initViews(){
viewPager = (ViewPager) findViewById(R.id.viewpager);
views.add(getLayoutInflater().inflate(R.layout.layout1,null));
views.add(getLayoutInflater().inflate(R.layout.layout2,null));
views.add(getLayoutInflater().inflate(R.layout.layout3,null));
views.add(getLayoutInflater().inflate(R.layout.layout4,null));
viewPager.addOnPageChangeListener(this);
viewPager.setAdapter(new MyPagerAdapter());
}
private void initPoint(){
LinearLayout point_layout = (LinearLayout)findViewById(R.id.point_layout);
imageViews = new ImageView[views.size()];
for (int i=0;i<imageViews.length;i++){
imageViews[i] = (ImageView) point_layout.getChildAt(i);
}
currentIndex = 0;
imageViews[currentIndex].setImageResource(R.drawable.touched_hole);
}
//设置图片
private void setCurrentPoint(int position){
if(currentIndex<0 || currentIndex==position || currentIndex>imageViews.length-1){
return;
}
imageViews[currentIndex].setImageResource(R.drawable.default_hole);
imageViews[position].setImageResource(R.drawable.touched_hole);
currentIndex = position;
}
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
setCurrentPoint(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
//适配器
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return views.size();
}
//实例化选项
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View v = views.get(position);
container.addView(v);
return v;
}
//删除选项卡
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(views.get(position));
}
//判断视图是否为返回的对象
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_vp2">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:id="@+id/point_layout">
<ImageView
android:layout_width="85dp"
android:layout_height="50dp"
android:src="@drawable/default_hole" />
<ImageView
android:layout_width="85dp"
android:layout_height="50dp"
android:src="@drawable/default_hole" />
<ImageView
android:layout_width="85dp"
android:layout_height="50dp"
android:src="@drawable/default_hole" />
<ImageView
android:layout_width="85dp"
android:layout_height="50dp"
android:src="@drawable/default_hole" />
</LinearLayout>
</FrameLayout>