Android底部导航栏实现(四)之TabLayout+ViewPager

这里简单记录一下通过TabLayout+ViewPager来实现Android底部导航栏。

 

这里写图片描述

 

这里写图片描述

布局

<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 
<include layout="@layout/fragment_content"/> 
<LinearLayout android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.v4.view.ViewPager 
android:id="@+id/view_pager"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1">
</android.support.v4.view.ViewPager> 
<android.support.design.widget.TabLayout
 android:id="@+id/tab_layout" 
android:layout_width="match_parent" 
android:layout_height="56dp" 
app:tabBackground="@color/white" 
app:tabIndicatorHeight="0dp" app:tabSelectedTextColor="@color/colorPrimary" app:tabTextAppearance="@style/tabTextSizeStyle" app:tabTextColor="@color/black_1">
</android.support.design.widget.TabLayout>
 </LinearLayout></RelativeLayout>

代码

mViewPager = (ViewPager) view.findViewById(R.id.view_pager); mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout); 
initTabList(); 
mAdapter = new TabLayoutFragmentAdapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs); 
mViewPager.setAdapter(mAdapter); 
mViewPager.setCurrentItem(0); 
mTabLayout.setupWithViewPager(mViewPager); 
mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置TabLayout的模式 
for (int i = 0; i < mTabLayout.getTabCount(); i++) { 
mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i)); 
}
 mTabLayout.addOnTabSelectedListener(this);//设置TabLayout的选中监听

这里需要注意的就是TabLayout的使用。TabLayou配合ViewPager使用。要用 mTabLayout.setupWithViewPager(mViewPager);使二者联系起来。
另外这里面使用的是customView,当然TabLayout自带方法也可实现icon+text的效果。也就是效果:TabLayout + ViewPager 2

根据Tab选中状态显示Tab键效果

@Override
public void onTabSelected(TabLayout.Tab tab) {
 setTabSelectedState(tab); 
} 
@Override 
public void onTabUnselected(TabLayout.Tab tab) { 
setTabUnSelectedState(tab); 
} 
@Override
 public void onTabReselected(TabLayout.Tab tab) { 
} 

private void setTabSelectedState(TabLayout.Tab tab) { 
View customView = tab.getCustomView(); 
TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon); 
tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
 String s = tabText.getText().toString();
 if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home_fill);
 } else if (getString(R.string.item_location).equals(s)) {
 tabIcon.setImageResource(R.drawable.location_fill);
} else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like_fill);
 } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person_fill); 
} 
}
 private void setTabUnSelectedState(TabLayout.Tab tab) { 
View customView = tab.getCustomView(); 
TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); 
ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon);
 tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_1)); String s = tabText.getText().toString(); if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home); } else if (getString(R.string.item_location).equals(s)) { tabIcon.setImageResource(R.drawable.location); } else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like); } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person); } }

这里面不用设置defaultFragment,TabLayout会默认显示第一个;另外,这里也贴出使用TabLayout自带方法来实现效果代码
值得说的是,自带方法不能自定义icon和text的间距。用起来很方便,但是可能不是你要求的那个尺寸大小。我没有去深究这里面的源码。>如果有人知道这个自带方法怎么改变的,也请告知一下。

 mViewPager = (ViewPager) view.findViewById(R.id.view_pager); 
mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout); initTabList(); 
mAdapter = new TabLayoutFragment2Adapter(getChildFragmentManager(), 
mTabList, getActivity(), mFragments, mTabImgs); 
mViewPager.setAdapter(mAdapter); mViewPager.setCurrentItem(0); 
mTabLayout.setupWithViewPager(mViewPager); 
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
mViewPager.setCurrentItem(0); 
mTabLayout.getTabAt(0).setIcon(R.drawable.home_fill);//自有方法添加icon 
mTabLayout.getTabAt(1).setIcon(R.drawable.location); 
mTabLayout.getTabAt(2).setIcon(R.drawable.like); 
mTabLayout.getTabAt(3).setIcon(R.drawable.person);

Tab切换

 @Override 
