参考:http://www.lcode.org/horizontalscrollviewfragmentfragmentstatepageradapter%E6%89%93%E9%80%A0%E7%BD%91%E6%98%93%E6%96%B0%E9%97%BBtab%E5%8F%8A%E6%BB%91%E5%8A%A8%E9%A1%B5%E9%9D%A2%E6%95%88%E6%9E%9C/
主要代码:
public class GoodsMgrFragment extends BaseFragmentActivity implements ViewPager.OnPageChangeListener { ViewPager info_viewpager; private List fragments; private CNKFixedPagerAdapter mPagerAdater; private String[] titles = new String[]{"销售中", "待上架", "审核中", "审核失败"}; /** * 当前选择的分类 */ private int mCurClassIndex = 0; /** * 选择的分类字体颜色 */ private int mColorSelected; /** * 非选择的分类字体颜色 */ private int mColorUnSelected; /** * 水平滚动的Tab容器 */ private HorizontalScrollView mScrollBar; /** * 分类导航的容器 */ private ViewGroup mClassContainer; /** * 水平滚动X */ private int mScrollX = 0; int width, height; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_mgr_main); initViews(); initValidata(); } /** * 初始化布局控件 */ private void initViews() { info_viewpager = (ViewPager) findViewById(R.id.info_viewpager); mScrollBar = (HorizontalScrollView) findViewById(R.id.horizontal_info); mClassContainer = (ViewGroup) findViewById(R.id.linearlayout_container); WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); width = wm.getDefaultDisplay().getWidth(); height = wm.getDefaultDisplay().getHeight(); } private void initValidata() { mColorSelected = getResources().getColor(R.color.text_refund_money); mColorUnSelected = getResources().getColor(R.color.text_lv_1); //添加Tab标签 addScrollView(titles); mScrollBar.post(new Runnable() { @Override public void run() { mScrollBar.scrollTo(mScrollX, 0); } }); fragments = new ArrayList<>(); for (int i = 0; i < 4; i++) { MgrContentFragment oneFragment = new MgrContentFragment(); Bundle bundle = new Bundle(); bundle.putString("extra", titles[i]); oneFragment.setArguments(bundle); fragments.add(oneFragment); } mPagerAdater = new CNKFixedPagerAdapter(getSupportFragmentManager()); mPagerAdater.setTitles(titles); mPagerAdater.setFragments(fragments); info_viewpager.setAdapter(mPagerAdater); info_viewpager.setOnPageChangeListener(this); } /** * 动态添加顶部Tab滑动的标签 * * @param titles */ private void addScrollView(String[] titles) { LayoutInflater mLayoutInflater = LayoutInflater.from(this); final int count = titles.length; for (int i = 0; i < count; i++) { final String title = titles[i]; final View view = mLayoutInflater.inflate(R.layout.horizontal_item_layout, null); view.setLayoutParams(new LinearLayout.LayoutParams(width / count, LinearLayout.LayoutParams.MATCH_PARENT)); final TextView tv_goods_type = (TextView) view.findViewById(R.id.tv_goods_type); final TextView tv_goods_count = (TextView) view.findViewById(R.id.tv_goods_count); tv_goods_type.setText(title); if (i == mCurClassIndex) { //已经选中 tv_goods_type.setTextColor(mColorSelected); tv_goods_count.setTextColor(mColorSelected); } else { //未选中 tv_goods_type.setTextColor(mColorUnSelected); tv_goods_count.setTextColor(mColorUnSelected); } final int index = i; //点击顶部Tab标签,动态设置下面的ViewPager页面 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //首先设置当前的Item为正常状态 View currentItem = mClassContainer.getChildAt(mCurClassIndex); ((TextView) (currentItem.findViewById(R.id.tv_goods_type))).setTextColor(mColorUnSelected); ((TextView) (currentItem.findViewById(R.id.tv_goods_count))).setTextColor(mColorUnSelected); mCurClassIndex = index; //设置点击状态 tv_goods_type.setTextColor(mColorSelected); tv_goods_count.setTextColor(mColorSelected); //跳转到指定的ViewPager info_viewpager.setCurrentItem(mCurClassIndex); } }); mClassContainer.addView(view); } } //下面三个回调方法 分别是在ViewPager进行滑动的时候调用 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //首先设置当前的Item为正常状态 View preView = mClassContainer.getChildAt(mCurClassIndex); ((TextView) (preView.findViewById(R.id.tv_goods_type))).setTextColor(mColorUnSelected); ((TextView) (preView.findViewById(R.id.tv_goods_count))).setTextColor(mColorUnSelected); mCurClassIndex = position; //设置当前为选中状态 View currentItem = mClassContainer.getChildAt(mCurClassIndex); ((TextView) (currentItem.findViewById(R.id.tv_goods_type))).setTextColor(mColorSelected); ((TextView) (currentItem.findViewById(R.id.tv_goods_count))).setTextColor(mColorSelected); //这边移动的距离 是经过计算粗略得出来的 mScrollX = currentItem.getLeft() - 300; mScrollBar.post(new Runnable() { @Override public void run() { mScrollBar.scrollTo(mScrollX, 0); } }); } @Override public void onPageScrollStateChanged(int state) { } }
布局文件:fragment_mgr_main
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> layout="@layout/include_title_bar"/> layout="@layout/include_view_x1px"/> android:id="@+id/horizontal_info" android:layout_width="fill_parent" android:layout_height="48dp" android:scrollbars="none"> android:id="@+id/linearlayout_container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> layout="@layout/include_view_x10dp"/> android:id="@+id/info_viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent"/>