我有一个应用程序,它是由Kotlin开发的。我的活动之一是使用带有viewpager的选项卡布局(例如发送广告接收)。在``发送''选项卡中包含A,B,C之类的数据。在接收标签1,2,3中。来自数据库的所有数据。我想用新数据(如D,E,F .. 4,5,6 .....)更新viewpaer / tabview。 我尝试了几种方法,但没有用。
注意:我不想用旧数据刷新TabLayout,因为它可以工作。我只需要如何绑定新数据。当我关闭应用程序并再次打开时,它将更新为新数据,但是如果没有关闭应用程序,它将无法正常工作。
private fun setupViewPager(viewPager: ViewPager, it: List<TransactionEntity>?, incoming: TransactionAdapterDirection) {
if (incoming.equals(OUTGOING)) {
adapter.addFragment(SendingFragment(it!!, appDatabase, OUTGOING, networkDefinitionProvider), "SEND")
adapter.notifyDataSetChanged() //KK
} else if (incoming.equals(INCOMING)) {
adapter.addFragment(ReceiveFragment(it!!, appDatabase, INCOMING, networkDefinitionProvider), "RECEIVE")
adapter.notifyDataSetChanged() //KK
}
viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
}
})
viewPager.adapter = adapter
}
inner class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence {
return mFragmentTitleList[position]
}
override fun getItemPosition(`object`: Any): Int {
return PagerAdapter.POSITION_NONE
}
}
更新:
override fun getItem(position: Int): Fragment {
if(this.count < 3) {
mFragmentList.add(ReceiveFragment(mfragmentIT, mfragmentAB, mfragmentINC, mfragmentnetwork))
mFragmentList.add(SendingFragment(mfragmentIT, mfragmentAB, mfragmentINC, mfragmentnetwork))
for (i in 1..3) {
println(i)
}
}
return mFragmentList.get(i)
}
更新2:
override fun getItem(position: Int): Fragment {
if (position == 0) {
val tab1 = this.addFragment(com.crypto.wallet.activities.ReceiveFragment(mfragmentIT,mfragmentAB,mfragmentINC,mfragmentnetwork), "RECEIVE")
return tab1 as Fragment
} else {
val tab2 = this.addFragment(com.crypto.wallet.activities.SendingFragment(mfragmentIT,mfragmentAB,mfragmentINC,mfragmentnetwork), "SEND")
return tab2 as Fragment
}
}