public void onTabSelected(TabLayout.Tab tab) {
// setTabSelectedState(tab);//这个也无需使用 resetTabIcon();
 int position = tab.getPosition(); 
Log.e("Kevin", "position--->" + position); 
switch (position) { 
case 0: tab.setIcon(R.drawable.home_fill);
 break; 
case 1: tab.setIcon(R.drawable.location_fill);
 break; 
case 2: tab.setIcon(R.drawable.like_fill);
 break;
 case 3: tab.setIcon(R.drawable.person_fill);
 break; 
} }
private void resetTabIcon() { 
mTabLayout.getTabAt(0).setIcon(R.drawable.home); 
mTabLayout.getTabAt(1).setIcon(R.drawable.location); 
mTabLayout.getTabAt(2).setIcon(R.drawable.like); 
mTabLayout.getTabAt(3).setIcon(R.drawable.person); }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
++) { printf("\t%d", students[i].scores[j]); } printf("\t%d\t%.2f\n", students[i].totalScore, students[i].averageScore); for(int j = 0; j < subjectCount; j++) { printfAndroid底部导航栏可以使用TabLayout和ViewPager组合实现页面跳转功能。 首先,在布局文件中定义("请输入%s成绩:", subjects[j]); scanf("%d", &students[i].scores[j]); students[i].totalScore += students[i].scores[j]; } students[i].averageScore = (float)students[i].totalScore / subjectCount; TabLayout和ViewPager: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" printf("学生成绩已成功修改!\n"); break; } } if(!found) { printf android:layout_height="match_parent"> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android("学号不存在!\n"); } } // 删除学生成绩 void deleteStudent() { if(studentCount == 0:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior) { printf("没有学生信息!\n"); return; } char id[MAX_ID_LEN]; int found = 0; printf("请输入学号:"); scanf("%s", id); for(int i = 0; i < student"/> <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" Count; i++) { if(strcmp(students[i].id, id) == 0) { found = 1; for android:layout_height="wrap_content" android:background="@color/colorPrimary" app:tabIndicatorColor="@color/colorAccent(int j = i; j < studentCount - 1; j++) { students[j] = students[j + 1]; } " app:tabSelectedTextColor="@color/colorAccent" app:tabTextColor="@android:color/white"/> </androidx.coordinator studentCount--; printf("学生成绩已成功删除!\n"); break; } } if(!foundlayout.widget.CoordinatorLayout> ``` 然后,创建Fragment并创建FragmentPagerAdapter,将FragmentPagerAdapter设置到ViewPager中: ```java) { printf("学号不存在!\n"); } } // 统计学生成绩 void calculateStats() { if public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public MyPagerAdapter(@NonNull FragmentManager fm, List(studentCount == 0) { printf("没有学生信息!\n"); return; } float averageScores[MAX_SUBJECTS] = {0}; float totalAverageScore = 0; printf("科目名称\t平均分\n"); <Fragment> fragments) { super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); this.fragments = fragments; } @ for(int i = 0; i < subjectCount; i++) { for(int j = 0; j < studentCount; jNonNull @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int++) { averageScores[i] += students[j].scores[i]; } averageScores[i] /= studentCount; printf("% getCount() { return fragments.size(); } } public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private TabLayout tabLayouts\t\t%.2f\n", subjects[i], averageScores[i]); } for(int i = 0; i < studentCount; i++) { totalAverageScore += students[i].averageScore; } totalAverageScore /= studentCount; printf("; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager =总平均分:%.2f\n", totalAverageScore); } // 保存学生成绩到文件 void saveToFile() { if(studentCount == 0) { printf("没有学生信息!\n"); return; } FILE *fp findViewById(R.id.viewPager); tabLayout = findViewById(R.id.tabLayout); List<Fragment> fragments = new ArrayList<>(); fragments; char filename[MAX_NAME_LEN]; printf("请输入文件名:"); scanf("%s", filename); fp = fopen(filename.add(new FragmentA()); fragments.add(new FragmentB()); fragments.add(new FragmentC()); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),, "w"); fprintf(fp, "学号\t姓名"); for(int i = 0; i < subjectCount; i++) fragments)); tabLayout.setupWithViewPager(viewPager); } } ``` 最后,在Fragment中实现具体的页面功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值