您的位置:首页 > 移动开发 > Android开发

android---------TabLayout 的基础应用=====横向滑动菜单

2018-02-25 18:28 603 查看
gradle中添加依赖*compile'com.android.support:design:26+'/*** 1--写tablayout* 2--- 写viewPager* 3-- 写viewPagerAdapter* 4-- 让TabLayout和Viewpager关联;*/
布局文件代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.bwie.tablayoutdemo2.MainActivity"><android.support.design.widget.TabLayoutandroid:id="@+id/tabLayout"android:layout_width="match_parent"android:layout_height="wrap_content" /><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout>
--------------------------
MainActivity代码,  注意FragmentPagerAdapt中要重写getPageTitle方法;
package com.bwie.tablayoutdemo2;import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import java.util.ArrayList;/*** 1--tablayout* 2--- viewPager* 3--viewPagerAdapter* 4-- 让TabLayout和Viewpager关联;*/public class MainActivity extends AppCompatActivity {private TabLayout tabLayout;private ViewPager viewPager;ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();ArrayList<String> titleList = new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabLayout = (TabLayout) findViewById(R.id.tabLayout);viewPager = (ViewPager) findViewById(R.id.viewPager);initData();//ctrl+p 提示参数的快捷键MPagerAdapter mPagerAdapter = new MPagerAdapter(getSupportFragmentManager());viewPager.setAdapter(mPagerAdapter);//设置TabLayout的模式tabLayout.setTabMode(TabLayout.MODE_FIXED);//让tablayout和Viewpager关联;tabLayout.setupWithViewPager(viewPager);}private void initData() {for (int i = 0; i <4 ; i++) {fragmentList.add(new BlankFragment());titleList.add("爱因斯坦" +i);}}class  MPagerAdapter extends FragmentPagerAdapter{public MPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {return fragmentList.get(position);}@Overridepublic int getCount() {return fragmentList.size();}//需要重写个返回标题的方法;@Overridepublic CharSequence getPageTitle(int position) {return titleList.get(position);}}}
------------------------------------------
Fragment代码,Fragment用的都是V4包里的;
public class BlankFragment extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {TextView textView = new TextView(getActivity());double random = Math.random();textView.setText("xxxxxxxxxxx"+random);return textView;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: