开发工具:
文件大小: 863kb
下载次数: 0
上传时间: 2015-01-15
详细说明: 标题左右滑动,标题下划线有滑动效果 /* * @author http://blog.csdn.net/singwhatiwanna */ package com.ryg.fragment.ui; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Message; import android.os.Parcel; import android.os.Parcelable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.Fragm entPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.ViewGroup; import android.view.Window; import com.ryg.indicatorfragment.R; @SuppressWarnings("static-access") public abstract class IndicatorFragmentActivity extends FragmentActivity implements OnPageChangeListener { private static final String TAG = "DxFragmentActivity"; public static final String EXTRA_TAB = "tab"; public static final String EXTRA_QUIT = "extra.quit"; protected int mCurrentTab = 0; protected int mLastTab = -1; //存放选项卡信息的列表 protected ArrayList mTabs = new ArrayList(); //viewpager adapter protected MyAdapter myAdapter = null; //viewpager protected ViewPager mPager; //选项卡控件 protected TitleIndicator mIndicator; public TitleIndicator getIndicator() { return mIndicator; } public class MyAdapter extends FragmentPagerAdapter { ArrayList tabs = null; Context context = null; public MyAdapter(Context context, FragmentManager fm, ArrayList tabs) { super(fm); this.tabs = tabs; this.context = context; } @Override public Fragment getItem(int pos) { Fragment fragment = null; if (tabs != null && pos < tabs.size()) { TabInfo tab = tabs.get(pos); if (tab == null) return null; fragment = tab.createFragment(); } return fragment; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } @Override public int getCount() { if (tabs != null && tabs.size() > 0) return tabs.size(); return 0; } @Override public Object instantiateItem(ViewGroup container, int position) { TabInfo tab = tabs.get(position); Fragment fragment = (Fragment) super.instantiateItem(container, position); tab.fragment = fragment; return fragment; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(getMainViewResId()); initViews(); //设置viewpager内部页面之间的间距 mPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin_width)); //设置viewpager内部页面间距的drawable mPager.setPageMarginDrawable(R.color.page_viewer_margin_color); } @Override protected void onDestroy() { mTabs.clear(); mTabs = null; myAdapter.notifyDataSetChanged(); myAdapter = null; mPager.setAdapter(null); mPager = null; mIndicator = null; super.onDestroy(); } private final void initViews() { // 这里初始化界面 mCurrentTab = supplyTabs(mTabs); Intent intent = getIntent(); if (intent != null) { mCurrentTab = intent.getIntExtra(EXTRA_TAB, mCurrentTab); } Log.d(TAG, "mTabs.size() == " + mTabs.size() + ", cur: " + mCurrentTab); myAdapter = new MyAdapter(this, getSupportFragmentManager(), mTabs); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(myAdapter); mPager.setOnPageChangeListener(this); mPager.setOffscreenPageLimit(mTabs.size()); mIndicator = (TitleIndicator) findViewById(R.id.pagerindicator); mIndicator.init(mCurrentTab, mTabs, mPager); mPager.setCurrentItem(mCurrentTab); mLastTab = mCurrentTab; } /** * 添加一个选项卡 * @param tab */ public void addTabInfo(TabInfo tab) { mTabs.add(tab); myAdapter.notifyDataSetChanged(); } /** * 从列表添加选项卡 * @param tabs */ public void addTabInfos(ArrayList tabs) { mTabs.addAll(tabs); myAdapter.notifyDataSetChanged(); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { mIndicator.onScrolled((mPager.getWidth() + mPager.getPageMargin()) * position + positionOffsetPixels); } @Override public void onPageSelected(int position) { mIndicator.onSwitched(position); mCurrentTab = position; } @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_IDLE) { mLastTab = mCurrentTab; } } protected TabInfo getFragmentById(int tabId) { if (mTabs == null) return null; for (int index = 0, count = mTabs.size(); index < count; index++) { TabInfo tab = mTabs.get(index); if (tab.getId() == tabId) { return tab; } } return null; } /** * 跳转到任意选项卡 * @param tabId 选项卡下标 */ public void navigate(int tabId) { for (int index = 0, count = mTabs.size(); index < count; index++) { if (mTabs.get(index).getId() == tabId) { mPager.setCurrentItem(index); } } } @Override public void onBackPressed() { finish(); } /** * 返回layout id * @return layout id */ protected int getMainViewResId() { return R.layout.titled_fragment_tab_activity; } /** * 在这里提供要显示的选项卡数据 */ protected abstract int supplyTabs(List tabs); @Override protected void onSaveInstanceState(Bundle outState) { // for fix a known issue in support library // https://code.google.com/p/android/issues/detail?id=19917 outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE"); super.onSaveInstanceState(outState); } /** * 单个选项卡类,每个选项卡包含名字,图标以及提示(可选,默认不显示) */ public static class TabInfo implements Parcelable { private int id; private int icon; private String name = null; public boolean hasTips = false; public Fragment fragment = null; public boolean notifyChange = false; @SuppressWarnings("rawtypes") public Class fragmentClass = null; @SuppressWarnings("rawtypes") public TabInfo(int id, String name, Class clazz) { this(id, name, 0, clazz); } @SuppressWarnings("rawtypes") public TabInfo(int id, String name, boolean hasTips, Class clazz) { this(id, name, 0, clazz); this.hasTips = hasTips; } @SuppressWarnings("rawtypes") public TabInfo(int id, String name, int iconid, Class clazz) { super(); this.name = name; this.id = id; icon = iconid; fragmentClass = clazz; } public TabInfo(Parcel p) { this.id = p.readInt(); this.name = p.readString(); this.icon = p.readInt(); this.notifyChange = p.readInt() == 1; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setIcon(int iconid) { icon = iconid; } public int getIcon() { return icon; } @SuppressWarnings({ "rawtypes", "unchecked" }) public Fragment createFragment() { if (fragment == null) { Constructor constructor; try { constructor = fragmentClass.getConstructor(new Class[0]); fragment = (Fragment) constructor.newInstance(new Object[0]); } catch (Exception e) { e.printStackTrace(); } } return fragment; } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public TabInfo createFromParcel(Parcel p) { return new TabInfo(p); } public TabInfo[] newArray(int size) { return new TabInfo[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel p, int flags) { p.writeInt(id); p.writeString(name); p.writeInt(icon); p.writeInt(notifyChange ? 1 : 0); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.