美文网首页
TabLayout+ViewPager+Fragment实现滑动

TabLayout+ViewPager+Fragment实现滑动

作者: 雨灬纷灬飞 | 来源:发表于2018-11-01 15:55 被阅读0次

最近闲来无事,想想滑动菜单好久没做了,写写熟悉一下


简书2.jpg

开启一个新的app,创建MainActivity和布局activity_main
在写布局文件的时候用到了ConstraintLayout控件(ConstraintLayout 为 约束布局,也有人把它称作 增强型的相对布局,由 2016 年 Google I/O 推出),ConstraintLayout有优化渲染布局的性能,如果在里面嵌套布局时
报错,请参考


1541059391.jpg

TabLayout布局可以通过app:tabIndicatorColor="@color/color_blue"设置选中下划线颜色,设置成透明就是不显示,也可以通过app:tabIndicatorHeight="0dp"将高度设置为0dp
具体请看代码
TabLayout布局属性https://www.jianshu.com/p/23863e4bbea1

        <?xml version="1.0" encoding="utf-8"?>
         <android.support.constraint.ConstraintLayout 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:background="@mipmap/bg"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:id="@+id/signin_title_ll"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@mipmap/title_bg"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/logo_bg"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:id="@+id/signin_title_tv"
            android:layout_width="wrap_content"
            android:layout_height="49dp"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="签到系统"
            android:textColor="@color/color_black"
            android:textSize="16sp" />

    </RelativeLayout>


    <android.support.design.widget.TabLayout
        android:id="@+id/signin_tabs"
        android:layout_width="0dp"
        android:layout_height="40dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signin_title_ll"
        app:tabIndicatorColor="@color/color_blue" 下划线颜色,设置成透明就是不显示
        app:tabIndicatorHeight="0dp"  下划线不显示
        app:tabBackground="@drawable/tab_background" 背景改变
        app:tabSelectedTextColor="@color/color_white"     文字被选中颜色
        app:tabTextColor="@color/color_1A"                       文字不被选中颜色
        android:padding="10dp"     背景图片内置距离
        app:tabMode="scrollable"/>
  
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="0dp"
        android:layout_height="502dp"
        android:layout_marginEnd="14dp"
        android:layout_marginStart="14dp"
        android:background="@mipmap/contain"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signin_tabs"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp" />

</android.support.constraint.ConstraintLayout>

