【Android】用Fragment、Viewpage实现tab栏切换

一、完成情况

1. 图片

请添加图片描述

2. 文字介绍

  1. 点击tab栏底部可以切换不同的页面:
    在切换时,被选中的颜色会变化
  2. 左右滑动屏幕也会切换不同的页面。

3. 代码目录结构

在这里插入图片描述

二、代码

1. xml

  1. tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  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"
    tools:context=".main">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/myViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="0dp">
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/content">
            </FrameLayout>
        </androidx.viewpager.widget.ViewPager>

<!--        tab栏-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            >

            <RelativeLayout
                android:id="@+id/rl_tab_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <!--            android:gravity="center"-->

                <TextView
                    android:id="@+id/home_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/home_pur"
                    android:layout_centerHorizontal="true"/>

                <TextView
                    android:id="@+id/home_text"
                    android:layout_below="@id/home_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页"
                    android:layout_centerHorizontal="true"/>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_tab_display"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                >

                <TextView
                    android:id="@+id/display_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/db1_grey"
                    android:layout_centerHorizontal="true"
                    />

                <TextView
                    android:id="@+id/display_text"
                    android:layout_below="@id/display_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="数据"/>

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl_tab_db"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                >

                <TextView
                    android:id="@+id/db_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/db_grey"
                    android:layout_centerHorizontal="true"/>

                <TextView
                    android:id="@+id/db_text"
                    android:layout_below="@id/db_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="折线图"
                    android:layout_centerHorizontal="true"/>

            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>
  1. fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是1"
        android:id="@+id/textView3"
        android:textSize="18dp" />
</LinearLayout>
  1. fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是2"
        android:id="@+id/textView3"
        android:textSize="18dp" />
</LinearLayout>
  1. fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是3"
        android:id="@+id/textView3"
        android:textSize="18dp" />
</LinearLayout>

2. java

  1. fragment1.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class fragment1 extends Fragment{

    public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
        View view=inflater .inflate(R.layout.fragment1 ,container,false) ;

        return view;
    }
}

  1. fragment2.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class fragment1 extends Fragment{

    public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
        View view=inflater .inflate(R.layout.fragment2 ,container,false) ;

        return view;
    }
}

  1. fragment3.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class fragment1 extends Fragment{

    public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
        View view=inflater .inflate(R.layout.fragment3 ,container,false) ;

        return view;
    }
}

  1. TabFragmentPagerAdapter.java
package com.example.tab;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
    private FragmentManager mfragmentManager;
    private List<Fragment> mlist;


    public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.mlist = list;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
    }

    @Override
    public Fragment getItem(int arg0) {
        return mlist.get(arg0);//显示第几个页面
    }

    @Override
    public int getCount() {
        return mlist.size();//有几个页面
    }
}
  1. main.java
package com.example.tab;


import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class main extends AppCompatActivity implements View.OnClickListener{
    private List<Fragment> list;
    private ViewPager myViewPager;
    private com.example.tab.TabFragmentPagerAdapter adapter;

    //定义一些成员变量
    TextView mHome;
    TextView mDisplay;
    TextView mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);
        initView();
    }
    private void initView()
    {
        mHome = findViewById(R.id.home_img);
        mDisplay = findViewById(R.id.display_img);
        mDb = findViewById(R.id.db_img);

        RelativeLayout rlHome = findViewById(R.id.rl_tab_home);
        RelativeLayout rlDisplay = findViewById(R.id.rl_tab_display);
        RelativeLayout rlDb = findViewById(R.id.rl_tab_db);

        rlHome.setOnClickListener( this);
        rlDisplay.setOnClickListener( this);
        rlDb.setOnClickListener( this);


        myViewPager = (ViewPager) findViewById(R.id.myViewPager);
        //绑定点击事件
        myViewPager.setOnPageChangeListener(new MyPagerChangeListener()) ;
        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        list.add(new com.example.tab.fragment1() );
        list.add(new com.example.tab.fragment2() );
        list.add(new fragment3() );
        adapter = new com.example.tab.TabFragmentPagerAdapter(getSupportFragmentManager(), list);
        myViewPager.setAdapter(adapter);
        myViewPager.setCurrentItem(0);  //初始化显示第一个页面

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            //当点击home
            case R.id.rl_tab_home:
                System.out.println("home被点击了!");
                mHome.setBackgroundResource(R.drawable.home_pur);
                mDisplay.setBackgroundResource(R.drawable.db1_grey);
                mDb.setBackgroundResource(R.drawable.db_grey);
                myViewPager.setCurrentItem(0);
                break;
            case R.id.rl_tab_display:
                System.out.println("数据被点击了!");
                mHome.setBackgroundResource(R.drawable.home_grey);
                mDisplay.setBackgroundResource(R.drawable.db1_pur);
                mDb.setBackgroundResource(R.drawable.db_grey);
                myViewPager.setCurrentItem(1);
                break;
            case R.id.rl_tab_db:
                System.out.println("折线图被点击了!");
                mHome.setBackgroundResource(R.drawable.home_grey);
                mDisplay.setBackgroundResource(R.drawable.db1_grey);
                mDb.setBackgroundResource(R.drawable.db_pur);
                myViewPager.setCurrentItem(2);
                break;

        }
    }

    /**
     * 设置一个ViewPager的侦听事件,当左右滑动ViewPager时菜单栏被选中状态跟着改变
     *
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {//状态改变时底部对应的字体颜色改变
                case 0:
                    mHome.setBackgroundResource(R.drawable.home_pur);
                    mDisplay.setBackgroundResource(R.drawable.db1_grey);
                    mDb.setBackgroundResource(R.drawable.db_grey);
                    break;
                case 1:
                    mHome.setBackgroundResource(R.drawable.home_grey);
                    mDisplay.setBackgroundResource(R.drawable.db1_pur);
                    mDb.setBackgroundResource(R.drawable.db_grey);
                    break;
                case 2:
                    mHome.setBackgroundResource(R.drawable.home_grey);
                    mDisplay.setBackgroundResource(R.drawable.db1_grey);
                    mDb.setBackgroundResource(R.drawable.db_pur);
                    break;
            }

        }

    }
}

3. manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tab">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Tab">

        <activity android:name=".MainActivity"
            android:exported="true">

        </activity>

        <activity android:name=".main"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

三、结尾彩蛋~

源码是0积分下载喔~~~
链接点这里

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值