PagerSlidingTabStrip
因為工作需要一個Tab 滑動,並且下方有三個功能項目不斷滑動的功能
剛好有一個符合需求
https://github.com/astuetz/PagerSlidingTabStrip
再以拿出自己想要的,做簡單做成下面這樣。
首先要在Activity的onCreate加入以下程式碼
private MyPagerAdapter adapter;
public void initTabViewPager() { //Initialize the ViewPager and set an adapter adapter = new MyPagerAdapter(getSupportFragmentManager()); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); // Bind the tabs to the ViewPager PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); tabs.setShouldExpand(true); tabs.setViewPager(pager); tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); }
一樣在Activity定義Adapter
public class MyPagerAdapter extends FragmentPagerAdapter { private final String[] TITLES = { "線上詢問","應徵紀錄","信件通知" }; public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } @Override public int getCount() { return TITLES.length; } @Override public Fragment getItem(int position) { return MyFragment.newInstance(position,TITLES[position]); } }
另外在編寫一個Fragment
import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.FrameLayout.LayoutParams; import android.widget.TextView; public class MyFragment extends Fragment { private static final String ARG_POSITION = "position"; private static final String ARG_TITLE = "title"; private int position; private String title; public static MyFragment newInstance(int position,String title) { MyFragment f = new MyFragment(); Bundle b = new Bundle(); b.putInt(ARG_POSITION, position); b.putString(ARG_TITLE, title); f.setArguments(b); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); position = getArguments().getInt(ARG_POSITION); title = getArguments().getString(ARG_TITLE); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); FrameLayout fl = new FrameLayout(getActivity()); fl.setLayoutParams(params); final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources() .getDisplayMetrics()); TextView v = new TextView(getActivity()); params.setMargins(margin, margin, margin, margin); v.setLayoutParams(params); v.setLayoutParams(params); v.setGravity(Gravity.CENTER); //v.setBackgroundResource(R.drawable.background_card); v.setText(title); fl.addView(v); return fl; } }
另外gradle加入
dependencies { compile 'com.astuetz:pagerslidingtabstrip:1.0.1' }
不確定gradle這樣加是否還能完全執行,因為我有加其它的
如果不行,再加其它的
dependencies { compile 'com.google.android.gms:play-services:8.1.0' compile 'com.android.support:appcompat-v7:21.0.1' compile 'com.android.support:support-v4:21.0.0' compile 'com.android.support:multidex:1.0.0' compile 'com.astuetz:pagerslidingtabstrip:1.0.1' }
因為本身版本是minSdk是10,而一般的Android Fragment需要的是11,
但由於作者使用的是 support library v4,所以可以向下相容
import android.support.v4.app.Fragment;
support library v4 fragment,可參考
http://blog.csdn.net/duguang77/article/details/17580993