| | |
| | | package com.application.zhangshi_app_android.ui.main; |
| | | |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.text.TextUtils; |
| | | import android.util.TypedValue; |
| | | import android.view.Menu; |
| | | import android.view.MenuItem; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.fragment.app.Fragment; |
| | | import androidx.lifecycle.Observer; |
| | | import androidx.viewpager2.widget.ViewPager2; |
| | | |
| | | import com.android.app_base.base.adapter.BaseVp2FragmentAdapter; |
| | | import com.android.app_base.base.view.BaseActivity; |
| | | import com.android.app_base.base.view.BaseFragment; |
| | | import com.android.app_base.helper.DoubleClickHelper; |
| | | import com.android.app_base.manager.AppManager; |
| | | import com.android.app_base.manager.UserManager; |
| | | import com.android.app_base.utils.rxbus.MessageEvent; |
| | | import com.android.app_base.utils.rxbus.RxBus; |
| | | import com.application.zhangshi_app_android.BR; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.GrowthExperienceInformationBean; |
| | | import com.application.zhangshi_app_android.bean.UpdateVersionBean; |
| | | import com.application.zhangshi_app_android.databinding.ActivityMainBinding; |
| | | import com.application.zhangshi_app_android.ui.function.FunctionFragment; |
| | | import com.application.zhangshi_app_android.ui.home.HomeFragment; |
| | | import com.application.zhangshi_app_android.ui.DLBaseActivity; |
| | | import com.application.zhangshi_app_android.ui.dialog.UpdateDialog; |
| | | import com.application.zhangshi_app_android.ui.function.HomeFragment; |
| | | import com.application.zhangshi_app_android.ui.home.TreeFragment; |
| | | import com.application.zhangshi_app_android.ui.personal_center.PersonalCenterFragment; |
| | | import com.blankj.utilcode.util.AppUtils; |
| | | import com.blankj.utilcode.util.ToastUtils; |
| | | import com.google.android.material.navigation.NavigationBarView; |
| | | |
| | |
| | | * @date 2023.3.15 |
| | | * @desc app主页 |
| | | */ |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | public class MainActivity extends DLBaseActivity<ActivityMainBinding, MainViewModel> { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter; |
| | | |
| | | @Override |
| | | protected boolean isDrawerLayoutEnabled() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public int getLayoutId() { |
| | | return R.layout.activity_main; |
| | |
| | | |
| | | @Override |
| | | public void initParam() { |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void initView() { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter.addFragment(HomeFragment.newInstance()); |
| | | mPagerAdapter.addFragment(FunctionFragment.newInstance()); |
| | | mPagerAdapter.addFragment(TreeFragment.newInstance()); |
| | | mPagerAdapter.addFragment(PersonalCenterFragment.newInstance()); |
| | | binding.mainVp.setAdapter(mPagerAdapter); |
| | | binding.mainVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { |
| | |
| | | } |
| | | }); |
| | | binding.mainVp.setUserInputEnabled(false); |
| | | |
| | | binding.mainVp.setOffscreenPageLimit(3); |
| | | // 不使用图标默认变色 |
| | | binding.mainBnv.setItemIconTintList(null); |
| | | binding.mainBnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { |
| | |
| | | case R.id.navigation_home: |
| | | binding.mainVp.setCurrentItem(0); |
| | | return true; |
| | | case R.id.navigation_function: |
| | | case R.id.navigation_tree: |
| | | binding.mainVp.setCurrentItem(1); |
| | | return true; |
| | | case R.id.navigation_personal_center: |
| | |
| | | return false; |
| | | } |
| | | }); |
| | | Menu menu = binding.mainBnv.getMenu(); |
| | | // 获取当前主题中的菜单图标资源 |
| | | TypedValue typedValue1 = new TypedValue(); |
| | | getTheme().resolveAttribute(R.attr.menuIconHome, typedValue1, true); |
| | | int icon1ResId = typedValue1.resourceId; |
| | | TypedValue typedValue2 = new TypedValue(); |
| | | getTheme().resolveAttribute(R.attr.menuIconTree, typedValue2, true); |
| | | int icon2ResId = typedValue2.resourceId; |
| | | TypedValue typedValue3 = new TypedValue(); |
| | | getTheme().resolveAttribute(R.attr.menuIconUser, typedValue3, true); |
| | | int icon3ResId = typedValue3.resourceId; |
| | | // 设置菜单项图标 |
| | | menu.findItem(R.id.navigation_home).setIcon(icon1ResId); |
| | | menu.findItem(R.id.navigation_tree).setIcon(icon2ResId); |
| | | menu.findItem(R.id.navigation_personal_center).setIcon(icon3ResId); |
| | | } |
| | | |
| | | @Override |
| | | public void initData() { |
| | | |
| | | viewModel.getUpdateVersion(); |
| | | if (TextUtils.isEmpty(UserManager.getInstance().getUserName()) || TextUtils.isEmpty(UserManager.getInstance().getUserAvatar())) { |
| | | viewModel.getInfo(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void initLiveDataObserve() { |
| | | viewModel.getUpdateVersionBeanLiveData().observe(this, new Observer<UpdateVersionBean>() { |
| | | @Override |
| | | public void onChanged(UpdateVersionBean updateVersionBean) { |
| | | if(updateVersionBean != null && updateVersionBean.getVersionCode()> AppUtils.getAppVersionCode()&&!TextUtils.isEmpty(updateVersionBean.getAppUrl())){ |
| | | // VersionUpdateDialog.Builder dialogBuilder = new VersionUpdateDialog.Builder(getSelfActivity()) |
| | | // .setContent(updateVersionBean.getContent()) |
| | | // .setDownloadUrl(updateVersionBean.getAppUrl()) |
| | | // ; |
| | | // if(updateVersionBean.getForceUpdate() == 1){ |
| | | // dialogBuilder.setCancelable(false); |
| | | // dialogBuilder.setCanceledOnTouchOutside(false); |
| | | // } |
| | | // dialogBuilder.show(); |
| | | |
| | | new UpdateDialog.Builder(getSelfActivity()) |
| | | // 版本名 |
| | | .setVersionName(updateVersionBean.getVersionName()) |
| | | // 是否强制更新 |
| | | .setForceUpdate(updateVersionBean.getForceUpdate()==1) |
| | | // 更新日志 |
| | | .setUpdateLog(updateVersionBean.getContent()) |
| | | // 下载 URL |
| | | .setDownloadUrl(updateVersionBean.getAppUrl()) |
| | | // 文件 MD5 |
| | | .setFileMd5(updateVersionBean.getMd5()) |
| | | .show(); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | viewModel.getInfoLiveData().observe(this, new Observer<GrowthExperienceInformationBean>() { |
| | | @Override |
| | | public void onChanged(GrowthExperienceInformationBean informationBean) { |
| | | if(informationBean != null){ |
| | | UserManager.getInstance().setUserName(informationBean.getNickName()); |
| | | UserManager.getInstance().setUserAvatar(informationBean.getImg()); |
| | | RxBus.getInstance().post(new MessageEvent(MessageEvent.EVENT_UPDATE_USER_INFO,informationBean)); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | protected void onNewIntent(Intent intent) { |
| | | super.onNewIntent(intent); |
| | | switchFragment(mPagerAdapter.getFragmentIndex((Class<? extends Fragment>) intent.getSerializableExtra(INTENT_KEY_IN_FRAGMENT_CLASS))); |
| | | } |
| | | |
| | | private void switchFragment(int fragmentIndex) { |
| | | if (fragmentIndex == -1) { |
| | | return; |
| | | } |
| | | |
| | | switch (fragmentIndex) { |
| | | case 0: |
| | | case 1: |
| | | case 2: |
| | | case 3: |
| | | binding.mainVp.setCurrentItem(fragmentIndex); |
| | | binding.mainBnv.setSelectedItemId(binding.mainBnv.getMenu().getItem(fragmentIndex).getItemId()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | |
| | | ToastUtils.showShort("再按一次退出"); |
| | | } |
| | | } |
| | | |
| | | private static final String INTENT_KEY_IN_FRAGMENT_INDEX = "fragmentIndex"; |
| | | private static final String INTENT_KEY_IN_FRAGMENT_CLASS = "fragmentClass"; |
| | | |
| | | public static void start(Context context) { |
| | | start(context, TreeFragment.class); |
| | | } |
| | | |
| | | public static void start(Context context, Class<? extends Fragment> fragmentClass) { |
| | | Intent intent = new Intent(context, MainActivity.class); |
| | | intent.putExtra(INTENT_KEY_IN_FRAGMENT_CLASS, fragmentClass); |
| | | if (!(context instanceof Activity)) { |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| | | } |
| | | context.startActivity(intent); |
| | | } |
| | | |
| | | } |