PagerView简单使用

2018.04.14

实现效果:

PagerView简单使用_第1张图片
第一个Page
PagerView简单使用_第2张图片
第二个Page

实现代码:

创建适配器 MyPagerAdapter

import android.support.v4.view.PagerAdapter;

import android.view.View;

import android.view.ViewGroup;

import java.util.ArrayList;

/**

* Created by Jay on 2015/10/8 0008.

*/

public class MyPagerAdapterextends PagerAdapter {

private ArrayListviewLists;

public MyPagerAdapter() {

}

public MyPagerAdapter(ArrayList viewLists) {

super();

this.viewLists = viewLists;

}

@Override

    public int getCount() {

return viewLists.size();

}

@Override

    public boolean isViewFromObject(View view, Object object) {

return view == object;

}

@Override

    public Object instantiateItem(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));

}

}


主函数实现:

private void initViews() {

st=findViewById(R.id.start);

en=findViewById(R.id.end);

vpager = findViewById(R.id.vpager);

tv_one = findViewById(R.id.tv_one);

tv_two = findViewById(R.id.tv_two);

tv_three = findViewById(R.id.tv_three);

tv_four = findViewById(R.id.tv_four);

img_cursor = findViewById(R.id.img_cursor);

//下划线动画的相关设置:

    bmpWidth = BitmapFactory.decodeResource(getResources(), R.mipmap.line).getWidth();// 获取图片宽度

    DisplayMetrics dm =new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

int screenW = dm.widthPixels;// 获取分辨率宽度

    offset = (screenW /4 -bmpWidth) /2;// 计算偏移量

    Matrix matrix =new Matrix();

matrix.postTranslate(offset,0);

img_cursor.setImageMatrix(matrix);// 设置动画初始位置

//移动的距离

    one =offset *2 +bmpWidth;// 移动一页的偏移量,比如1->2,或者2->3

    two =one *2;// 移动两页的偏移量,比如1直接跳3

    three =one *3;

//往ViewPager填充View,同时设置点击事件与页面切换事件

    listViews =new ArrayList();

LayoutInflater mInflater = getLayoutInflater();

View view1=mInflater.inflate(R.layout.view_one,null,false);

initPagerView1(view1,0,900);

View view2=mInflater.inflate(R.layout.view_two,null,false);

initPagerView3(view2,"A",0,900);

View view3=mInflater.inflate(R.layout.view_three,null,false);

initPagerView3(view3,"V",0,900);

View view4=mInflater.inflate(R.layout.view_four,null,false);

initPagerView2(view4,0,900);

listViews.add(view1);

listViews.add(view2);

listViews.add(view3);

listViews.add(view4);

vpager.setAdapter(new MyPagerAdapter(listViews));

vpager.setCurrentItem(0);//设置ViewPager当前页,从0开始算

    tv_one.setOnClickListener(this);

tv_two.setOnClickListener(this);

tv_three.setOnClickListener(this);

tv_four.setOnClickListener(this);

vpager.addOnPageChangeListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.tv_one:

vpager.setCurrentItem(0);

break;

case R.id.tv_two:

vpager.setCurrentItem(1);

break;

case R.id.tv_three:

vpager.setCurrentItem(2);

break;

case R.id.tv_four:

vpager.setCurrentItem(3);

break;

}

}

@Override

public void onPageSelected(int index) {

Animation animation =null;

switch (index) {

case 0:

if (currIndex ==1) {

animation =new TranslateAnimation(one,0,0,0);

}else if (currIndex ==2) {

animation =new TranslateAnimation(two,0,0,0);

}else if (currIndex ==3) {

animation =new TranslateAnimation(three,0,0,0);

}

break;

case 1:

if (currIndex ==0) {

animation =new TranslateAnimation(offset,one,0,0);

}else if (currIndex ==2) {

animation =new TranslateAnimation(two,one,0,0);

}else if (currIndex ==3) {

animation =new TranslateAnimation(three,one,0,0);

}

break;

case 2:

if (currIndex ==0) {

animation =new TranslateAnimation(offset,two,0,0);

}else if (currIndex ==1) {

animation =new TranslateAnimation(one,two,0,0);

}else if (currIndex ==3) {

animation =new TranslateAnimation(three,two,0,0);

}

break;

case 3:

if (currIndex ==0) {

animation =new TranslateAnimation(offset,three,0,0);

}else if (currIndex ==1) {

animation =new TranslateAnimation(one,three,0,0);

}else if (currIndex ==2) {

animation =new TranslateAnimation(two,three,0,0);

}

break;

}

currIndex = index;

animation.setFillAfter(true);// true表示图片停在动画结束位置

    animation.setDuration(200);//设置动画时间为300毫秒

    img_cursor.startAnimation(animation);//开始动画

}

@Override

public void onPageScrollStateChanged(int i) {

}

@Override

public void onPageScrolled(int i,float v,int i1) {

}

initPagerView函数

private void initPagerView1(View view,float yMin,float yMax){

float[] datas1 = {536,123,769,432,102,26,94,85};

List dataList1=new ArrayList<>();

for(int i=0;i

dataList1.add(new Data(i+12+":00",datas1[i]+"kwh"));

}

LineChart Chart=view.findViewById(R.id.chart);

LineData lineData = MyLineChart.initSingleLineChart(this,datas1.length, datas1);

MyLineChart.initDataStyle(Chart, lineData,this,"kwh",yMin,yMax);

RecyclerView recyclerView=view.findViewById(R.id.recycler_view);

LinearLayoutManager layoutManager=new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManager);

DataAdapter1 adapter=new DataAdapter1(dataList1);

recyclerView.setAdapter(adapter);

}

private void initPagerView2(View view,float yMin,float yMax){

List dataList2=new ArrayList<>();

float[] datas1 = {536,123,769,432,102,26,94,85};

float[] datas2 = {10,87,355,432,900,800,700,85};

for(int i=0;i

dataList2.add(new Data(i+12+":00",datas1[i]+"°C",datas2[i]+"°C"));

}

LineChart Chart=view.findViewById(R.id.chart);

LineData lineData = MyLineChart.initDoubleLineChart(this,datas1.length, datas1,datas2);

MyLineChart.initDataStyle(Chart, lineData,this,"°C",yMin,yMax);

RecyclerView recyclerView=view.findViewById(R.id.recycler_view);

LinearLayoutManager layoutManager=new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManager);

DataAdapter2 adapter=new DataAdapter2(dataList2);

recyclerView.setAdapter(adapter);

}

private void initPagerView3(View view,String Unit,float yMin,float yMax){

List dataList3=new ArrayList<>();

float[] datas1 = {536,123,769,432,102,26,94,85};

float[] datas2 = {10,87,355,432,900,800,700,85};

float[] datas3 = {877,500,355,647,590,400,307,85};

for(int i=0;i

dataList3.add(new Data(i+12+":00",datas1[i]+Unit,datas2[i]+Unit,datas3[i]+Unit));

}

LineChart Chart=view.findViewById(R.id.chart);

LineData lineData = MyLineChart.initTrebleLineChart(this,datas1.length, datas1,datas2,datas3);

MyLineChart.initDataStyle(Chart, lineData,this,Unit,yMin,yMax);

RecyclerView recyclerView=view.findViewById(R.id.recycler_view);

LinearLayoutManager layoutManager=new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManager);

DataAdapter3 adapter=new DataAdapter3(dataList3);

recyclerView.setAdapter(adapter);

}


有关MyLineChart参见https://www.jianshu.com/p/43aa4766737b。

你可能感兴趣的:(PagerView简单使用)