问题描述:
我环顾四周,但与此主题相关的所有其他问题似乎只能解决滚动问题,而且我无法找到与点击标签相关的任何内容。< / em>的
PS:代码是用Kotlin编写的
从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
}
}
}
非常感谢。
答案 0 :(得分:0)
事实证明,我的问题根本不是ViewPagers。我有4个片段,其中3个有一个同名的RecyclerView。这在页面之间移动太快时引起冲突(第一个片段被破坏,RecyclerView元素也被破坏。当发生这种情况时,它也会排除最后一个Fragment RecyclerView。)
将RecyclerView名称更改为唯一名称,问题是gonne