这两天学习了ViewPager,在这里做个笔记,方便以后查看
参考资料:Android 基础入门教程——2.6.3 ViewPager的简单使用
首先制作3个界面作为滑动跳转的界面
view_one.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:background="#CC22FF7A">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/black"
android:text="第一个view"/>
</LinearLayout>
view_two.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:background="#8022FF7A"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/black"
android:text="第二个view"/>
</LinearLayout>
view_three.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:background="#3322FF7A"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:textColor="@android:color/black"
android:text="第三个view"/>
</LinearLayout>
之后这3个界面会重复使用
一、简单的viewPager使用
首先是创建oneActivity.java和activity_one.xml
1、在activity_one.xml中添加viewPager
<?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"
android:orientation="vertical"
tools:context=".oneActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单的viewPager使用"
android:textColor="@android:color/white"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/button1"
android:textAllCaps="false"
android:background="@android:color/holo_blue_light"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/one_pager">
</android.support.v4.view.ViewPager>
</LinearLayout>
2、创建适配器MyPagerAdapter
package com.example.viewpager;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<View> viewLists;
public MyPagerAdapter() { }
public MyPagerAdapter(ArrayList<View> viewLists) {
this.viewLists = viewLists;
}
// 获取要滑动的控件的数量
@Override
public int getCount() {
return viewLists.size();
}
// 来判断显示的是否是同一个界面,这里我们将两个参数相比较返回即可
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
// 当要显示的界面可以进行缓存的时候,会调用这个方法进行显示界面的初始化,
// 我们将要显示的界面加入到ViewGroup中,然后作为返回值返回即可
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(viewLists.get(position));
return viewLists.get(position);
}
// PagerAdapter只缓存3个界面,如果滑动的界面超出了缓存的范围,就会调用这个方法,将界面销毁
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(viewLists.get(position));
}
}
3、在oneActivity 中添加数据
package com.example.viewpager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class oneActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager Pager_one;
private ArrayList<View> arrayList;
private MyPagerAdapter myPagerAdapter;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Pager_one = (ViewPager) findViewById(R.id.one_pager);
arrayList = new ArrayList<>();
LayoutInflater layoutInflater = getLayoutInflater();
arrayList.add(layoutInflater.inflate(R.layout.view_one, null, false));
arrayList.add(layoutInflater.inflate(R.layout.view_two, null, false));
arrayList.add(layoutInflater.inflate(R.layout.view_three, null, false));
myPagerAdapter = new MyPagerAdapter(arrayList);
Pager_one.setAdapter(myPagerAdapter);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
startActivity(new Intent(oneActivity.this,MainActivity.class));
finish();
break;
}
}
}
button是用来返回main的
标题栏——PagerTitleStrip与PagerTabStrip
PagerTabStrip与PagerTitleStrip的使用基本相同,唯一不同的是:
1、PagerTabStrip在当前页面下,会有一个下划线条来提示当前页面的Tab是哪个。
2、PagerTabStrip的Tab是可以点击的,当用户点击某一个Tab时,当前页面就会跳转到这个页面,而PagerTitleStrip则没这个功能。
此处引用于大神"启舰"的这篇博客------>链接
二、PagerTitleStrip的使用
首先是创建twoActivity.java和activity_two.xml
1、首先也是先添加控件,这次添加的控件是PagerTitleStrip
activity_two.xml
<?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"
android:orientation="vertical"
tools:context=".twoActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PagerTitleStrip效果演示"
android:textColor="@android:color/white"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/button1"
android:textAllCaps="false"
android:background="@android:color/holo_blue_light"/>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/two_pager">
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pager_title"
android:background="@android:color/holo_blue_bright">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
2、然后是创建适配器MyPagerAdapter2,和之前的区别不大,就是多了个装标题栏的东西, private ArrayList titleLists;
package com.example.viewpager;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class MyPagerAdapter2 extends PagerAdapter {
private ArrayList<View> viewLists;
private ArrayList<String> titleLists;
public MyPagerAdapter2(){}
public MyPagerAdapter2(ArrayList<View> viewLists, ArrayList<String> titleList) {
this.viewLists = viewLists;
this.titleLists = titleList;
}
@Override
public int getCount() {
return viewLists.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(viewLists.get(position));
return viewLists.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewLists.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
return titleLists.get(position);
}
}
3、在twoActivity中添加数据
package com.example.viewpager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class twoActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager pager_two;
private ArrayList<View> viewArrayList;
private ArrayList<String> stringArrayList;
private MyPagerAdapter2 myPagerAdapter2;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
pager_two = (ViewPager) findViewById(R.id.two_pager);
viewArrayList = new ArrayList<View>();
LayoutInflater layoutInflater = getLayoutInflater();
viewArrayList.add(layoutInflater.inflate(R.layout.view_one, null, false));
viewArrayList.add(layoutInflater.inflate(R.layout.view_two, null, false));
viewArrayList.add(layoutInflater.inflate(R.layout.view_three, null, false));
stringArrayList = new ArrayList<String>();
stringArrayList.add("一");
stringArrayList.add("二");
stringArrayList.add("三");
myPagerAdapter2 = new MyPagerAdapter2(viewArrayList, stringArrayList);
pager_two.setAdapter(myPagerAdapter2);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
startActivity(new Intent(twoActivity.this,MainActivity.class));
finish();
break;
}
}
}
三、 PagerTabStrip的使用
首先是创建threeActivity.java和activity_three.xml
1、在activity_three.xml加入控件PagerTabStrip
<?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"
android:orientation="vertical"
tools:context=".threeActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PagerTabStrip效果演示"
android:textColor="@android:color/white"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/button1"
android:textAllCaps="false"
android:background="@android:color/holo_blue_light"/>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/three_pager">
<android.support.v4.view.PagerTabStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pager_title"
android:background="@android:color/holo_blue_bright">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
2、这次不用重新写适配器了,接着沿用MyPagerAdapter2
3、在threeActivity中添加数据,这里与twoActivity区别不大
package com.example.viewpager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class threeActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager pager_three;
private ArrayList<View> viewArrayList;
private ArrayList<String> stringArrayList;
private MyPagerAdapter2 myPagerAdapter2;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
pager_three = (ViewPager) findViewById(R.id.three_pager);
viewArrayList = new ArrayList<View>();
LayoutInflater layoutInflater = getLayoutInflater();
viewArrayList.add(layoutInflater.inflate(R.layout.view_one, null, false));
viewArrayList.add(layoutInflater.inflate(R.layout.view_two, null, false));
viewArrayList.add(layoutInflater.inflate(R.layout.view_three, null, false));
stringArrayList = new ArrayList<String>();
stringArrayList.add("一");
stringArrayList.add("二");
stringArrayList.add("三");
myPagerAdapter2 = new MyPagerAdapter2(viewArrayList, stringArrayList);
pager_three.setAdapter(myPagerAdapter2);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
startActivity(new Intent(threeActivity.this,MainActivity.class));
finish();
break;
}
}
}
四、自定义标题栏
在一般情况下,标题栏是需要自定义的
首先新建fourActivity.java和activity_four.xml
1、activity_four.xml
使用LinearLayout布局在顶端放置三个textview 作为标题栏
然后是ViewPager
<?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"
android:orientation="vertical"
tools:context=".fourActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ViewPager实现TabHost的效果"
android:textColor="@android:color/white"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/button1"
android:textAllCaps="false"
android:background="@android:color/holo_blue_light"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="一"
android:gravity="center"
android:textColor="@android:color/black"
android:id="@+id/textview1"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="二"
android:gravity="center"
android:textColor="@android:color/black"
android:id="@+id/textview2"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="三"
android:gravity="center"
android:textColor="@android:color/black"
android:id="@+id/textview3"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/four_pager"></android.support.v4.view.ViewPager>
</LinearLayout>
2、适配器使用MyPagerAdapter
3、fourActivity添加数据
package com.example.viewpager;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class fourActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
private ViewPager pager_four;
private TextView textview1;
private TextView textview2;
private TextView textview3;
private Button btn1;
private ArrayList<View> listViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
initViews();
}
private void initViews() {
pager_four = (ViewPager) findViewById(R.id.four_pager);
textview1 = (TextView) findViewById(R.id.textview1);
textview2 = (TextView) findViewById(R.id.textview2);
textview3 = (TextView) findViewById(R.id.textview3);
//往ViewPager填充View,同时设置点击事件与页面切换事件
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
listViews.add(mInflater.inflate(R.layout.view_one, null, false));
listViews.add(mInflater.inflate(R.layout.view_two, null, false));
listViews.add(mInflater.inflate(R.layout.view_three, null, false));
pager_four.setAdapter(new MyPagerAdapter(listViews));
pager_four.setCurrentItem(0); //设置ViewPager当前页,从0开始算
textview1.setOnClickListener(this);
textview2.setOnClickListener(this);
textview3.setOnClickListener(this);
pager_four.addOnPageChangeListener(this);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.textview1:
pager_four.setCurrentItem(0);
break;
case R.id.textview2:
pager_four.setCurrentItem(1);
break;
case R.id.textview3:
pager_four.setCurrentItem(2);
break;
case R.id.button1:
startActivity(new Intent(fourActivity.this,MainActivity.class));
finish();
break;
}
}
@Override
public void onPageSelected(int index) {
//添加特效,相应界面的标题栏周围会出现边框
switch (index){
case 0:
textview1.setBackgroundResource(R.drawable.shape);
textview2.setBackgroundResource(R.drawable.normal_shape);
textview3.setBackgroundResource(R.drawable.normal_shape);
break;
case 1:
textview2.setBackgroundResource(R.drawable.shape);
textview1.setBackgroundResource(R.drawable.normal_shape);
textview3.setBackgroundResource(R.drawable.normal_shape);
break;
case 2:
textview3.setBackgroundResource(R.drawable.shape);
textview1.setBackgroundResource(R.drawable.normal_shape);
textview2.setBackgroundResource(R.drawable.normal_shape);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
@Override
public void onPageScrolled(int i, float v, int i1) {
}
}
使用到的shape(有框)和normal_shape(没框)
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp" android:color="#837575">
<stroke
android:width="1dp"
android:color="#837575"/>
</shape>
normal_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp" android:color="#837575">
</shape>
五、效果展示
为了方便截图,在一个界面中做了4个按钮,分别跳转那些界面