首页>代码>原生android app常用开发模板>/main/java/com/mygoods/MainActivity.java
001package com.mygoods;
002 
003import android.graphics.Typeface;
004import android.os.Bundle;
005import android.support.v4.app.FragmentManager;
006import android.support.v4.content.ContextCompat;
007import android.view.View;
008import android.widget.LinearLayout;
009import android.widget.Toast;
010 
011import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationItem;
012import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;
013import com.luseen.luseenbottomnavigation.BottomNavigation.OnBottomNavigationItemClickListener;
014import com.mygoods.ui.data.DataFragment;
015import com.mygoods.ui.guard.GuardFragment;
016import com.mygoods.ui.index.IndexFragment;
017import com.mygoods.ui.me.MeFragment;
018import com.mygoods.utils.FragmentManagerHelper;
019import com.mygoods.view.BaseActivity;
020import com.qiantao.coordinatormenu.CoordinatorMenu;
021 
022/**
023 * create by yang on 2017/10/11 16:48
024 *
025 */
026public class MainActivity extends BaseActivity implements View.OnClickListener{
027 
028    private BottomNavigationView bottomNavigationView;
029 
030    private CoordinatorMenu mCoordinatorMenu;
031 
032    private FragmentManagerHelper mFragmentHelper;
033 
034    //private ImageView mHeadIv;//左上角按钮
035    private LinearLayout ll_slidePage_item1;//侧滑栏
036    private FragmentManager fragmentManager;
037    private IndexFragment mIndexFragment;//首页Fragment
038    private GuardFragment mGuardFragment;//保障Fragment
039    private DataFragment mDataFragment;//数据Fragment
040    private MeFragment mMeFragment;//个人中心Fragment
041 
042    @Override
043    protected void onCreate(Bundle savedInstanceState) {
044        super.onCreate(savedInstanceState);
045        setContentView(R.layout.activity_main);
046 
047        initView();
048 
049        initData();
050 
051        initSetting();
052 
053        initListen();
054    }
055 
056    private void initView() {
057        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation);
058        mCoordinatorMenu = (CoordinatorMenu) findViewById(R.id.cm_slide_and_index);
059 
060        ll_slidePage_item1 = (LinearLayout) findViewById(R.id.ll_slidePage_item1);
061        ll_slidePage_item1.setOnClickListener(this);
062    }
063 
064    private void initData() {
065 
066        fragmentManager = getSupportFragmentManager();
067        mFragmentHelper = new FragmentManagerHelper(fragmentManager,R.id.fl_fragment);
068        //默认加载首页
069        mIndexFragment = new IndexFragment();
070        mFragmentHelper.addFg(mIndexFragment);
071 
072        int[] image = {R.drawable.ic_mic_black_24dp, R.drawable.ic_favorite_black_24dp,
073                R.drawable.ic_book_black_24dp, R.drawable.github_circle};
074        int[] color = {ContextCompat.getColor(this, R.color.firstColor), ContextCompat.getColor(this, R.color.firstColor),
075                ContextCompat.getColor(this, R.color.firstColor), ContextCompat.getColor(this, R.color.firstColor)};
076 
077        //初始化底部菜单
078        if (bottomNavigationView != null) {
079            //汉字是否一直显示:(false:显示选中的;true全部显示)
080            bottomNavigationView.isWithText(false);
081            //菜单栏在左侧
082            //bottomNavigationView.activateTabletMode();
083            //关闭动画
084            // bottomNavigationView.disableViewPagerSlide();
085            //去掉阴影
086            //bottomNavigationView.disableShadow();
087            //整体背景色
088            bottomNavigationView.isColoredBackground(true);
089            //选中字体的大小
090            bottomNavigationView.setTextActiveSize(getResources().getDimension(R.dimen.text_active));
091            //未选中字体的大小
092            bottomNavigationView.setTextInactiveSize(getResources().getDimension(R.dimen.text_inactive));
093            //当 bottomNavigationView.isColoredBackground(true);设置为false时icon和汉字显示颜色能用
094            bottomNavigationView.setItemActiveColorWithoutColoredBackground(ContextCompat.getColor(this, R.color.firstColor));
095            bottomNavigationView.setFont(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Noh_normal.ttf"));
096            //使用viewpager
097            // bottomNavigationView.setUpWithViewPager(yourPager , colorResources , imageResources);
098        }
099 
100        //菜单栏文字颜色图片
101        BottomNavigationItem bottomNavigationItem = new BottomNavigationItem
102                ("动态", color[0], image[0]);
103        BottomNavigationItem bottomNavigationItem1 = new BottomNavigationItem
104                ("保障", color[1], image[1]);
105        BottomNavigationItem bottomNavigationItem2 = new BottomNavigationItem
106                ("数据", color[2], image[2]);
107        BottomNavigationItem bottomNavigationItem3 = new BottomNavigationItem
108                ("个人中心", color[3], image[3]);
109 
110        bottomNavigationView.addTab(bottomNavigationItem);
111        bottomNavigationView.addTab(bottomNavigationItem1);
112        bottomNavigationView.addTab(bottomNavigationItem2);
113        bottomNavigationView.addTab(bottomNavigationItem3);
114    }
115 
116    private void initSetting() {
117 
118    }
119 
120    private void initListen() {
121        //首页左上角按钮
122//        mHeadIv.setOnClickListener(new View.OnClickListener() {
123//            @Override
124//            public void onClick(View v) {
125//                if (mCoordinatorMenu.isOpened()) {
126//                    mCoordinatorMenu.closeMenu();
127//                } else {
128//                    mCoordinatorMenu.openMenu();
129//                }
130//            }
131//        });
132 
133        //菜单栏点击事件
134        bottomNavigationView.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {
135            @Override
136            public void onNavigationItemClick(int index) {
137                switch (index) {
138                    case 0://首页/动态
139                        if(mIndexFragment == null){
140                            mIndexFragment = new IndexFragment();
141                        }
142                        mFragmentHelper.switchFg(mIndexFragment);
143                        break;
144                    case 1://保障
145                        if(mGuardFragment == null){
146                            mGuardFragment = new GuardFragment();
147                        }
148                        mFragmentHelper.switchFg(mGuardFragment);
149                        break;
150                    case 2://数据
151                        if(mDataFragment == null){
152                            mDataFragment = new DataFragment();
153                        }
154                        mFragmentHelper.switchFg(mDataFragment);
155                        break;
156                    case 3://个人中心
157                        if(mMeFragment == null){
158                            mMeFragment = new MeFragment();
159                        }
160                        mFragmentHelper.switchFg(mMeFragment);
161                        break;
162 
163                }
164            }
165        });
166 
167    }
168 
169    @Override
170    public void onClick(View v) {
171        switch (v.getId()) {
172            case R.id.ll_slidePage_item1://侧滑页第一个item
173                Toast.makeText(MainActivity.this, "你第一个要上天", Toast.LENGTH_SHORT).show();
174                break;
175 
176        }
177    }
178 
179    //返回键监听
180    @Override
181    public void onBackPressed() {
182        if (mCoordinatorMenu.isOpened()) {
183            mCoordinatorMenu.closeMenu();
184        } else {
185            super.onBackPressed();
186        }
187    }
188}
最近下载更多
652654393  LV5 2024年11月18日
求学的熊猫  LV11 2024年6月3日
学习112  LV2 2023年12月21日
yyyyyyzh  LV8 2023年6月11日
微信网友_6487832068788224  LV1 2023年5月23日
李亮  LV19 2023年4月6日
molu123456 2022年11月21日
暂无贡献等级
胖且12345z6  LV1 2022年5月4日
微信网友_5921782836367360  LV1 2022年4月20日
511076827  LV33 2022年4月11日
最近浏览更多
Arcueid  LV2 5月16日
5460545 2024年12月24日
暂无贡献等级
chenranr  LV10 2024年12月20日
Sotouch  LV15 2024年12月9日
652654393  LV5 2024年11月18日
yfb790910  LV3 2024年11月2日
wwwqwq 2024年6月21日
暂无贡献等级
2636804923  LV6 2024年6月20日
cxfaqww 2024年6月18日
暂无贡献等级
AIWU1234  LV2 2024年6月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友