首页 > 编程学习 > Android UI--ViewPager扩展Tab标签指示,android开发网上购物app

布局搞定之后,定义一个适配器如下:

package com.wwj.viewpager;

import java.util.List;

import android.support.v4.view.PagerAdapter;

import android.view.View;

import android.view.ViewGroup;

/**

  • ViewPager适配器

  • @author wwj

*/

public class ViewPagerAdapter extends PagerAdapter {

// 界面列表

private List views;

public ViewPagerAdapter(List views) {

this.views = views;

}

@Override

public void destroyItem(ViewGroup container, int position, Object o
bject) {

container.removeView(views.get(position));

}

@Override

public int getCount() {

if (views != null) {

return views.size();

}

return 0;

}

@Override

public Object instantiateItem(ViewGroup container, int position) {

container.addView(views.get(position)); // 把被点击的图片放入缓存中

return views.get(position); // 返回被点击图片对象

}

@Override

public boolean isViewFromObject(View arg0, Object arg1) {

return (arg0 == arg1);

}

}

适配器搞定之后,直接去Activity

package com.wwj.viewpager;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.os.Bundle;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.util.DisplayMetrics;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.TranslateAnimation;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

ViewPager viewPager;

ViewPagerAdapter pagerAdapter;

View view1, view2, view3, view4; // 页面

List views; // Tab页面列表

ImageView img_cursor; // 指示图片

TextView tv_guid1, tv_guid2, tv_guid3, tv_guid4; // 页卡头标

int offset = 0; // 偏移量

int currIndex = 0; // 当前页卡编号

int bmpW; // 图片宽度

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initImageView();

initTextView();

view1 = (View) findViewById(R.layout.viewpager1);

view2 = (View) findViewById(R.layout.viewpager2);

view3 = (View) findViewById(R.layout.viewpager3);

view4 = (View) findViewById(R.layout.viewpager4);

LayoutInflater inflater = LayoutInflater.from(this);

view1 = inflater.inflate(R.layout.viewpager1, null);

view2 = inflater.inflate(R.layout.viewpager2, null);

view3 = inflater.inflate(R.layout.viewpager3, null);

view4 = inflater.inflate(R.layout.viewpager4, null);

views = new ArrayList();

views.add(view1);

views.add(view2);

views.add(view3);

views.add(view4);

pagerAdapter = new ViewPagerAdapter(views);

viewPager = (ViewPager) findViewById(R.id.viewpager);

viewPager.setAdapter(pagerAdapter);

viewPager.setCurrentItem(0);

viewPager.setOnPageChangeListener(new MyOnPageChangeListener());

}

/**

  • 初始化动画图片

*/

private void initImageView() {

img_cursor = (ImageView) findViewById(R.id.cursor);

bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.cursor)

.getWidth();

DisplayMetrics dm = new DisplayMetrics();

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

int screenW = dm.widthPixels; // 获取手机屏幕宽度分辨率

offset = (screenW / 4 - bmpW) / 2; // 获取图片偏移量

// imageview设置平移,使下划线平移到初始位置(平移一个offset)

Matrix matrix = new Matrix();

matrix.postTranslate(offset, 0);

img_cursor.setImageMatrix(matrix);

}

/**

  • 初始化TextView控件,和注册监听器

*/

private void initTextView() {

tv_guid1 = (TextView) findViewById(R.id.tv_guid1);

tv_guid2 = (TextView) findViewById(R.id.tv_guid2);

tv_guid3 = (TextView) findViewById(R.id.tv_guid3);

tv_guid4 = (TextView) findViewById(R.id.tv_guid4);

tv_guid1.setOnClickListener(listener);

tv_guid2.setOnClickListener(listener);

tv_guid3.setOnClickListener(listener);

tv_guid4.setOnClickListener(listener);

}

OnClickListener listener = new OnClickListener() {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.tv_guid1:

viewPager.setCurrentItem(0);

break;

case R.id.tv_guid2:

viewPager.setCurrentItem(1);

break;

case R.id.tv_guid3:

viewPager.setCurrentItem(2);

break;

case R.id.tv_guid4:

viewPager.setCurrentItem(3);

break;

default:

break;

}

}

};

// 当页面滑动时,开始动画并跳出Toast

public class MyOnPageChangeListener implements OnPageChangeListener {

private int one = offset * 2 + bmpW; // 页面1到页面2的偏移量

@Override

public void onPageScrollStateChanged(int arg0) {

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

ntItem(3);

break;

default:

break;

}

}

};

// 当页面滑动时,开始动画并跳出Toast

public class MyOnPageChangeListener implements OnPageChangeListener {

private int one = offset * 2 + bmpW; // 页面1到页面2的偏移量

@Override

public void onPageScrollStateChanged(int arg0) {

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000