本章节讲述Table和ViewPager混合使用以达到滑动页面的效果。
代码就不详细介绍了,里面每一句都有注释。
1.在布局文件中添加相应的控件:TabLayout和ViewPager
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <!--布局页面写入两个控件TabLayout,ViewPager将两个控件相结合--> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout" /> </RelativeLayout>
2.主函数程序
public class MainActivity extends AppCompatActivity { /*界面的数量和各自界面名称设置*/ private static final int NUMBER_OF_FRAGMENTS = 4;/*四个部分*/ private static final int FRAGMENT_INDEXT_SETTING = 0;/*配置部分*/ private static final int FRAGMENT_INDEXT_LOGGER = 1;/*数据记录部分*/ private static final int FRAGMENT_INDEXT_OUTDOORMAP = 2;/*室外地图显示部分*/ private static final int FRAGMENT_INDEXT_INDOORMAP = 3;/*室内定位部分*/ /*定义一个包含权限的字符串数组,将需要的权限全部写到数组中*/ private static final String[] REQUIRED_PERMISSIONS = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE }; private static final int LOCATION_REQUEST_ID = 1;/*方法地一个参数,未解决*/ private Fragment[] mFragments;/*用于存放要使用的Fragment*/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); requestPermitionAndSetupFragment(this); } /* * 用于选择使用当前使用哪一个页面的适配器类 * */ public class ViewPagerAdapter extends FragmentStatePagerAdapter{ /*下面这三个方法是必须要覆写的*/ /*构造函数*/ public ViewPagerAdapter(FragmentManager fm) { super(fm); } /*获得页面的数量*/ @Override public int getCount() { return NUMBER_OF_FRAGMENTS; } /*通过这个方法得到当前页面的位置*/ @Override public Fragment getItem(int position) { switch(position){ case FRAGMENT_INDEXT_SETTING: return mFragments[FRAGMENT_INDEXT_SETTING];/*返回设置界面*/ case FRAGMENT_INDEXT_LOGGER: return mFragments[FRAGMENT_INDEXT_LOGGER];/*返回记录数据界面*/ case FRAGMENT_INDEXT_OUTDOORMAP: return mFragments[FRAGMENT_INDEXT_OUTDOORMAP];/*返回室外定位地图界面*/ case FRAGMENT_INDEXT_INDOORMAP: return mFragments[FRAGMENT_INDEXT_INDOORMAP];/*返回室内定位地图界面*/ default: throw new IllegalArgumentException("Invalid section: " + position);/*抛出错误*/ } } /*获得标题*/ @Override public CharSequence getPageTitle(int position) { switch(position){ case FRAGMENT_INDEXT_SETTING: return "配置";/*返回设置界面*/ case FRAGMENT_INDEXT_LOGGER: return "数据记录";/*返回记录数据界面*/ case FRAGMENT_INDEXT_OUTDOORMAP: return "室外定位";/*返回室外定位地图界面*/ case FRAGMENT_INDEXT_INDOORMAP: return "室内定位";/*返回室内定位地图界面*/ default: throw new IllegalArgumentException("Invalid section: " + position);/*抛出错误*/ } } } /* * 启动Fragment * */ private void setupFragment(){ mFragments = new Fragment[NUMBER_OF_FRAGMENTS];/*设置Fragment的数量*/ /*SettingFragment设置*/ SettingFragment settingFragment = new SettingFragment(); mFragments[FRAGMENT_INDEXT_SETTING] = settingFragment; /* * 以下设置使用到的Fragment * */ /*LoggerFragment设置*/ LoggerFragment loggerFragment = new LoggerFragment(); mFragments[FRAGMENT_INDEXT_LOGGER] = loggerFragment; /*OutdoorMapFragment设置*/ OutdoorMapFragment outdoorMapFragment = new OutdoorMapFragment(); mFragments[FRAGMENT_INDEXT_OUTDOORMAP] = outdoorMapFragment; /*IndoorMapFragment设置*/ IndoorMapFragment indoorMapFragment = new IndoorMapFragment(); mFragments[FRAGMENT_INDEXT_INDOORMAP] = indoorMapFragment; ViewPager viewPager = (ViewPager)findViewById(R.id.pager);/*创建ViewPager对象*/ viewPager.setOffscreenPageLimit(4);/*设置界面的页数为4*/ ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());/*通过构造函数构造页面适配器*/ viewPager.setAdapter(adapter);/*将ViewPager设置一个适配器PagerAdapter*/ TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);/*创建一个TabLayout类的对象*/ tabLayout.setupWithViewPager(viewPager);/*使用ViewPager启动TabLayout*/ /*一旦TabLayout的位置发生改变就会添加一个TabLayout上的页面选择监听器*/ tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager)); /*无论页面发生变化或则反卷都会添加一个页面变化监听器*/ viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); } /* * 查询是否具有相应的权限 * */ private Boolean hasPermission(Activity activity){ /*如果版本号小于Android6.0,不需要判断,直接返回真*/ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ return true; } /*如果有任何一项权限没有获得就返回false*/ for(String p : REQUIRED_PERMISSIONS){ if(ContextCompat.checkSelfPermission(activity,p) != PackageManager.PERMISSION_GRANTED){ return false; } } return true; } /* * 查询是否具有相应的权限以及启动Fragment * */ private void requestPermitionAndSetupFragment(final Activity activity){ /*如果获得所有的权限,就直接启动Fragment*/ if(hasPermission(activity)){ setupFragment(); }else{ ActivityCompat.requestPermissions(activity, REQUIRED_PERMISSIONS, LOCATION_REQUEST_ID); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
3.结果图
注意的问题:
这里需要注意的是Fragment和FragmentStatePagerAdapter要导入的包是
import android.app.Fragment; import android.app.FragmentManager; import android.support.v13.app.FragmentStatePagerAdapter;误将TabLayout写成TableLayout。