如何在单击选项卡时从ViewPager正确更新最后一个片段?

时间:2016-07-25 19:54:16

标签: android android-viewpager

问题描述:

  1. 我有一个包含四个片段的ViewPager;
  2. 第一个和最后一个有一个RecyclerView;
  3. 当我滚动ViewPager时,一切正常 - 这意味着所有内容都按预期加载;
  4. 如果我点击最后/第一个标签(其中包含RecyclerViews并由彼此的三个片段分开),则RecyclerView内容不会显示在屏幕上 - 所有其他UI组件(如FloatingActionButtons)都是;
  5. 我环顾四周,但与此主题相关的所有其他问题似乎只能解决滚动问题,而且我无法找到与点击标签相关的任何内容。< / em>的

    PS:代码是用Kotlin编写的

    CODE

    从Activity调用以封装与选项卡管理器相关的代码:

    class TabManager {
    
    companion object {
        val INVALID_POSITION_LABEL = "Invalid Position"
    }
    
    private var mActivity: AppCompatActivity? = null
    private var mViewPagerTab: SmartTabLayout? = null
    private var mViewPager: ViewPager? = null
    
    // PUBLIC INTERFACE ============================================================================
    fun initViewPager(activity: AppCompatActivity) {
        mActivity = activity
    
        mViewPager = activity.findViewById(R.id.viewpager) as ViewPager
        mViewPagerTab = activity.findViewById(R.id.tabs) as SmartTabLayout
    
        mViewPager!!.adapter = AdapterTab(activity.supportFragmentManager)
        mViewPager!!.currentItem = 0
    
        mViewPagerTab!!.setCustomTabView(setCustomTabView())
        mViewPagerTab!!.setViewPager(mViewPager)
    }
    
    fun setViewPagerListener(onChangeListener: (position: Int) -> Unit) {
        mViewPager!!.onPageChangeListener {
            onPageScrolled {
                position, positionOffset, positionOffsetPixels ->
                onChangeListener(position)
            }
        }
    }
    
    
    // =============================================================================================
    private fun setCustomTabView(): SmartTabLayout.TabProvider {
        return SmartTabLayout.TabProvider {
            container,
            position,
            adapter ->
            val icon = mActivity!!.layoutInflater.inflate(R.layout.rsc_homepage_tab_icon, container, false) as ImageView
            when (position) {
                0 -> icon.setImageDrawable(ContextCompat.getDrawable(mActivity!!, R.drawable.ic_profile_white_24))
                1 -> icon.setImageDrawable(ContextCompat.getDrawable(mActivity!!, R.drawable.ic_magnify_white_24))
                2 -> icon.setImageDrawable(ContextCompat.getDrawable(mActivity!!, R.drawable.ic_msg_reply_text_white_24))
                3 -> icon.setImageDrawable(ContextCompat.getDrawable(mActivity!!, R.drawable.ic_game_white_24))
                else -> throw IllegalStateException(INVALID_POSITION_LABEL + position)
            }
            icon
        }
    }
    

    }

    PageAdapter for ViewPager项目:

    class AdapterTab(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
    
        private val FRAGMENT_PROFILE = FragmentProfile()
        private val FRAGMENT_SEARCH_USER = FragmentSearchUsers()
        private val FRAGMENT_CHATS = FragmentChats()
        private val FRAGMENT_GAMES = FragmentGames()
    
        // LIFECYCLE ===================================================================================d
        override fun getPageTitle(position: Int): CharSequence {
            when (position) {
                0 -> return "PROFILE"
                1 -> return "SEARCH USER"
                2 -> return "CHATS"
                3 -> return "GAMES"
                else -> return "UNKNOWN"
            }
        }
    
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            return super.instantiateItem(container, position)
        }
    
        override fun getCount(): Int {
            return 4
        }
    
        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE
        }
    
        override fun getItem(position: Int): android.support.v4.app.Fragment {
            when (position) {
                0 -> return FRAGMENT_PROFILE
                1 -> return FRAGMENT_SEARCH_USER
                2 -> return FRAGMENT_CHATS
                3 -> return FRAGMENT_GAMES
                else -> return FRAGMENT_GAMES
            }
        }
    }
    

    非常感谢。

1 个答案:

答案 0 :(得分:0)

事实证明,我的问题根本不是ViewPagers。我有4个片段,其中3个有一个同名的RecyclerView。这在页面之间移动太快时引起冲突(第一个片段被破坏,RecyclerView元素也被破坏。当发生这种情况时,它也会排除最后一个Fragment RecyclerView。)

将RecyclerView名称更改为唯一名称,问题是gonne