您的位置:首页 > 其它

ViewPage滑动切换点击切换

2014-12-04 17:35 197 查看




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.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewPageActivity extends Activity {
private ViewPager mPager;
private List<View> listViews;
private ImageView cursor;
private TextView t1, t2, t3;
private int offset = 0;
private int currIndex;
private int bmpW;
private int tabW;
private Toast mToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_page);

t1 = (TextView) findViewById(R.id.tv_top1);
t2 = (TextView) findViewById(R.id.tv_top2);
t3 = (TextView) findViewById(R.id.tv_top3);
mPager = (ViewPager) findViewById(R.id.vPage);
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
listViews.add(mInflater.inflate(R.layout.ly1, null));
listViews.add(mInflater.inflate(R.layout.ly2, null));
listViews.add(mInflater.inflate(R.layout.ly3, null));

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

bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
.getWidth();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (screenW / 3 - bmpW) / 2;
tabW = screenW / 3;
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix);
System.out.println("bmpW" + bmpW + "offset" + offset + "screenW"
+ screenW+"tabW"+tabW);

mPager.setAdapter(new MyPagerAdapter(listViews));
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());

t1.setOnClickListener(new MyOnClickListener(0));
t2.setOnClickListener(new MyOnClickListener(1));
t3.setOnClickListener(new MyOnClickListener(2));
}

public class MyOnClickListener implements OnClickListener {
private int index = 0;

public MyOnClickListener(int i) {
index = i;
}

@Override
public void onClick(View v) {
mPager.setCurrentItem(index);
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(),
"index" + index, Toast.LENGTH_SHORT);
}else{
mToast.setText("index" + index);
}
mToast.show();
}

}

public class MyPagerAdapter extends PagerAdapter {
public List<View> mListView;

public MyPagerAdapter(List<View> mlistViews) {
this.mListView = mlistViews;
}

@Override
public int getCount() {
System.out.println(listViews.size() + "listViews");
return listViews.size();
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

// super.destroyItem(container, position, object);
container.removeView(mListView.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// return super.instantiateItem(container, position);
container.addView(mListView.get(position), 0);
return mListView.get(position);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
// return false;
}

}

public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW;
int two = one * 2;

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
Animation animation = null;
animation = new TranslateAnimation(tabW*currIndex,tabW*arg0 , 0, 0);
currIndex = arg0;
animation.setFillAfter(true);
animation.setDuration(300);
cursor.startAnimation(animation);
}

}
}


布局:

<LinearLayout 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:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/linlayout_top"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>

<TextView
android:gravity="center"
android:id="@+id/tv_top1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="page1" />
<TextView
android:gravity="center"
android:id="@+id/tv_top2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="page2" />
<TextView
android:gravity="center"
android:id="@+id/tv_top3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="page3" />

</LinearLayout>
<ImageView
android:id="@+id/cursor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#000000"
android:persistentDrawingCache="animation"
/>

</LinearLayout>


viewpage的item布局:
<?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:background="#11ff12" >

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: