| | |
| | | package com.application.zhangshi_app_android.ui.main; |
| | | |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.text.TextUtils; |
| | | import android.view.MenuItem; |
| | | import android.view.View; |
| | | |
| | | 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.manager.AppManager; |
| | | import com.application.zhangshi_app_android.BR; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.UpdateVersionBean; |
| | | import com.application.zhangshi_app_android.databinding.ActivityMainBinding; |
| | | import com.application.zhangshi_app_android.ui.dialog.VersionUpdateDialog; |
| | | 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.personal_center.PersonalCenterFragment; |
| | | import com.blankj.utilcode.util.AppUtils; |
| | | import com.blankj.utilcode.util.ToastUtils; |
| | | import com.google.android.material.navigation.NavigationBarView; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | * @desc app主页 |
| | | */ |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter; |
| | | @Override |
| | | public int getLayoutId() { |
| | | return R.layout.activity_main; |
| | |
| | | |
| | | @Override |
| | | public void initView() { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter.addFragment(HomeFragment.newInstance()); |
| | | mPagerAdapter.addFragment(FunctionFragment.newInstance()); |
| | | mPagerAdapter.addFragment(PersonalCenterFragment.newInstance()); |
| | |
| | | super.onPageSelected(position); |
| | | } |
| | | }); |
| | | |
| | | binding.mainVp.setUserInputEnabled(false); |
| | | binding.mainVp.setOffscreenPageLimit(3); |
| | | // 不使用图标默认变色 |
| | | binding.mainBnv.setItemIconTintList(null); |
| | | binding.mainBnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { |
| | |
| | | |
| | | @Override |
| | | public void initData() { |
| | | |
| | | viewModel.getUpdateVersion(); |
| | | } |
| | | |
| | | @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.isForceUpdate()){ |
| | | dialogBuilder.setCancelable(false); |
| | | dialogBuilder.setCanceledOnTouchOutside(false); |
| | | |
| | | } |
| | | dialogBuilder.show(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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, HomeFragment.class); |
| | | } |
| | | |
| | | public static void start(Context context, Class<? extends BaseFragment> 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); |
| | | } |
| | | |
| | | } |