-
Notifications
You must be signed in to change notification settings - Fork 5
【项目FJU】Fragment ViewPager TabLayout制作底部导航
ITindoorsman edited this page Mar 1, 2020
·
1 revision
在项目的build.gradle里面添加
implementation rootProject.ext.dependencies["design"]
在gradle的统一管理配置文件里添加
本例一共写5个Fragment,如下图所示: 如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;
}
}
本例自定义Tab
- 首先在项目的layout目录下创建一个自定义Tab的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:padding="5dp">
<ImageView
android:id="@+id/home_item_iv"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="20dp"
android:layout_height="20dp"
tools:src="@drawable/icon_tabbar_subscription_active"/>
<TextView
android:id="@+id/home_item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/home_item_iv"
android:textColor="@drawable/tabbar_subscription_text_selecter"
android:textSize="10sp"
tools:text="首页"/>
</android.support.constraint.ConstraintLayout>
- 其次添加布局
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);
}
}
}
}
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<Fragment> mFragmentList;
public HomeTabFragmentPagerAdapter(FragmentManager fm,List<Fragment> mFragmentList) {
super(fm);
this.mFragmentList = mFragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
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));
<?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:orientation="vertical"
tools:context="com.yds.mainmodule.mobile.views.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/bottom_tab_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v4.view.ViewPager>
<View
android:id="@+id/line_view"
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:background="@color/f_line_gray"
app:layout_constraintBottom_toTopOf="@+id/main_bottom_tabLayout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottom_tab_viewpager"
app:layout_constraintVertical_bias="1.0" />
<android.support.design.widget.TabLayout
android:id="@+id/main_bottom_tabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/main_module_bottom_tab_height"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line_view"
app:layout_constraintVertical_bias="1.0"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/f_font_tabbar_text_selected"
app:tabTextColor="@color/f_font_tabbar_text_default">
</android.support.design.widget.TabLayout>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/icon_tabbar_subscription_active"/>
<item android:state_selected="false" android:drawable="@drawable/icon_tabbar_subscription"/>
</selector>
源码:https://github.com/Yedongsheng/Jianshu/tree/develop