文章導航
一、【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碎片,實作不同功能頁面的切換效果圖如下:

專案新增檔案目錄如下:

邏輯功能實作
添加依賴:
在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:
<?xml version="1.0" encoding="utf-8"?>
<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:
<?xml version="1.0" encoding="utf-8"?>
<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:
<?xml version="1.0" encoding="utf-8"?>
<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:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="bule">#0987ED</color>
<color name="yellow">#FFC107</color>
</resources>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277512.html
標籤:其他