在drawable中添加tab_background.xml文件

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@mipmap/title_sel" android:state_selected="true" />
        <item android:drawable="@drawable/sel" android:state_focused="false"  />
    </selector>```
在drawable中添加sel.xml文件,这个就是为了设置透明属性
   ```   <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@color/color_tralate" />
        </shape>```

布局就简单的完成了(注意添加依赖
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
)
接下来就是Activity中的实现

    public class MainActivity extends BaseActivity {
    
        private TabLayout mSigninTabs;
        private TextView mSigninTitle;
        private ViewPager mViewPager;
        private Context context = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);//为了让图片覆盖title而进行的自定义
            context = this;
            initData();
            initView();
            initViewPager();
        }
    
        private void initViewPager() {
            List<String> titles = new ArrayList<>();
            titles.add("第1节课");
            titles.add("第2节课");
            titles.add("第3节课");
            titles.add("第4节课");
            titles.add("第5节课");
            titles.add("第6节课");
            titles.add("第7节课");
            titles.add("第8节课");
            titles.add("第9节课");
            titles.add("第10节课");
    
            for (int i = 0; i < titles.size(); i++) {
                //滑动菜单文字的写入
                mSigninTabs.addTab(mSigninTabs.newTab().setText(titles.get(i)));
            }
            //加入相应的Fragment
            List<Fragment> fragments = new ArrayList<>();
            for (int i = 0; i < titles.size(); i++) {
                if(i==0){
                    fragments.add(new ListsFragment());
                }else {
                    fragments.add(new ListsFragment2());
                }
    
            }
    
            FragmentAdapter mFragmentAdapteradapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
            //给ViewPager设置适配器
             mViewPager.setAdapter(mFragmentAdapteradapter);
            // 将TabLayout和ViewPager关联起来。
            mSigninTabs.setupWithViewPager(mViewPager);
            // 给TabLayout设置适配器
            mSigninTabs.setTabsFromPagerAdapter(mFragmentAdapteradapter);
    
        }
    
        private void initView() {
            mSigninTitle = (TextView) findViewById(R.id.signin_title_tv);
            mSigninTabs = (TabLayout) findViewById(R.id.signin_tabs);
            mViewPager = (ViewPager) findViewById(R.id.viewpager);
        }
    
        private void initData() {
        }
    }

ListsFragment与ListsFragment2中的代码是一样的只是显示个布局
    public class ListsFragment extends Fragment {
        private View mView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mView = (View) inflater.inflate(R.layout.list_fragment, container, false);
            return mView;
        }
        
    }
布局文件list_fragment
  <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:fitsSystemWindows="true">
    
        <TextView
            android:id="@+id/lesson_detail_tv"
            android:layout_width="150dp"
            android:layout_height="26dp"
            android:layout_marginTop="@dimen/px_70"
            android:background="@mipmap/lesson_bg"
            android:gravity="center"
            android:text="课程详细信息"
            android:textColor="@color/color_1A"
            android:textSize="@dimen/px_36"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1" />
    
        <RelativeLayout
            android:id="@+id/lesson_detail_re1"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginTop="@dimen/px_90"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_tv"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:text="课程:  综合汉语一 \nCourse"
                android:textColor="@color/color_1A"
                android:layout_marginLeft="@dimen/px_140"
                android:textSize="@dimen/px_40" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/px_140"
                android:layout_alignParentRight="true"
    
                android:text="应到人数:  40\nno.ofpeople"
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/lesson_detail_re2"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginTop="@dimen/px_50"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_re1"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:text="教室:  TRB201 \nClassroom"
                android:textColor="@color/color_1A"
                android:layout_marginLeft="@dimen/px_140"
                android:textSize="@dimen/px_40" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/px_140"
                android:layout_alignParentRight="true"
    
                android:text="实到人数:  36\nActual no."
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/lesson_detail_re3"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginTop="@dimen/px_50"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_re2"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:text="班级:   08春A1-01 \nClass"
                android:textColor="@color/color_1A"
                android:layout_marginLeft="@dimen/px_140"
                android:textSize="@dimen/px_40" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/px_140"
                android:layout_alignParentRight="true"
    
                android:text="迟到人数:  2  \nlate no."
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/lesson_detail_re4"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginTop="@dimen/px_50"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_re3"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    
                android:text="教师:  张春光 \nTeacher"
                android:textColor="@color/color_1A"
                android:layout_marginLeft="@dimen/px_140"
                android:textSize="@dimen/px_40" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/px_140"
                android:layout_alignParentRight="true"
    
                android:text="未到人数:  4  \nAbsence no."
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
        </RelativeLayout>
        <TextView
            android:id="@+id/lesson_detail_tv2"
            android:layout_width="150dp"
            android:layout_height="26dp"
            android:layout_marginTop="@dimen/px_70"
            android:background="@mipmap/lesson_bg"
            android:gravity="center"
            android:text="学生刷卡信息"
            android:textColor="@color/color_1A"
            android:textSize="@dimen/px_36"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_re4"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1" />
        <RelativeLayout
            android:id="@+id/students_detail_re5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/px_132"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lesson_detail_tv2"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">
    
            <ImageView
                android:layout_width="81.33dp"
                android:layout_height="111.33dp"
                android:background="@mipmap/students_bg"
                android:src="@mipmap/students"
                android:padding="5dp"
                android:textColor="@color/color_1A"
                android:layout_marginLeft="@dimen/px_140"
                android:textSize="@dimen/px_40" />
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/px_534"
                android:layout_alignParentLeft="true"
    
                android:text="姓名:  小赵\nName"
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/px_534"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/name"
                android:layout_marginTop="@dimen/px_30"
                android:text="签到时间:  08:29:36\nCheck in time"
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/px_534"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/time"
                android:layout_marginTop="@dimen/px_30"
                android:text="刷卡状态:  正常\nSwipe status"
                android:textColor="@color/color_1A"
                android:textSize="@dimen/px_40" />
        </RelativeLayout>
    </android.support.constraint.ConstraintLayout>

这个只是简单的布局实现,还有很多地方需要优化

这个简单的滑动菜单就实现了,根据需要可以添加不停的Fragment内容

相关文章

网友评论

      本文标题:TabLayout+ViewPager+Fragment实现滑动

      本文链接:https://www.haomeiwen.com/subject/iaexxqtx.html