【Android项目实战 | 从零开始写app (六) 】用TabLayout+ViewPager搭建App 框架主页面底部导航栏

文章导航

一、【Android项目实战 | 从零开始写app(一)】 创建项目

二、【Android项目实战 | 从零开始写app(二)】实现闪屏页,启动app

三、【Android项目实战 | 从零开始写app(三)】实现引导页,进入登录or主页面

四、【Android项目实战 | 从零开始写app(四)】Okhttp+Gson实现服务端登录验证功能

五、【Android项目实战 | 从零开始写app(五)】okhttp+gson实现服务端注册功能

六、【Android项目实战 | 从零开始写app (六)】用TabLayout+ViewPager搭建App 框架主页面底部导航栏

七、【Android项目实战 | 从零开始写app (七) 】优化主页导航栏,禁用主页页面滑动切换效果

八、【Android项目实战 | 从零开始写app (八) 】实现app首页广告轮播图切换和搜索跳转

九、【Android项目实战 | 从零开始写app (九) 】实现app首页热门推荐

十、【Android项目实战 | 从零开始写app (十) 】实现app首页九宫格服务分类点击跳转

十一、【Android项目实战 | 从零开始写app (11) 】实现新闻模块数据的解析

十二、【Android项目实战 | 从零开始写app (12) 】实现服务页面数据的解析

十三、【Android项目实战 | 从零开始写app (13) 】实现用户中心模块登录退出登录&信息修改&个人订单列表查看…

十四、【Android项目实战 | 从零开始写app (14) 】实现智慧建党模块…


本篇实现效果:

搭建app框架的方式有很多,本节主要用TabLayout+ViewPager搭建App框架,这种方式简单易实现,在主页中加载Fragment碎片,实现不同功能页面的切换效果图如下:
【Android项目实战 | 从零开始写app (六) 】用TabLayout+ViewPager搭建App 框架主页面底部导航栏_第1张图片

项目新增文件目录如下:

【Android项目实战 | 从零开始写app (六) 】用TabLayout+ViewPager搭建App 框架主页面底部导航栏_第2张图片

逻辑功能实现

添加依赖:

build.gradle 中添加依赖,TabLayout才能正常使用

implementation 'com.google.android.material:material:1.1.0'

MainActivity.class

在MainActivity.class 中添加如下代码:

package com.example.myapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

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

import com.example.myapp.fragment.HomeFragment;
import com.example.myapp.fragment.NewsFragment;
import com.example.myapp.fragment.PostFragment;
import com.example.myapp.fragment.ServiceFragment;
import com.example.myapp.fragment.UserFragment;
import com.google.android.material.tabs.TabLayout;

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

public class MainActivity extends AppCompatActivity {
     
    private ViewPager viewPager;
    private TabLayout tablayout;
    private List<Fragment> fragmentList;
    private String[] titles={
     "首页","服务","发布","新闻","用户中心"};
    private int[] unSele = {
     R.mipmap.main_home,R.mipmap.main_type,R.mipmap.main_cart,R.mipmap.main_community,R.mipmap.main_user};
    private int[] onSele = {
     R.mipmap.main_home_press,R.mipmap.main_type_press,R.mipmap.main_cart_press,R.mipmap.main_community_press,R.mipmap.main_user_press};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        initData();
    }

    public void initData(){
     
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ServiceFragment());
        fragmentList.add(new PostFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new UserFragment());

        MainTabAdapter mainTabAdapter = new MainTabAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mainTabAdapter);
        tablayout.setupWithViewPager(viewPager);

        for (int i=0;i<tablayout.getTabCount();i++){
     
            TabLayout.Tab tab = tablayout.getTabAt(i);
            tab.setCustomView(mainTabAdapter.getView(i));
        }

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
     
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
     
                View view = tab.getCustomView();
                ImageView img = view.findViewById(R.id.img);
                TextView tv = view.findViewById(R.id.tv);
                String title = tv.getText().toString();
                if (title=="首页"){
     
                    img.setImageResource(onSele[0]);
                } else if (title=="服务") {
     
                    img.setImageResource(onSele[1]);
                } else if (title=="发布") {
     
                    img.setImageResource(onSele[2]);
                } else if (title=="新闻") {
     
                    img.setImageResource(onSele[3]);
                } else if (title=="用户中心") {
     
                    img.setImageResource(onSele[4]);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
     
                View view = tab.getCustomView();
                ImageView img = view.findViewById(R.id.img);
                TextView tv = view.findViewById(R.id.tv);
                String title = tv.getText().toString();
                if (title=="首页"){
     
                    img.setImageResource(unSele[0]);
                } else if (title=="服务") {
     
                    img.setImageResource(unSele[1]);
                } else if (title=="发布") {
     
                    img.setImageResource(unSele[2]);
                } else if (title=="新闻") {
     
                    img.setImageResource(unSele[3]);
                } else if (title=="用户中心") {
     
                    img.setImageResource(unSele[4]);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
     

            }
        });

    }



    public class MainTabAdapter extends FragmentPagerAdapter{
     


        public MainTabAdapter(@NonNull FragmentManager fm) {
     
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
     
            if (position==0){
     
                return fragmentList.get(0);
            } else if (position==1){
     
                return fragmentList.get(1);
            }else if (position==2){
     
                return fragmentList.get(2);
            }else if (position==3){
     
                return fragmentList.get(3);
            }else if (position==4){
     
                return fragmentList.get(4);
            }
            return fragmentList.get(0);
        }

        @Override
        public int getCount() {
     
            return titles.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
     
            return titles[position];
        }

        public View getView(int position) {
     
            View view = View.inflate(MainActivity.this,R.layout.main_tab_item,null);
            ImageView img = view.findViewById(R.id.img);
            TextView tv = view.findViewById(R.id.tv);
            if (tablayout.getTabAt(position).isSelected()) {
     
                img.setImageResource(onSele[position]);
            } else {
     
                img.setImageResource(unSele[position]);
            }
            tv.setText(titles[position]);
            tv.setTextColor(tablayout.getTabTextColors());
            return view;
        }
    }


}

