【项目FJU】Fragment+ViewPager+TabLayout制作底部导航

实例

【项目FJU】Fragment+ViewPager+TabLayout制作底部导航_第1张图片
在这里插入图片描述

1 导入依赖

在项目的build.gradle里面添加

implementation rootProject.ext.dependencies["design"]

在gradle的统一管理配置文件里添加


【项目FJU】Fragment+ViewPager+TabLayout制作底部导航_第2张图片
在这里插入图片描述

2 Fragment

本例一共写5个Fragment,如下图所示:


【项目FJU】Fragment+ViewPager+TabLayout制作底部导航_第3张图片
在这里插入图片描述

如HomeFragment

package com.yds.mainmodule.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yds.mainmodule.R;

/**
 * Created by yds
 * on 2019/8/13.
 */
public class HomeFragment extends Fragment {

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

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_module_fragment_home,container,false);
        return view;
    }
}

3 MainActivity

本例自定义Tab

3.1 自定义Tab

  • 首先在项目的layout目录下创建一个自定义Tab的布局


    
    

  • 其次添加布局
private void setTabs(String[] bottomTabTitles, int[] bottomTabImages) {
        if (bottomTabImages != null && bottomTabTitles != null) {
            for (int i = 0; i < bottomTabTitles.length; i++) {
                TabLayout.Tab tab = mBottomTabLayout.newTab();
                View view = getLayoutInflater().inflate(R.layout.main_module_layout_home_tab_item, null);
                tab.setCustomView(view);

                TextView tabTitle = view.findViewById(R.id.home_item_tv);
                tabTitle.setText(bottomTabTitles[i]);


                ImageView tabImage = view.findViewById(R.id.home_item_iv);
                tabImage.setImageResource(bottomTabImages[i]);
                if (i == 0) {
                    mBottomTabLayout.addTab(tab, true);
                } else {
                    mBottomTabLayout.addTab(tab, false);
                }
            }
        }

    }

3.2 定义FragmentPagerAdapter

package com.yds.mainmodule.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.yds.mainmodule.fragment.AttentionFragment;
import com.yds.mainmodule.fragment.HomeFragment;
import com.yds.mainmodule.fragment.JevelFragment;
import com.yds.mainmodule.fragment.MeFragment;
import com.yds.mainmodule.fragment.NotificationFragment;

import java.util.List;

/**
 * Created by yds
 * on 2019/9/18.
 */
public class HomeTabFragmentPagerAdapter extends FragmentPagerAdapter {
    private List mFragmentList;
    public HomeTabFragmentPagerAdapter(FragmentManager fm,List mFragmentList) {
        super(fm);
        this.mFragmentList = mFragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


}

3.3 加载Adapter并将TabLayout与ViewPager绑定

        setTabs(mTabTitles, mTabImages);
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new AttentionFragment());
        mFragmentList.add(new JevelFragment());
        mFragmentList.add(new NotificationFragment());
        mFragmentList.add(new MeFragment());

        mAdapter = new HomeTabFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);
        mBottomViewPager.setAdapter(mAdapter);
        mBottomViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBottomTabLayout));
        mBottomTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mBottomViewPager));

4 布局文件

4.1 main布局文件




    

    

    

    

    

4.2 tab选择图片的布局



    
    

5 源码地址

源码:https://github.com/Yedongsheng/Jianshu/tree/develop

你可能感兴趣的:(【项目FJU】Fragment+ViewPager+TabLayout制作底部导航)