Fragment 安卓中 比较重要的 框架 它的作用 就是 方便 代码的重复用 详情见 https://www.jianshu.com/p/465f645adac0
重要的 它能实现 菜单栏滑动功能 如下的功能 可以 滑动左右滑动 实现 页面的切换 而不是 activity间的跳转
实现的代码
@Override
protected void initView(Bundle savedInstanceState) {//初始化界面
setContentView(R.layout.activity_landform_tab);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
tabStrip = (PagerSlidingTabStrip)findViewById(R.id.tabs);
viewPager = (ViewPager)findViewById(R.id.pager);
fragmentArrayList = new ArrayList<>();
LandFormFragment landFormFragment = new LandFormFragment();
WaterSampeFragment waterSampeFragment = new WaterSampeFragment();
SoilSampleFragment soilSampleFragment = new SoilSampleFragment();
//添加fragment到集合中时注意顺序
fragmentArrayList.add(landFormFragment);
fragmentArrayList.add(waterSampeFragment);
fragmentArrayList.add(soilSampleFragment);
landFormFragment.setSession(mSession);
landFormFragment.setLandForm(landForm);
waterSampeFragment.setSession(mSession);
waterSampeFragment.setWaterSample(landForm.getWaterSample());
soilSampleFragment.setSession(mSession);
soilSampleFragment.setSoilSample(landForm.getSoilSample());
String[] titles = {"地质地貌调查","水样采样","土样采样"};
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyPagerAdapter(fragmentManager,titles,fragmentArrayList));
viewPager.setCurrentItem(0);
//当ViewPager的onPagerChangeListener回调时,PagerSlidingTabStrip也一起随之变动
//具体做法都已封装到了PagerSlidingTabStrip.setViewPager()方法里,使用时调用实例如下
tabStrip.setViewPager(viewPager);
tabStrip.setTextSize(20);
}
/**
* 自定义适配器
*/
class MyPagerAdapter extends FragmentPagerAdapter {
private String[] titles;
ArrayList<Fragment> fragments;
//根据需求定义构造方法,方便外部调用
public MyPagerAdapter(FragmentManager fm, String[] titles, ArrayList<Fragment> fragments) {
super(fm);
this.titles = titles;
this.fragments = fragments;
}
//设置每页的标题
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
//设置每一页对应的fragment
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
//fragment的数量
@Override
public int getCount() {
return fragments.size();
}
}
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
android:orientation="vertical"
android:background="@color/colorPrimary">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="36dip" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_below="@+id/tabs" />
</LinearLayout>
推荐几个常用的学习网站
1.菜鸟教程 2.csdn3.简书 4.githib