主页布局

activity_main.xml:


<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:orientation="vertical"
    android:layout_height="match_parent">

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/viewPager"/>

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:id="@+id/tablayout"
        app:tabSelectedTextColor="@color/bule"
        app:tabTextColor="#1C1C1C"
        android:background="#EFEFEF"
        app:tabIndicatorHeight="0dp"/>

LinearLayout>

TabLayout 子项布局

main_tab_item.xml:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingTop="15dp"
    android:layout_centerVertical="true"
    android:gravity="center_horizontal"
    android:layout_height="20dp">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/img"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="首页"
        android:gravity="center"
        android:id="@+id/tv"/>

LinearLayout>

Fragment 碎片

BaseFragment.class:

创建一个抽象BaseFragment基类继承 Fragment,里面写抽象方法,由子类继承重写:

package com.example.myapp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * @ProjectName: MyApp
 * @Package: com.example.myapp.fragment
 * @ClassName: BaseFragment
 * @Description:
 * @Author: liyingxia
 * @CreateDate: 2021/4/13 21:30
 */
public abstract class BaseFragment extends Fragment {
     
    private Context mContext;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     
        return initView();
    }

    public abstract View initView();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
     
        super.onActivityCreated(savedInstanceState);
        initData();
    }
    public void initData(){
     

    }

}

HomeFragment.class:

public class HomeFragment extends BaseFragment {
     

    private TextView tv;


    @Override
    public View initView() {
     
        View view = View.inflate(getActivity(), R.layout.fragment_home,null);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }

    @Override
    public void initData() {
     
        super.initData();
    }


}

ServiceFragment.class:

public class ServiceFragment extends BaseFragment {
     

    private TextView tv;


    @Override
    public View initView() {
     
        View view = View.inflate(getActivity(), R.layout.fragment_service,null);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }

    @Override
    public void initData() {
     
        super.initData();
    }


}

PostFragment.class:

public class PostFragment extends BaseFragment {
     

    private TextView tv;


    @Override
    public View initView() {
     
        View view = View.inflate(getActivity(), R.layout.fragment_post,null);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }

    @Override
    public void initData() {
     
        super.initData();
    }


}

NewsFragment.class:

public class NewsFragment extends BaseFragment {
     

    private TextView tv;

    @Override
    public View initView() {
     
        View view = View.inflate(getActivity(), R.layout.fragment_news,null);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }

    @Override
    public void initData() {
     
        super.initData();
    }

}

UserFragment.class:

public class UserFragment extends BaseFragment {
     

    private TextView tv;

    @Override
    public View initView() {
     
        View view = View.inflate(getActivity(), R.layout.fragment_user,null);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }

    @Override
    public void initData() {
     
        super.initData();
    }

}

fragment 碎片布局

fragment 碎片布局文件都一样,这里,只放一个,其他四个类似:

fragment_home.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="首页"
        android:textColor="@color/bule"
        android:textSize="60sp"
        android:textStyle="bold"
        android:gravity="center"
        android:id="@+id/tv"/>

LinearLayout>

color.xml:


<resources>
    <color name="colorPrimary">#6200EEcolor>
    <color name="colorPrimaryDark">#3700B3color>
    <color name="colorAccent">#03DAC5color>
    <color name="bule">#0987EDcolor>
    <color name="yellow">#FFC107color>
resources>

你可能感兴趣的:(Android,app,移动开发)