java安卓开发模板软件_原生android app常用开发模板

本文介绍了一个基于Java的原生Android应用开发模板,详细展示了如何创建底部导航栏、设置菜单栏点击事件以及切换不同Fragment。代码示例中包含了初始化BottomNavigationView、设置字体和颜色,以及处理页面点击事件的方法。
摘要由CSDN通过智能技术生成

package com.mygoods;

import android.graphics.Typeface;

import android.os.Bundle;

import android.support.v4.app.FragmentManager;

import android.support.v4.content.ContextCompat;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.Toast;

import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationItem;

import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;

import com.luseen.luseenbottomnavigation.BottomNavigation.OnBottomNavigationItemClickListener;

import com.mygoods.ui.data.DataFragment;

import com.mygoods.ui.guard.GuardFragment;

import com.mygoods.ui.index.IndexFragment;

import com.mygoods.ui.me.MeFragment;

import com.mygoods.utils.FragmentManagerHelper;

import com.mygoods.view.BaseActivity;

import com.qiantao.coordinatormenu.CoordinatorMenu;

/**

* create by yang on 2017/10/11 16:48

*

*/

public class MainActivity extends BaseActivity implements View.OnClickListener{

private BottomNavigationView bottomNavigationView;

private CoordinatorMenu mCoordinatorMenu;

private FragmentManagerHelper mFragmentHelper;

//private ImageView mHeadIv;//左上角按钮

private LinearLayout ll_slidePage_item1;//侧滑栏

private FragmentManager fragmentManager;

private IndexFragment mIndexFragment;//首页Fragment

private GuardFragment mGuardFragment;//保障Fragment

private DataFragment mDataFragment;//数据Fragment

private MeFragment mMeFragment;//个人中心Fragment

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initData();

initSetting();

initListen();

}

private void initView() {

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation);

mCoordinatorMenu = (CoordinatorMenu) findViewById(R.id.cm_slide_and_index);

ll_slidePage_item1 = (LinearLayout) findViewById(R.id.ll_slidePage_item1);

ll_slidePage_item1.setOnClickListener(this);

}

private void initData() {

fragmentManager = getSupportFragmentManager();

mFragmentHelper = new FragmentManagerHelper(fragmentManager,R.id.fl_fragment);

//默认加载首页

mIndexFragment = new IndexFragment();

mFragmentHelper.addFg(mIndexFragment);

int[] image = {R.drawable.ic_mic_black_24dp, R.drawable.ic_favorite_black_24dp,

R.drawable.ic_book_black_24dp, R.drawable.github_circle};

int[] color = {ContextCompat.getColor(this, R.color.firstColor), ContextCompat.getColor(this, R.color.firstColor),

ContextCompat.getColor(this, R.color.firstColor), ContextCompat.getColor(this, R.color.firstColor)};

//初始化底部菜单

if (bottomNavigationView != null) {

//汉字是否一直显示:(false:显示选中的;true全部显示)

bottomNavigationView.isWithText(false);

//菜单栏在左侧

//bottomNavigationView.activateTabletMode();

//关闭动画

// bottomNavigationView.disableViewPagerSlide();

//去掉阴影

//bottomNavigationView.disableShadow();

//整体背景色

bottomNavigationView.isColoredBackground(true);

//选中字体的大小

bottomNavigationView.setTextActiveSize(getResources().getDimension(R.dimen.text_active));

//未选中字体的大小

bottomNavigationView.setTextInactiveSize(getResources().getDimension(R.dimen.text_inactive));

//当 bottomNavigationView.isColoredBackground(true);设置为false时icon和汉字显示颜色能用

bottomNavigationView.setItemActiveColorWithoutColoredBackground(ContextCompat.getColor(this, R.color.firstColor));

bottomNavigationView.setFont(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Noh_normal.ttf"));

//使用viewpager

// bottomNavigationView.setUpWithViewPager(yourPager , colorResources , imageResources);

}

//菜单栏文字颜色图片

BottomNavigationItem bottomNavigationItem = new BottomNavigationItem

("动态", color[0], image[0]);

BottomNavigationItem bottomNavigationItem1 = new BottomNavigationItem

("保障", color[1], image[1]);

BottomNavigationItem bottomNavigationItem2 = new BottomNavigationItem

("数据", color[2], image[2]);

BottomNavigationItem bottomNavigationItem3 = new BottomNavigationItem

("个人中心", color[3], image[3]);

bottomNavigationView.addTab(bottomNavigationItem);

bottomNavigationView.addTab(bottomNavigationItem1);

bottomNavigationView.addTab(bottomNavigationItem2);

bottomNavigationView.addTab(bottomNavigationItem3);

}

private void initSetting() {

}

private void initListen() {

//首页左上角按钮

// mHeadIv.setOnClickListener(new View.OnClickListener() {

// @Override

// public void onClick(View v) {

// if (mCoordinatorMenu.isOpened()) {

// mCoordinatorMenu.closeMenu();

// } else {

// mCoordinatorMenu.openMenu();

// }

// }

// });

//菜单栏点击事件

bottomNavigationView.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {

@Override

public void onNavigationItemClick(int index) {

switch (index) {

case 0://首页/动态

if(mIndexFragment == null){

mIndexFragment = new IndexFragment();

}

mFragmentHelper.switchFg(mIndexFragment);

break;

case 1://保障

if(mGuardFragment == null){

mGuardFragment = new GuardFragment();

}

mFragmentHelper.switchFg(mGuardFragment);

break;

case 2://数据

if(mDataFragment == null){

mDataFragment = new DataFragment();

}

mFragmentHelper.switchFg(mDataFragment);

break;

case 3://个人中心

if(mMeFragment == null){

mMeFragment = new MeFragment();

}

mFragmentHelper.switchFg(mMeFragment);

break;

}

}

});

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.ll_slidePage_item1://侧滑页第一个item

Toast.makeText(MainActivity.this, "你第一个要上天", Toast.LENGTH_SHORT).show();

break;

}

}

//返回键监听

@Override

public void onBackPressed() {

if (mCoordinatorMenu.isOpened()) {

mCoordinatorMenu.closeMenu();

} else {

super.onBackPressed();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值