本代码有缺陷,如果回到桌面后,资源被系统回收后会报错。
新文章: http://blog.csdn.net/q445697127/article/details/18352033
Activity继承FragmentActivity
package com.bitcare.activity;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Matrix;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bitcare.adapter.FmtPagerAdapter;
import com.bitcareV2.activity.R;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.NoTitle;
import com.googlecode.androidannotations.annotations.ViewById;
@EActivity(R.layout.activity_hospital)
public class HospitalActivity extends FragmentActivity {
@ViewById
TextView tvTitle;
@ViewById
TextView tvProfiles, tvOffice, tvEvaluate;
@ViewById
ImageButton ibLeft;
@ViewById
ImageView ivBottomLine;
@ViewById
ViewPager vpHospital;
private List<BaseFragment> fragments;
private int currIndex = 0;
private int bottomLineWidth;
private int offset = 0;
@AfterViews
void initView() {
tvTitle.setText("南昌大学第二附属医院");
ibLeft.setVisibility(View.VISIBLE);
InitWidth();
InitViewPager();
}
private void InitWidth() {
int screenWidth;// 屏幕宽度 如果是使用图片可以使用另一篇文章的宽度计算方法
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
bottomLineWidth = screenWidth / 3;
LayoutParams layoutParams = ivBottomLine.getLayoutParams();
layoutParams.width = bottomLineWidth;
ivBottomLine.setLayoutParams(layoutParams);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 获取分辨率宽度
offset = (screenW / 3 - bottomLineWidth) / 2;// 计算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
ivBottomLine.setImageMatrix(matrix);// 设置动画初始位置
}
private HospitalProfilesFragment_ profilesFragment_;
private HospitalOfficeFragment_ officeFragment_;
private HospitalEvaluateFragment_ evaluateFragment_;
private void InitViewPager() {
fragments = new ArrayList<BaseFragment>();
profilesFragment_ = new HospitalProfilesFragment_();
officeFragment_ = new HospitalOfficeFragment_();
evaluateFragment_ = new HospitalEvaluateFragment_();
fragments.add(profilesFragment_);
fragments.add(officeFragment_);
fragments.add(evaluateFragment_);
vpHospital.setAdapter(new FmtPagerAdapter(getSupportFragmentManager(), fragments));
vpHospital.setOnPageChangeListener(changeListener);
tvProfiles.setTextColor(getResources().getColor(R.color.black_txt));
}
@Click
void ibLeft() {
finish();
}
@Click
void tvProfiles() {
vpHospital.setCurrentItem(0);
}
@Click
void tvOffice() {
vpHospital.setCurrentItem(1);
}
@Click
void tvEvaluate() {
vpHospital.setCurrentItem(2);
}
OnPageChangeListener changeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
int one = offset * 2 + bottomLineWidth;// 页卡1 -> 页卡2 偏移量
Animation animation = new TranslateAnimation(one * currIndex, one * arg0, 0, 0);// 显然这个比较简洁,只有一行代码。
currIndex = arg0;
animation.setFillAfter(true);
animation.setDuration(100);
ivBottomLine.startAnimation(animation);
tvProfiles.setTextColor(getResources().getColor(R.color.gray_txt));
tvOffice.setTextColor(getResources().getColor(R.color.gray_txt));
tvEvaluate.setTextColor(getResources().getColor(R.color.gray_txt));
switch (arg0) {
case 0:
tvProfiles.setTextColor(getResources().getColor(R.color.black_txt));
break;
case 1:
tvOffice.setTextColor(getResources().getColor(R.color.black_txt));
break;
case 2:
tvEvaluate.setTextColor(getResources().getColor(R.color.black_txt));
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
}
为了做延迟加载数据需要自定义一个BaseFragment类
package com.bitcare.activity;
import android.support.v4.app.Fragment;
public abstract class BaseFragment extends Fragment {
/**
* 显示页面数据
*/
public abstract void show();
}
在写一个FragmentPagerAdapter类
package com.bitcare.adapter;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
import com.bitcare.activity.BaseFragment;
public class FmtPagerAdapter extends FragmentPagerAdapter {
private List<BaseFragment> fragmentsList;
private FragmentManager fm;
public FmtPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
public FmtPagerAdapter(FragmentManager fm, List<BaseFragment> fragments) {
super(fm);
this.fragmentsList = fragments;
this.fm = fm;
}
@Override
public int getCount() {
return fragmentsList.size();
}
@Override
public Fragment getItem(int arg0) {
return fragmentsList.get(arg0);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
//知道当前是第几页,但是每次滑动后可能会调用多次
//这个方法是重点
super.setPrimaryItem(container, position, object);
fragmentsList.get(position).show();
}
@Override
public int getItemPosition(Object object) {
//加此方法可以使viewpager可以进行刷新
return PagerAdapter.POSITION_NONE;
}
//使用此方法刷新数据 每次都要NEW一个新的List,不然没有刷新效果 转至http://blog.sina.com.cn/s/blog_783ede03010173b4.html
public void setFragments(ArrayList<Fragment> fragments) {
if(this.fragments != null){
FragmentTransaction ft = fm.beginTransaction();
for(Fragment f:this.fragments){
ft.remove(f);
}
ft.commit();
ft=null;
fm.executePendingTransactions();
}
this.fragments = fragments;
notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 注释自带的销毁方法防止页面被销毁
//这个方法是重点
// super.destroyItem(container, position, object);}
}
给每个单独的页面继承BaseFragment即可,在show方法中实现调用数据的方法,但是注意的是每次滑动会多次调用show方法
版权声明:本文为博主原创文章,未经博主允许不得转载。