本文出自 “雨轩印象” 博客,请务必保留此出处http://zilla.blog.51cto.com/3095640/967387
之前首页的框架采用TabActivity+Activity的形式实现,首页页面切换时,性能消耗较大,本次修改可以大大节约页面切换性能。
下面是这次框架调整的一些具体内容
思路
使用ViewPager作为首页的容器,替换TabActivity;
使用Fragment作为具体页面的容器,替换Activity;
将Fragment添加到ViewPager中,以实现页面切换。
实现
ViewPager实现
引入:
ViewPager可以通过layout
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:background="#ffffff" android:flipInterval="30" android:persistentDrawingCache="animation" android:layout_centerInParent="true" > </android.support.v4.view.ViewPager>
public class MainFragmentPagerAdapter extends FragmentPagerAdapter { private ArrayList<Fragment> fragments; public MainFragmentPagerAdapter(FragmentManager fm) { super(fm); // TODO Auto-generated constructor stub } public MainFragmentPagerAdapter(FragmentManager fm,ArrayList<Fragment> fragments){ super(fm); this.fragments = fragments; } /* (non-Javadoc) * @see android.support.v4.app.FragmentPagerAdapter#getItem(int) */ @Override public Fragment getItem(int arg0) { return fragments.get(arg0); } /* (non-Javadoc) * @see android.support.v4.view.PagerAdapter#getCount() */ @Override public int getCount() { return fragments.size(); } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } }
viewPager.setOnPageChangeListener(pageChangeListener); viewPager.setOnTouchListener(touchListener); pageChangeListener定义如下: private OnPageChangeListener pageChangeListener = new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { setIconSelected(arg0); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }; touchListener定义如下: private OnTouchListener touchListener = new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if (currentIndex != 0) { return false; } int[] location = new int[2]; homeFragment.gallery.getLocationOnScreen(location); if (location[0] != 0) { return false; } if (event.getRawY() > location[1] && event.getRawY() - location[1] < homeFragment.gallery .getHeight()) { return homeFragment.gallery.dispatchTouchEvent(event); } return false; } };
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.setting); getView(); setListener(); }
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Utils.log("onCreateView"); View v = inflater.inflate(R.layout.setting, container, false); getViews(v); setListener(); return v; }
private OnTouchListener touchListener = new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if (currentIndex != 0) { return false; } int[] location = new int[2]; homeFragment.gallery.getLocationOnScreen(location); if (location[0] != 0) { return false; } if (event.getRawY() > location[1] && event.getRawY() - location[1] < homeFragment.gallery .getHeight()) { return homeFragment.gallery.dispatchTouchEvent(event); } return false; } };