Renew Android App View Pager

Asked 2 years ago, Updated 2 years ago, 103 views

Hello! This is the only place I can rely on.Haha

Question about updating the fragment viewflipper.


class SearchViewPagerAdapter(var fragNum : Int, fm : FragmentManager, searchText : String) : FragmentStatePagerAdapter(fm) {

    val firstFrag : Fragment = SearchTagsFragment(searchText)
    val secondFrag : Fragment = SearchAccountsFragment(searchText)


    // Decide which Fragment to move to by position
    override fun getItem(position: Int): Fragment? {
        return when (position) {
            0 -> firstFrag
            1 -> secondFrag
            else -> null
        }
    }

    // Determine how many Fragments
    override fun getCount(): Int = fragNum

The code above is viewpager adapter.kt.

The problem is, when performing the search function with TextWatcher in the second fragment, You will need to turn the page back to the side to move to the first fragment and see the search results.

When I searched about fragment renewal, they told me to put the code below in MainActivity.

 fun refresh(){
        SearchViewPagerAdapter.notifyDataSetChanged()
    }

But no matter how many times I put this and that (company object, interface...) in the viewpager adapter, The red line keeps popping up in notifyDataSetChanged().

How do we solve this?

It's not been long since the app was developed, so there are many shortcomings ㅜㅜ Please answer the questions from the masters.

Thank you!

kotlin android android-viewpager fragment

2022-09-22 18:42

1 Answers

https://www.google.com/search?client=safari&rls=en&q=FragmentStatePagerAdapter+getItemPosition&ie=UTF-8&oe=UTF-8

If you add getItemPosition() to SearchViewPagerAdapter, it will probably work


2022-09-22 18:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.