diff options
author | Arne Schwabe <arne@rfc2549.org> | 2024-07-26 15:26:17 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2024-07-26 15:26:17 +0200 |
commit | 6b0a2e59b20fa328ff7f82afb6867f9f8e14e344 (patch) | |
tree | 524947e9b42d790d685a24773c770a462715931d | |
parent | aae95613927ad80794bb5683208172aa6269bd23 (diff) |
Move to viewpager2
-rw-r--r-- | gradle/libs.versions.toml | 2 | ||||
-rw-r--r-- | main/build.gradle.kts | 1 | ||||
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt | 14 | ||||
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.java | 220 | ||||
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt | 208 | ||||
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.java | 80 | ||||
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt | 59 | ||||
-rw-r--r-- | main/src/ui/res/layout/main_activity.xml | 27 |
8 files changed, 294 insertions, 317 deletions
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9dffd27a..a9c675c5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,6 +13,7 @@ androidx-security-crypto = "1.1.0-alpha06" androidx-constraintlayout = "2.1.4" androidx-cardview = "1.0.0" androidx-recyclerview = "1.3.2" +androidx-viewpager2 = "1.1.0" bouncycastle = "1.69" mpandroidchart = "v3.1.0" kotlin = "1.9.20" @@ -29,6 +30,7 @@ android-view-material = { group = "com.google.android.material", name = "materia androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidx-annotation" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" } androidx-cardview = { group = "androidx.cardview", name = "cardview", version.ref = "androidx-cardview" } +androidx-viewpager2 = { group = "androidx.viewpager2", name = "viewpager2", version.ref= "androidx-viewpager2" } androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" } androidx-core-ktx = { group = "androidx.core", name = "core", version.ref = "androidx-core-ktx" } androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "androidx-fragment-ktx" } diff --git a/main/build.gradle.kts b/main/build.gradle.kts index 984f075f..20dc6479 100644 --- a/main/build.gradle.kts +++ b/main/build.gradle.kts @@ -237,6 +237,7 @@ dependencies { uiImplementation(libs.android.view.material) uiImplementation(libs.androidx.appcompat) uiImplementation(libs.androidx.cardview) + uiImplementation(libs.androidx.viewpager2) uiImplementation(libs.androidx.constraintlayout) uiImplementation(libs.androidx.core.ktx) uiImplementation(libs.androidx.fragment.ktx) diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt b/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt index 68117b52..49778b88 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt @@ -11,23 +11,26 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.viewpager.widget.ViewPager +import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.tabs.TabLayout +import com.google.android.material.tabs.TabLayoutMediator import de.blinkt.openvpn.R import de.blinkt.openvpn.fragments.* import de.blinkt.openvpn.fragments.ImportRemoteConfig.Companion.newInstance import de.blinkt.openvpn.views.ScreenSlidePagerAdapter class MainActivity : BaseActivity() { - private lateinit var mPager: ViewPager + private lateinit var mPager: ViewPager2 private lateinit var mPagerAdapter: ScreenSlidePagerAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) - // Instantiate a ViewPager and a PagerAdapter. mPager = findViewById(R.id.pager) - mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, this) + val tablayout: TabLayout = findViewById(R.id.tab_layout) + + mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this) /* Toolbar and slider should have the same elevation */disableToolbarElevation() mPagerAdapter.addTab(R.string.vpn_list_title, VPNProfileList::class.java) @@ -41,6 +44,11 @@ class MainActivity : BaseActivity() { mPagerAdapter.addTab(R.string.openvpn_log, LogFragment::class.java) mPagerAdapter.addTab(R.string.about, AboutFragment::class.java) mPager.setAdapter(mPagerAdapter) + + TabLayoutMediator(tablayout, mPager) { tab, position -> + tab.text = mPagerAdapter.getPageTitle(position) + }.attach() + } private fun disableToolbarElevation() { diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.java b/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.java deleted file mode 100644 index 2c9eb761..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (c) 2012-2016 Arne Schwabe - * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt - */ - -package de.blinkt.openvpn.activities; - -import android.annotation.TargetApi; -import android.app.AlertDialog; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; - -import androidx.appcompat.app.ActionBar; -import androidx.viewpager.widget.ViewPager; - -import de.blinkt.openvpn.R; -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.core.ProfileManager; -import de.blinkt.openvpn.fragments.Settings_Allowed_Apps; -import de.blinkt.openvpn.fragments.Settings_Authentication; -import de.blinkt.openvpn.fragments.Settings_Basic; -import de.blinkt.openvpn.fragments.Settings_Connections; -import de.blinkt.openvpn.fragments.Settings_IP; -import de.blinkt.openvpn.fragments.Settings_Obscure; -import de.blinkt.openvpn.fragments.Settings_Routing; -import de.blinkt.openvpn.fragments.Settings_UserEditable; -import de.blinkt.openvpn.fragments.ShowConfigFragment; -import de.blinkt.openvpn.fragments.VPNProfileList; -import de.blinkt.openvpn.views.ScreenSlidePagerAdapter; - - -public class VPNPreferences extends BaseActivity { - - static final Class[] validFragments = new Class[]{ - Settings_Authentication.class, Settings_Basic.class, Settings_IP.class, - Settings_Obscure.class, Settings_Routing.class, ShowConfigFragment.class, - Settings_Connections.class, Settings_Allowed_Apps.class, - }; - - private String mProfileUUID; - private VpnProfile mProfile; - private ViewPager mPager; - private ScreenSlidePagerAdapter mPagerAdapter; - - public VPNPreferences() { - super(); - } - - - @TargetApi(Build.VERSION_CODES.KITKAT) - protected boolean isValidFragment(String fragmentName) { - for (Class c: validFragments) - if (c.getName().equals(fragmentName)) - return true; - return false; - - } - - @Override - protected void onStop() { - super.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - outState.putString(getIntent().getStringExtra(getPackageName() + ".profileUUID"),mProfileUUID); - super.onSaveInstanceState(outState); - } - - @Override - protected void onResume() { - super.onResume(); - getProfile(); - // When a profile is deleted from a category fragment in hadset mod we need to finish - // this activity as well when returning - if (mProfile==null || mProfile.profileDeleted) { - setResult(VPNProfileList.RESULT_VPN_DELETED); - finish(); - } - if (mProfile.mTemporaryProfile) - { - Toast.makeText(this, "Temporary profiles cannot be edited", Toast.LENGTH_LONG).show(); - finish(); - } - } - - private void getProfile() { - Intent intent = getIntent(); - - if(intent!=null) { - String profileUUID = intent.getStringExtra(getPackageName() + ".profileUUID"); - if(profileUUID==null) { - Bundle initialArguments = getIntent().getBundleExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS); - profileUUID = initialArguments.getString(getPackageName() + ".profileUUID"); - } - if(profileUUID!=null){ - - mProfileUUID = profileUUID; - mProfile = ProfileManager.get(this, mProfileUUID); - - } - } - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - mProfileUUID = getIntent().getStringExtra(getPackageName() + ".profileUUID"); - if(savedInstanceState!=null){ - String savedUUID = savedInstanceState.getString(getPackageName() + ".profileUUID"); - if(savedUUID!=null) - mProfileUUID=savedUUID; - } - super.onCreate(savedInstanceState); - - mProfile = ProfileManager.get(this,mProfileUUID); - if(mProfile==null) { - Toast.makeText(this, "Profile to edit cannot be found.", Toast.LENGTH_LONG).show(); - finish(); - return; - } - - setTitle(getString(R.string.edit_profile_title, mProfile.getName())); - - - setContentView(R.layout.main_activity); - - disableToolbarElevation(); - - // Instantiate a ViewPager and a PagerAdapter. - mPager = findViewById(R.id.pager); - mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), this); - - - Bundle fragmentArguments = new Bundle(); - fragmentArguments.putString(getPackageName() + ".profileUUID",mProfileUUID); - mPagerAdapter.setFragmentArgs(fragmentArguments); - - if (mProfile.mUserEditable) { - mPagerAdapter.addTab(R.string.basic, Settings_Basic.class); - mPagerAdapter.addTab(R.string.server_list, Settings_Connections.class); - mPagerAdapter.addTab(R.string.ipdns, Settings_IP.class); - mPagerAdapter.addTab(R.string.routing, Settings_Routing.class); - mPagerAdapter.addTab(R.string.settings_auth, Settings_Authentication.class); - - mPagerAdapter.addTab(R.string.advanced, Settings_Obscure.class); - } else { - mPagerAdapter.addTab(R.string.basic, Settings_UserEditable.class); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - mPagerAdapter.addTab(R.string.vpn_allowed_apps, Settings_Allowed_Apps.class); - } - mPagerAdapter.addTab(R.string.generated_config, ShowConfigFragment.class); - - - mPager.setAdapter(mPagerAdapter); - - //TabBarView tabs = (TabBarView) findViewById(R.id.sliding_tabs); - //tabs.setViewPager(mPager); - - } - - - @Override - public void onBackPressed() { - setResult(RESULT_OK, getIntent()); - super.onBackPressed(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == R.id.remove_vpn) - askProfileRemoval(); - if (item.getItemId() == R.id.duplicate_vpn) { - Intent data = new Intent(); - data.putExtra(VpnProfile.EXTRA_PROFILEUUID, mProfileUUID); - setResult(VPNProfileList.RESULT_VPN_DUPLICATE, data); - finish(); - } - - return super.onOptionsItemSelected(item); - } - - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - - getMenuInflater().inflate(R.menu.vpnpreferences_menu, menu); - - return super.onCreateOptionsMenu(menu); - } - - private void askProfileRemoval() { - AlertDialog.Builder dialog = new AlertDialog.Builder(this); - dialog.setTitle("Confirm deletion"); - dialog.setMessage(getString(R.string.remove_vpn_query, mProfile.mName)); - - dialog.setPositiveButton(android.R.string.yes, - (dialog1, which) -> removeProfile(mProfile)); - dialog.setNegativeButton(android.R.string.no,null); - dialog.create().show(); - } - - protected void removeProfile(VpnProfile profile) { - ProfileManager.getInstance(this).removeProfile(this,profile); - setResult(VPNProfileList.RESULT_VPN_DELETED); - finish(); - - } - - private void disableToolbarElevation() { - ActionBar toolbar = getSupportActionBar(); - toolbar.setElevation(0); - } - -} diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt b/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt new file mode 100644 index 00000000..fd9fd43e --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2012-2016 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ +package de.blinkt.openvpn.activities + +import android.annotation.TargetApi +import android.app.AlertDialog +import android.content.DialogInterface +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.preference.PreferenceActivity +import android.view.Menu +import android.view.MenuItem +import android.widget.Toast +import androidx.viewpager2.widget.ViewPager2 +import com.google.android.material.tabs.TabLayout +import com.google.android.material.tabs.TabLayoutMediator +import de.blinkt.openvpn.R +import de.blinkt.openvpn.VpnProfile +import de.blinkt.openvpn.core.ProfileManager +import de.blinkt.openvpn.fragments.Settings_Allowed_Apps +import de.blinkt.openvpn.fragments.Settings_Authentication +import de.blinkt.openvpn.fragments.Settings_Basic +import de.blinkt.openvpn.fragments.Settings_Connections +import de.blinkt.openvpn.fragments.Settings_IP +import de.blinkt.openvpn.fragments.Settings_Obscure +import de.blinkt.openvpn.fragments.Settings_Routing +import de.blinkt.openvpn.fragments.Settings_UserEditable +import de.blinkt.openvpn.fragments.ShowConfigFragment +import de.blinkt.openvpn.fragments.VPNProfileList +import de.blinkt.openvpn.views.ScreenSlidePagerAdapter + +class VPNPreferences : BaseActivity() { + private var mProfileUUID: String? = null + private var mProfile: VpnProfile? = null + private lateinit var mPager: ViewPager2 + private lateinit var mPagerAdapter: ScreenSlidePagerAdapter + + @TargetApi(Build.VERSION_CODES.KITKAT) + protected fun isValidFragment(fragmentName: String): Boolean { + for (c in validFragments) if (c.name == fragmentName) return true + return false + } + + override fun onStop() { + super.onStop() + } + + override fun onSaveInstanceState(outState: Bundle) { + outState.putString(intent.getStringExtra("$packageName.profileUUID"), mProfileUUID) + super.onSaveInstanceState(outState) + } + + override fun onResume() { + super.onResume() + profile + // When a profile is deleted from a category fragment in hadset mod we need to finish + // this activity as well when returning + if (mProfile == null || mProfile!!.profileDeleted) { + setResult(VPNProfileList.RESULT_VPN_DELETED) + finish() + } + if (mProfile!!.mTemporaryProfile) { + Toast.makeText(this, "Temporary profiles cannot be edited", Toast.LENGTH_LONG).show() + finish() + } + } + + private val profile: Unit + get() { + val intent = intent + + if (intent != null) { + var profileUUID = intent.getStringExtra("$packageName.profileUUID") + if (profileUUID == null) { + val initialArguments = + getIntent().getBundleExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS) + profileUUID = initialArguments!!.getString("$packageName.profileUUID") + } + if (profileUUID != null) { + mProfileUUID = profileUUID + mProfile = ProfileManager.get(this, mProfileUUID) + } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + mProfileUUID = intent.getStringExtra("$packageName.profileUUID") + if (savedInstanceState != null) { + val savedUUID = savedInstanceState.getString("$packageName.profileUUID") + if (savedUUID != null) mProfileUUID = savedUUID + } + super.onCreate(savedInstanceState) + + mProfile = ProfileManager.get(this, mProfileUUID) + if (mProfile == null) { + Toast.makeText(this, "Profile to edit cannot be found.", Toast.LENGTH_LONG).show() + finish() + return + } + + title = getString(R.string.edit_profile_title, mProfile!!.name) + + + setContentView(R.layout.main_activity) + + disableToolbarElevation() + + // Instantiate a ViewPager and a PagerAdapter. + mPager = findViewById(R.id.pager) + val tablayout: TabLayout = findViewById(R.id.tab_layout) + mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this) + + + val fragmentArguments = Bundle() + fragmentArguments.putString("$packageName.profileUUID", mProfileUUID) + mPagerAdapter.setFragmentArgs(fragmentArguments) + + if (mProfile!!.mUserEditable) { + mPagerAdapter.addTab(R.string.basic, Settings_Basic::class.java) + mPagerAdapter.addTab(R.string.server_list, Settings_Connections::class.java) + mPagerAdapter.addTab(R.string.ipdns, Settings_IP::class.java) + mPagerAdapter.addTab(R.string.routing, Settings_Routing::class.java) + mPagerAdapter.addTab(R.string.settings_auth, Settings_Authentication::class.java) + + mPagerAdapter.addTab(R.string.advanced, Settings_Obscure::class.java) + } else { + mPagerAdapter.addTab(R.string.basic, Settings_UserEditable::class.java) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mPagerAdapter.addTab(R.string.vpn_allowed_apps, Settings_Allowed_Apps::class.java) + } + mPagerAdapter.addTab(R.string.generated_config, ShowConfigFragment::class.java) + + + mPager.setAdapter(mPagerAdapter) + + //TabBarView tabs = (TabBarView) findViewById(R.id.sliding_tabs); + //tabs.setViewPager(mPager); + + TabLayoutMediator(tablayout, mPager) { tab, position -> + tab.text = mPagerAdapter.getPageTitle(position) + }.attach() + } + + + override fun onBackPressed() { + setResult(RESULT_OK, intent) + super.onBackPressed() + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == R.id.remove_vpn) askProfileRemoval() + if (item.itemId == R.id.duplicate_vpn) { + val data = Intent() + data.putExtra(VpnProfile.EXTRA_PROFILEUUID, mProfileUUID) + setResult(VPNProfileList.RESULT_VPN_DUPLICATE, data) + finish() + } + + return super.onOptionsItemSelected(item) + } + + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.vpnpreferences_menu, menu) + + return super.onCreateOptionsMenu(menu) + } + + private fun askProfileRemoval() { + val dialog = AlertDialog.Builder(this) + dialog.setTitle("Confirm deletion") + dialog.setMessage(getString(R.string.remove_vpn_query, mProfile!!.mName)) + + dialog.setPositiveButton( + android.R.string.yes + ) { dialog1: DialogInterface?, which: Int -> removeProfile(mProfile) } + dialog.setNegativeButton(android.R.string.no, null) + dialog.create().show() + } + + protected fun removeProfile(profile: VpnProfile?) { + ProfileManager.getInstance(this).removeProfile(this, profile) + setResult(VPNProfileList.RESULT_VPN_DELETED) + finish() + } + + private fun disableToolbarElevation() { + val toolbar = supportActionBar + toolbar!!.elevation = 0f + } + + companion object { + val validFragments: Array<Class<*>> = arrayOf( + Settings_Authentication::class.java, + Settings_Basic::class.java, + Settings_IP::class.java, + Settings_Obscure::class.java, + Settings_Routing::class.java, + ShowConfigFragment::class.java, + Settings_Connections::class.java, + Settings_Allowed_Apps::class.java, + ) + } +} diff --git a/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.java b/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.java deleted file mode 100644 index 4f1150a2..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2012-2016 Arne Schwabe - * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt - */ - -package de.blinkt.openvpn.views; - -import android.content.Context; -import android.content.res.Resources; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.annotation.StringRes; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; - -import java.util.Vector; - -/** -* Created by arne on 18.11.14. -*/ -public class ScreenSlidePagerAdapter extends FragmentPagerAdapter { - - private final Resources res; - private Bundle mFragmentArguments; - - public void setFragmentArgs(Bundle fragmentArguments) { - mFragmentArguments = fragmentArguments; - } - - static class Tab { - public Class<? extends Fragment> fragmentClass; - String mName; - - public Tab(Class<? extends Fragment> fClass, String name){ - mName = name; - fragmentClass = fClass; - } - - } - - - private Vector<Tab> mTabs = new Vector<Tab>(); - - public ScreenSlidePagerAdapter(FragmentManager fm, Context c) { - super(fm); - res = c.getResources(); - } - - @NonNull - @Override - public Fragment getItem(int position) { - try { - Fragment fragment = mTabs.get(position).fragmentClass.newInstance(); - if (mFragmentArguments!=null) - fragment.setArguments(mFragmentArguments); - return fragment; - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return null; - } - - @Override - public CharSequence getPageTitle(int position) { - return mTabs.get(position).mName; - } - - @Override - public int getCount() { - return mTabs.size(); - } - - public void addTab(@StringRes int name, Class<? extends Fragment> fragmentClass) { - mTabs.add(new Tab(fragmentClass, res.getString(name))); - } -} diff --git a/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt b/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt new file mode 100644 index 00000000..0ce0afaa --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2012-2016 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ +package de.blinkt.openvpn.views + +import android.content.Context +import android.content.res.Resources +import android.os.Bundle +import androidx.annotation.StringRes +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.lifecycle.Lifecycle +import androidx.viewpager2.adapter.FragmentStateAdapter +import java.util.Vector + +/** + * Created by arne on 18.11.14. + */ +class ScreenSlidePagerAdapter(fm: FragmentManager, lc: Lifecycle, c: Context) : + FragmentStateAdapter( + fm, lc + ) { + private val res: Resources = c.resources + private var mFragmentArguments: Bundle? = null + + fun setFragmentArgs(fragmentArguments: Bundle?) { + mFragmentArguments = fragmentArguments + } + + internal class Tab(var fragmentClass: Class<out Fragment>, var mName: String) + + private val mTabs = Vector<Tab>() + + override fun createFragment(position: Int): Fragment { + try { + val fragment = mTabs[position].fragmentClass.newInstance() + if (mFragmentArguments != null) fragment.arguments = mFragmentArguments + return fragment + } catch (e: InstantiationException) { + e.printStackTrace() + } catch (e: IllegalAccessException) { + e.printStackTrace() + } + throw IndexOutOfBoundsException("index wrong") + } + + fun getPageTitle(position: Int): CharSequence { + return mTabs[position].mName + } + + override fun getItemCount(): Int { + return mTabs.size + } + + fun addTab(@StringRes name: Int, fragmentClass: Class<out Fragment>) { + mTabs.add(Tab(fragmentClass, res.getString(name))) + } +} diff --git a/main/src/ui/res/layout/main_activity.xml b/main/src/ui/res/layout/main_activity.xml index b2fe2251..edc64ece 100644 --- a/main/src/ui/res/layout/main_activity.xml +++ b/main/src/ui/res/layout/main_activity.xml @@ -9,23 +9,22 @@ android:layout_height="match_parent" android:orientation="vertical"> - - <androidx.viewpager.widget.ViewPager - android:id="@+id/pager" + <com.google.android.material.tabs.TabLayout + android:id="@+id/tab_layout" + style="@style/blinkt.tabLayout" + app:tabMaxWidth="0dp" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content" + android:layout_gravity="top" + app:tabMode="scrollable" - <com.google.android.material.tabs.TabLayout - android:id="@+id/tab_layout" - style="@style/blinkt.tabLayout" - app:tabMaxWidth="0dp" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="top" - app:tabMode="scrollable" + /> - /> + <androidx.viewpager2.widget.ViewPager2 + android:id="@+id/pager" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="3"/> - </androidx.viewpager.widget.ViewPager> </LinearLayout> |