日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android實現類似iOS分欄控制器_Android

作者:陳世流年 ? 更新時間: 2022-05-23 編程語言

近公司接了一個項目,需要會安卓,人手不夠的情況作為一個開發iOS的也需要跟進,開始學習android,集成開發環境以后。直接就被難到了,iOS里面的分欄控制器(tabbarcontroller)android里面根本沒有這個控件,安卓都是自己來實現這個效果的。所以開始研究android是如何實現的,下面這些代碼。

當我們創建一個android APP項目的時候會自動生成一個MainActivity,我們可以在這Activity實現這個效果。首先我們先看一下效果圖

代碼實現



?
? ? 
?
? ? 
?
? ? 
?
? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? 
?
? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? 
?
? ? ? ? 
?
? ? ? ? ? ? 
?
?
? ? ? ? ? ? 
?
? ? ? ? 
?
?
? ? ? ? 
?
? ? ? ? ? ? 
?
?
? ? ? ? ? ? 
?
? ? ? ? 
?
? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? ? ? 
?
? ? ? ? 
?
? ? 
?

這樣頁面我們就實現了。

接下來就是點擊切換頁面的效果實現了,代碼主要就是實現點擊切換頁面的功能。所以我們要關聯一下五個Fragment了

package com.zkteco.pridebiosecurity.view;
?
import android.annotation.SuppressLint;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
?
import com.zkteco.pridebiosecurity.R;
import com.zkteco.pridebiosecurity.base.BaseActivity;
import com.zkteco.pridebiosecurity.base.BaseFragment;
import com.zkteco.pridebiosecurity.util.StatusBarUtil;
import com.zkteco.pridebiosecurity.view.clock.ClockFragment;
import com.zkteco.pridebiosecurity.view.entrance.EntranceFragment;
import com.zkteco.pridebiosecurity.view.home.HomeFragment;
import com.zkteco.pridebiosecurity.view.me.MeFragment;
import com.zkteco.pridebiosecurity.view.message.MessageFragment;
import com.zkteco.pridebiosecurity.widget.TitleBar;
?
/**
?* 程序主界面
?*
?* Created by sunyd on 2019/2/22.
?*/
public class MainActivity extends BaseActivity implements OnClickListener {
?
? ? private LinearLayout mMessageLl, mColckLl, mHomeLl, mEntranceLl, mMeLl;
? ? private ImageView mMessageIv, mColckIv, mHomeIv, mEntranceIv, mMeIv;
? ? private TextView mMessageTv, mColckTv, mHomeTv, mEntranceTv, mMeTv;
? ? private BaseFragment baseFragment;
? ??
? ? @Override
? ? protected int bindLayout() {
? ? ? ? return R.layout.activity_main;
? ? }
?
? ? @Override
? ? protected void initView() {
? ? ? ? // 設置狀態欄背景藍色,文字白色
? ? ? ? StatusBarUtil.setStatusBarLightMode(this, false);
?
? ? ? ? mMessageLl = bindView(R.id.message_ll);
? ? ? ? mColckLl = bindView(R.id.clock_ll);
? ? ? ? mHomeLl = bindView(R.id.home_ll);
? ? ? ? mEntranceLl = bindView(R.id.entrance_ll);
? ? ? ? mMeLl = bindView(R.id.me_ll);
? ? ? ? mMessageIv = bindView(R.id.message_iv);
? ? ? ? mColckIv = bindView(R.id.clock_iv);
? ? ? ? mHomeIv = bindView(R.id.home_iv);
? ? ? ? mEntranceIv = bindView(R.id.entrance_iv);
? ? ? ? mMeIv = bindView(R.id.me_iv);
? ? ? ? mMessageTv = bindView(R.id.message_tv);
? ? ? ? mColckTv = bindView(R.id.clock_tv);
? ? ? ? mHomeTv = bindView(R.id.home_tv);
? ? ? ? mEntranceTv = bindView(R.id.entrance_tv);
? ? ? ? mMeTv = bindView(R.id.me_tv);
?
? ? ? ? changeFragment(MessageFragment.class, 0);
? ? }
?
? ? @Override
? ? protected void initData() {
?
? ? }
?
? ? @Override
? ? protected void setListeners() {
? ? ? ? mMessageLl.setOnClickListener(this);
? ? ? ? mColckLl.setOnClickListener(this);
? ? ? ? mHomeLl.setOnClickListener(this);
? ? ? ? mEntranceLl.setOnClickListener(this);
? ? ? ? mMeLl.setOnClickListener(this);
? ? }
?
? ? @Override
? ? protected void autoRefresh() {
?
? ? }
?
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.message_ll:
? ? ? ? ? ? ? ? changeFragment(MessageFragment.class, 0);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.clock_ll:
? ? ? ? ? ? ? ? changeFragment(ClockFragment.class, 1);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.home_ll:
? ? ? ? ? ? ? ? changeFragment(HomeFragment.class, 2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.entrance_ll:
? ? ? ? ? ? ? ? changeFragment(EntranceFragment.class, 3);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.me_ll:
? ? ? ? ? ? ? ? changeFragment(MeFragment.class, 4);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
?
? ? /**
? ? ?* 切換主界面
? ? ?* @param clazz
? ? ?* @param position
? ? ?*/
? ? public void changeFragment(Class clazz, int position) {
? ? ? ? FragmentManager fm = getSupportFragmentManager();
? ? ? ? FragmentTransaction ft = fm.beginTransaction();
? ? ? ? Fragment fragment = fm.findFragmentByTag(clazz.getName());
?
? ? ? ? if (fragment == null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? fragment = clazz.newInstance();
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? if (baseFragment != null && baseFragment != fragment) {
? ? ? ? ? ? ft.hide(baseFragment);
? ? ? ? }
?
? ? ? ? if (!fragment.isAdded()) {
? ? ? ? ? ? ft.add(R.id.main_fl, fragment, clazz.getName());
? ? ? ? } else {
? ? ? ? ? ? ft.show(fragment);
? ? ? ? }
?
? ? ? ? ft.commitAllowingStateLoss();
? ? ? ? baseFragment = (BaseFragment) fragment;
?
? ? ? ? mMessageIv.setImageResource(R.mipmap.ic_message_0);
? ? ? ? mColckIv.setImageResource(R.mipmap.ic_clock_0);
? ? ? ? mHomeIv.setImageResource(R.mipmap.ic_home_0);
? ? ? ? mEntranceIv.setImageResource(R.mipmap.ic_entrance_0);
? ? ? ? mMeIv.setImageResource(R.mipmap.ic_me_0);
? ? ? ? mMessageTv.setTextColor(getResources().getColor(R.color.gray));
? ? ? ? mColckTv.setTextColor(getResources().getColor(R.color.gray));
? ? ? ? mHomeTv.setTextColor(getResources().getColor(R.color.gray));
? ? ? ? mEntranceTv.setTextColor(getResources().getColor(R.color.gray));
? ? ? ? mMeTv.setTextColor(getResources().getColor(R.color.gray));
?
? ? ? ? if (position == 0) {
? ? ? ? ? ? mMessageIv.setImageResource(R.mipmap.ic_message_1);
? ? ? ? ? ? mMessageTv.setTextColor(getResources().getColor(R.color.app_style_color));
? ? ? ? } else if (position == 1) {
? ? ? ? ? ? mColckIv.setImageResource(R.mipmap.ic_clock_1);
? ? ? ? ? ? mColckTv.setTextColor(getResources().getColor(R.color.app_style_color));
? ? ? ? } else if (position == 2) {
? ? ? ? ? ? mHomeIv.setImageResource(R.mipmap.ic_home_1);
? ? ? ? ? ? mHomeTv.setTextColor(getResources().getColor(R.color.app_style_color));
? ? ? ? } else if (position == 3) {
? ? ? ? ? ? mEntranceIv.setImageResource(R.mipmap.ic_entrance_1);
? ? ? ? ? ? mEntranceTv.setTextColor(getResources().getColor(R.color.app_style_color));
? ? ? ? } else if (position == 4) {
? ? ? ? ? ? mMeIv.setImageResource(R.mipmap.ic_me_1);
? ? ? ? ? ? mMeTv.setTextColor(getResources().getColor(R.color.app_style_color));
? ? ? ? }
? ? }
}

這里呢,每一個Fragment都繼承了BaseFragment,下面是BaseFragment的代碼實現

/**
?* TODO
?* By sunyd, 2016-7-19
?*/
package com.zkteco.pridebiosecurity.base;
?
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
?
import com.zkteco.pridebiosecurity.widget.LoadingDialog;
?
/**
?* fragment基類
?*
?* @author sunyd, 2016-7-19
?*/
public abstract class BaseFragment extends android.support.v4.app.Fragment {
?
? ? protected Context mContext;
? ? private LoadingDialog mABLoadingDialog;
? ? protected View mRootView;
?
? ? @Override
? ? public void onCreate(@Nullable Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? }
?
? ? @Override
? ? @Nullable
? ? public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
? ? ? ? return inflater.inflate(bindLayout(), container, false);
? ? }
?
? ? @Override
? ? public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
? ? ? ? super.onViewCreated(view, savedInstanceState);
? ? ? ? mABLoadingDialog = new LoadingDialog(getActivity());
? ? ? ? mContext = getActivity();
? ? ? ? mRootView = view;
? ? ? ? initView();
? ? }
?
? ? protected  T bindView(int id) {
? ? ? ? @SuppressWarnings("unchecked") T t = (T) mRootView.findViewById(id);
? ? ? ? return t;
? ? }
?
? ? @Override
? ? public void onActivityCreated(@Nullable Bundle savedInstanceState) {
? ? ? ? super.onActivityCreated(savedInstanceState);
? ? ? ? initData();
? ? ? ? setListeners();
? ? }
?
? ? @Override
? ? public void onResume() {
? ? ? ? super.onResume();
? ? ? ? autoRefresh();
? ? }
?
? ? /**
? ? ?* 綁定布局
? ? ?* @return
? ? ?*/
? ? protected abstract int bindLayout();
?
? ? /**
? ? ?* 初始化組件
? ? ?*/
? ? protected abstract void initView();
?
? ? /**
? ? ?* 數據
? ? ?*/
? ? protected abstract void initData();
?
? ? /**
? ? ?* 設置監聽
? ? ?*/
? ? protected abstract void setListeners();
?
? ? /**
? ? ?* 自動刷新
? ? ?*/
? ? protected abstract void autoRefresh();
?
? ? /**
? ? ?* 加載動畫
? ? ?* @param show
? ? ?*/
? ? public void showOrHideWaitBar(boolean show) {
? ? ? ? if (mABLoadingDialog != null) {
? ? ? ? ? ? if (show) {
? ? ? ? ? ? ? ? mABLoadingDialog.show();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? mABLoadingDialog.dismiss();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? @Override
? ? public void onDestroy() {
? ? ? ? if (mABLoadingDialog != null && mABLoadingDialog.isDialogShowing()) {
? ? ? ? ? ? mABLoadingDialog.dismiss();
? ? ? ? }
? ? ? ? super.onDestroy();
? ? }
}

MainActivity繼承BaseActivity,下面便是BaseActivity實現代碼

?package com.zkteco.pridebiosecurity.base;
?
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
?
import com.zkteco.pridebiosecurity.R;
import com.zkteco.pridebiosecurity.util.StatusBarUtil;
import com.zkteco.pridebiosecurity.widget.LoadingDialog;
?
?
/**
?* activity基類
?*
?* @author sunyd, 2019-2-25
?*/
@SuppressLint("NewApi")
public abstract class BaseActivity extends AppCompatActivity {
?
? ? private LoadingDialog mABLoadingDialog;
? ? protected Context mContext;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? BaseApplication.getInstance().addActivity(this);
? ? ? ? // 設置狀態欄背景白色,文字黑色
? ? ? ? StatusBarUtil.setStatusBarLightMode(this, true);
? ? ? ? setContentView(bindLayout());
?
? ? ? ? mABLoadingDialog = new LoadingDialog(this);
? ? ? ? mContext = this;
? ? ? ? initView();
? ? ? ? initData();
? ? ? ? setListeners();
? ? }
?
? ? @Override
? ? protected void onResume() {
? ? ? ? super.onResume();
? ? ? ? autoRefresh();
? ? }
?
? ? @Override
? ? public void finish() {
? ? ? ? BaseApplication.getInstance().removeAcitivity(this);
? ? ? ? super.finish();
? ? }
?
? ? /**
? ? ?* 簡化初始化過程
? ? ?* @param id
? ? ?* @param 
? ? ?* @return
? ? ?*/
? ? protected  T bindView(int id) {
? ? ? ? @SuppressWarnings("unchecked") T t = (T) findViewById(id);
? ? ? ? return t;
? ? }
?
? ? /**
? ? ?* 綁定布局
? ? ?* @return
? ? ?*/
? ? protected abstract int bindLayout();
?
? ? /**
? ? ?* 初始化組件
? ? ?*/
? ? protected abstract void initView();
?
? ? /**
? ? ?* 數據
? ? ?*/
? ? protected abstract void initData();
?
? ? /**
? ? ?* 設置監聽
? ? ?*/
? ? protected abstract void setListeners();
?
? ? /**
? ? ?* 自動刷新
? ? ?*/
? ? protected abstract void autoRefresh();
?
? ? /**
? ? ?* 加載動畫
? ? ?*/
? ? public void showOrHideWaitBar(boolean show) {
? ? ? ? if (mABLoadingDialog != null) {
? ? ? ? ? ? if (show) {
? ? ? ? ? ? ? ? mABLoadingDialog.show();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? mABLoadingDialog.dismiss();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? @Override
? ? public void onDestroy() {
? ? ? ? if (mABLoadingDialog != null && mABLoadingDialog.isDialogShowing()) {
? ? ? ? ? ? mABLoadingDialog.dismiss();
? ? ? ? }
? ? ? ? super.onDestroy();
? ? }
?
}

以上便實現了,這個功能的所有效果。

當我們APP使用時也會常用到Application,也就是iOS里面的AppDelegate。可以方便我們實現很多方法

下面是Application的實現

package com.zkteco.pridebiosecurity.base;
?
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
?
import com.zkteco.pridebiosecurity.util.CrashHandler;
import com.zkteco.pridebiosecurity.view.login.LoginActivity;
?
import java.util.Set;
import java.util.WeakHashMap;
?
/**
?* APP主入口
?*
?* @author sunyd, 2016-7-19
?*/
public class BaseApplication extends Application {
?
? ? private final WeakHashMap mActivityGroup = new WeakHashMap<>(1);
? ? private static BaseApplication sInstance = null;
?
? ? @Override
? ? public void onCreate() {
? ? ? ? super.onCreate();
? ? ? ? sInstance = this;
? ? }
?
? ? /**
? ? ?* 獲取全局context
? ? ?* @return
? ? ?*/
? ? public static BaseApplication getInstance() {
? ? ? ? return sInstance;
? ? }
?
? ? /**
? ? ?* 添加Activity
? ? ?* @param a
? ? ?*/
? ? protected void addActivity(Activity a) {
? ? ? ? mActivityGroup.put(a, 0);
? ? }
?
? ? /**
? ? ?* 移除Activity
? ? ?* @param a
? ? ?*/
? ? protected void removeAcitivity(Activity a) {
? ? ? ? mActivityGroup.remove(a);
? ? }
?
? ? /**
? ? ?* 獲取所有Activity
? ? ?* @param c
? ? ?* @return
? ? ?*/
? ? public Activity getActivityOfClass(Class c) {
? ? ? ? Set set = mActivityGroup.keySet();
? ? ? ? for (Activity a : set) {
? ? ? ? ? ? if (a.getClass() == c) {
? ? ? ? ? ? ? ? return a;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
?
? ? /**
? ? ?* 退出所有Activity
? ? ?*/
? ? public void exitAllActivities() {
? ? ? ? while (mActivityGroup.size() > 0) {
? ? ? ? ? ? Set as = mActivityGroup.keySet();
? ? ? ? ? ? ((Activity[]) as.toArray())[0].finish();
? ? ? ? }
? ? ? ? mActivityGroup.clear();
? ? }
}

原文鏈接:https://blog.csdn.net/WangQingLei0307/article/details/88168271

欄目分類
最近更新