1.下载PagerSlidingTabStrip这个库文件,用来滑动导航栏下载地址 https://github.com/astuetz/PagerSlidingTabStrip
2.导入自己创建的项目中,在导入的过程中可能会出错,一是因为support.v4.jar包冲突,将其中的一个删掉,并引入另一个的support.v4.jar;二是因为项目的版本问题,修改一下创建的项目版本就可以了,项目版本越高越好。
3.简单使用:(1)在main_activity.xml文件中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.kgcyy.app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="@+id/pagertab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:saveEnabled="true"
app:tabPaddingLeftRight="20dp"
android:background="#6699FF"
app:indicatorColor="@color/indicator_color" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/pagertab"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
(2)在activity中
public class MainActivity extends Activity{
private ViewPager mViewPager;
private PagerSlidingTabStrip mPagerSlidingTabStrip;
private View view1,view2,view3;
private List<View> viewList = new ArrayList<View>;//用来添加需要滑动的view
private List<String> titleList = new ArrayList<String>;//用来添加导航栏的标题
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout..main_activity);
LayoutInflater inflater = getLayoutInflater();
//layout 的定义不在此显示
view1 = inflater.inflate(R.layout.layout1,null);
view2 = inflater.inflate(R.layout.layout2,null);
view2 = inflater.inflate(R.layout.layout3,null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
titleList.add("A");
titleList.add("B");
titleList.add("C");
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mPagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.pagertab);
mAdapter = new MyAdapter();
mViewPager.setAdapter(mAdapter);
mPagerSlidingTabStrip.setViewPager(mViewPager);
mPagerTabStrip.setOnPageChangeListener(new PageChangeListener());
}
class PageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
//page滑动时的事件
}
}
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView(viewList.get(position));
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(viewList.get(position), 0);
return viewList.get(position);
}
}
}
4.此项目能够实现基本的滑动,但是界面不太美观,尤其是导航栏,需要自己动手改变库文件的内容。