K
K
Kirill2018-09-25 07:04:20
Android
Kirill, 2018-09-25 07:04:20

How can one replace the ViewPagerApapter with another one?

There is a ViewPager attached to it by MyPagerAdapter, I would like that under a certain condition, for example, if catStr == "two", then you need to attach the ViewPager already to MyPagerAdapterTwo.

<androidx.constraintlayout.widget.ConstraintLayout>
    <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs_main"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:background="?attr/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:tabIndicatorColor="#FFAB40"
            app:tabMode="fixed"
            app:tabTextColor="@color/colorWhiteTransparent"
            style="@style/MyCustomTabLayout" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

(MyPagerAdapterTwo идентичный за исключение что он выводит другие layout и текст в getPageTitle немного другой)

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 -> {
                FirstFragment()
            }
            1 -> FirstFragment()
            else -> {
                return FirstFragment()
            }
        }
    }

    override fun getCount(): Int {
        return 3
    }

    override fun getPageTitle(position: Int): CharSequence {
        return when (position) {
            0 -> "Счета"
            1 -> "Операции"
            else -> {
                return "Бюджет"
            }
        }
    }
}

Here is the most interesting thing under this condition (ONLY THE TEXT IN tabs_main CHANGES BUT the layout REMAINS THE SAME)
override fun showCategory(catStr: String, catIcon: Int, catNav: Int) {
      
        val fragmentAdapter = MyPagerAdapter(supportFragmentManager)
        val fragmentAdapterTwo = MyPagerAdapterTwo(supportFragmentManager)

        if (catStr == "Финансы")
            viewpager_main.adapter = fragmentAdapter
        else if (catStr == "Задания")
            viewpager_main.adapter = fragmentAdapterTwo

        tabs_main.setupWithViewPager(viewpager_main)
    }

What can be the assumptions?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question