public class MyFragment extends Fragment {
private View view;
private ViewPager mPager;
private DefaultPagerAdapter mPagerAdapter;
private TextView pageone ;
private TextView pagetwo ;
private TextView pagethree ;
private TextView pagefour ;
private ImageView cursor;
private int bmpW;
private int offset;
private int currIndex = 0;
public static DefaultFragment newInstance() {
return new DefaultFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.new_default_layout, container, false);
init();
return view;
}
private void InitImageView() {
cursor = (ImageView) view.findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 获取图片宽度
DisplayMetrics dm = new DisplayMetrics();
this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (screenW / 4 - bmpW) / 2; // 计算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix); // 设置动画初始位置
}
@Override
public void onResume() {
super.onResume();
}
private void init(){
pageone = (TextView) view.findViewById(R.id.pageone);
pagetwo = (TextView) view.findViewById(R.id.pagetwo);
pagethree = (TextView) view.findViewById(R.id.pagethree);
pagefour = (TextView) view.findViewById(R.id.pagefour);
mPager = (ViewPager)view.findViewById(R.id.pager);
mPagerAdapter = new DefaultPagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(0);//默认为第一页
InitImageView();
mPager.setOnPageChangeListener(new OnPageChangeListener() {
int one = offset * 2 + bmpW; // 页卡1 -> 页卡2 偏移量
int two = one * 2; // 页卡1 -> 页卡3 偏移量
int three = one * 3; // 页卡1 -> 页卡4 偏移量
@Override
public void onPageSelected(int position) {
Animation animation = null;
switch (position) {
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 = position;//保存当前页
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(300);
cursor.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public class DefaultPagerAdapter extends FragmentStatePagerAdapter {
private final String TAG = DefaultMsgAdapter.class.getSimpleName();
public DefaultPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int position) {
try {
switch (position) {
case 0:
return DefaultPager1Fragment.newInstance();
case 1:
return DefaultPager2Fragment.newInstance();
case 2:
return DefaultPager3Fragment.newInstance();
case 3:
return DefaultPager4Fragment.newInstance();
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/grey"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="第一"
android:textSize="20sp"
android:id="@+id/pageone"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="第二"
android:textSize="20sp"
android:id="@+id/pagetwo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="第二"
android:textSize="20sp"
android:id="@+id/pagethree"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="第三"
android:textSize="20sp"
android:id="@+id/pagefour"
/>
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="8dip"
android:scaleType="matrix"
android:src="@drawable/a"
android:id="@+id/cursor"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
ViewPage实现
最新推荐文章于 2019-10-10 08:56:19 发布