From a55ad194eb09905914b77afc2addf9a92311e5e3 Mon Sep 17 00:00:00 2001 From: ZaneYork Date: Mon, 8 Jun 2020 11:11:01 +0800 Subject: [PATCH] 1.Tab layout --- .../com/zane/smapiinstaller/MainActivity.java | 13 +++- .../ui/install/InstallFragment.java | 6 +- .../ui/main/MainTabPagerAdapter.java | 75 +++++++++++++++++++ .../ui/main/MainTabsFragment.java | 59 +++++++++++++++ app/src/main/res/layout/fragment_main.xml | 17 +++++ .../main/res/menu/activity_main_drawer.xml | 4 +- .../main/res/navigation/mobile_navigation.xml | 17 +++-- 7 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabPagerAdapter.java create mode 100644 app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabsFragment.java create mode 100644 app/src/main/res/layout/fragment_main.xml diff --git a/app/src/main/java/com/zane/smapiinstaller/MainActivity.java b/app/src/main/java/com/zane/smapiinstaller/MainActivity.java index 5855b8a..c5fac58 100644 --- a/app/src/main/java/com/zane/smapiinstaller/MainActivity.java +++ b/app/src/main/java/com/zane/smapiinstaller/MainActivity.java @@ -63,7 +63,7 @@ public class MainActivity extends AppCompatActivity { private AppBarConfiguration mAppBarConfiguration; @BindView(R.id.launch) - FloatingActionButton buttonLaunch; + public FloatingActionButton buttonLaunch; @BindView(R.id.toolbar) Toolbar toolbar; @@ -74,7 +74,9 @@ public class MainActivity extends AppCompatActivity { @BindView(R.id.nav_view) NavigationView navigationView; - private int currentFragment = R.id.nav_install; + private int currentFragment = R.id.nav_main; + + public static MainActivity instance; private void requestPermissions() { if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) @@ -98,6 +100,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + instance = this; setContentView(R.layout.activity_main); ButterKnife.bind(this); AppConfig appConfig = ConfigUtils.getConfig((MainApplication) this.getApplication(), AppConfigKey.PRIVACY_POLICY_CONFIRM, false); @@ -122,7 +125,7 @@ public class MainActivity extends AppCompatActivity { // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. mAppBarConfiguration = new AppBarConfiguration.Builder( - R.id.nav_install, R.id.nav_config, R.id.nav_help, R.id.nav_download, R.id.nav_about) + R.id.nav_main, R.id.nav_config, R.id.nav_help, R.id.nav_download, R.id.nav_about) .setOpenableLayout(drawer) .build(); final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); @@ -406,5 +409,9 @@ public class MainActivity extends AppCompatActivity { DialogUtils.dismissDialog(); super.onDestroy(); } + + public void setFloatingBarVisibility(boolean value) { + buttonLaunch.setVisibility(value ? View.VISIBLE: View.INVISIBLE); + } } diff --git a/app/src/main/java/com/zane/smapiinstaller/ui/install/InstallFragment.java b/app/src/main/java/com/zane/smapiinstaller/ui/install/InstallFragment.java index fb8dfbe..4c5096d 100644 --- a/app/src/main/java/com/zane/smapiinstaller/ui/install/InstallFragment.java +++ b/app/src/main/java/com/zane/smapiinstaller/ui/install/InstallFragment.java @@ -20,6 +20,8 @@ import com.zane.smapiinstaller.constant.DialogAction; import com.zane.smapiinstaller.logic.ApkPatcher; import com.zane.smapiinstaller.logic.CommonLogic; import com.zane.smapiinstaller.logic.ModAssetsManager; +import com.zane.smapiinstaller.ui.main.MainTabsFragment; +import com.zane.smapiinstaller.ui.main.MainTabsFragmentDirections; import com.zane.smapiinstaller.utils.ConfigUtils; import com.zane.smapiinstaller.utils.DialogUtils; @@ -128,7 +130,7 @@ public class InstallFragment extends Fragment { DialogUtils.showConfirmDialog(root, R.string.error, StringUtils.firstNonBlank(patcher.getErrorMessage().get(), context.getString(R.string.failed_to_patch_game)), R.string.menu_download, R.string.cancel, (d, which) -> { if (which == DialogAction.POSITIVE) { NavController controller = Navigation.findNavController(installButton); - controller.navigate(InstallFragmentDirections.actionNavInstallToNavDownload()); + controller.navigate(MainTabsFragmentDirections.actionNavMainToNavDownload()); } }); } else { @@ -198,7 +200,7 @@ public class InstallFragment extends Fragment { DialogUtils.showConfirmDialog(root, R.string.error, StringUtils.firstNonBlank(patcher.getErrorMessage().get(), context.getString(R.string.failed_to_patch_game)), R.string.menu_download, R.string.cancel, (d, which) -> { if (which == DialogAction.POSITIVE) { NavController controller = Navigation.findNavController(installButton); - controller.navigate(InstallFragmentDirections.actionNavInstallToNavDownload()); + controller.navigate(MainTabsFragmentDirections.actionNavMainToNavDownload()); } }); } else { diff --git a/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabPagerAdapter.java b/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabPagerAdapter.java new file mode 100644 index 0000000..48a88ac --- /dev/null +++ b/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabPagerAdapter.java @@ -0,0 +1,75 @@ +package com.zane.smapiinstaller.ui.main; + +import android.os.Bundle; + +import com.zane.smapiinstaller.R; +import com.zane.smapiinstaller.ui.about.AboutFragment; +import com.zane.smapiinstaller.ui.config.ConfigFragment; +import com.zane.smapiinstaller.ui.download.DownloadableContentFragment; +import com.zane.smapiinstaller.ui.help.HelpFragment; +import com.zane.smapiinstaller.ui.install.InstallFragment; + +import org.jetbrains.annotations.NotNull; + +import androidx.fragment.app.Fragment; +import androidx.viewpager2.adapter.FragmentStateAdapter; + +class MainTabPagerAdapter extends FragmentStateAdapter { + private Fragment fragment; + + public MainTabPagerAdapter(Fragment fragment) { + super(fragment); + this.fragment = fragment; + } + + @NotNull + @Override + public Fragment createFragment(int position) { + Bundle args = new Bundle(); + Fragment fragment; + switch (position){ + case 1: + fragment = new ConfigFragment(); + fragment.setArguments(args); + return fragment; + case 2: + fragment = new DownloadableContentFragment(); + fragment.setArguments(args); + return fragment; + case 3: + fragment = new HelpFragment(); + fragment.setArguments(args); + return fragment; + case 4: + fragment = new AboutFragment(); + fragment.setArguments(args); + return fragment; + case 0: + default: + fragment = new InstallFragment(); + fragment.setArguments(args); + return fragment; + } + } + + public String getTitle(int position) { + switch (position){ + case 1: + return fragment.getString(R.string.menu_config); + case 2: + return fragment.getString(R.string.menu_download); + case 3: + return fragment.getString(R.string.menu_help); + case 4: + return fragment.getString(R.string.menu_about); + case 0: + default: + return fragment.getString(R.string.menu_install); + } + } + + @Override + public int getItemCount() { + return 5; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabsFragment.java b/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabsFragment.java new file mode 100644 index 0000000..b210868 --- /dev/null +++ b/app/src/main/java/com/zane/smapiinstaller/ui/main/MainTabsFragment.java @@ -0,0 +1,59 @@ +package com.zane.smapiinstaller.ui.main; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.google.android.material.tabs.TabLayout; +import com.google.android.material.tabs.TabLayoutMediator; +import com.zane.smapiinstaller.MainActivity; +import com.zane.smapiinstaller.R; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.viewpager2.widget.ViewPager2; +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * @author Zane + */ +public class MainTabsFragment extends Fragment { + + @BindView(R.id.main_tab_layout) + TabLayout tabLayout; + @BindView(R.id.main_pager) + ViewPager2 viewPager; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_main, container, false); + ButterKnife.bind(this, view); + return view; + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + MainTabPagerAdapter pagerAdapter = new MainTabPagerAdapter(this); + viewPager.setAdapter(pagerAdapter); + new TabLayoutMediator(tabLayout, viewPager, + (tab, position) -> tab.setText(pagerAdapter.getTitle(position)) + ).attach(); + viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { + @Override + public void onPageSelected(int position) { + super.onPageSelected(position); + if (position >= 3) { + MainActivity.instance.setFloatingBarVisibility(false); + } else { + MainActivity.instance.setFloatingBarVisibility(true); + } + } + }); + } +} diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml new file mode 100644 index 0000000..df553d5 --- /dev/null +++ b/app/src/main/res/layout/fragment_main.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 9e25cb1..f50397e 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -5,9 +5,9 @@ + android:title="@string/nav_header_title" /> - + app:startDestination="@+id/nav_main"> + + + -