这个问题已经问了很多,但是在每种解决方案中我都找不到实现它的好方法。
基本上我有我的 FragmentPagerAdaper
internal class FragmentPagerAdapter(fm: androidx.fragment.app.FragmentManager, private val mNumbOfTabs: Int) : androidx.fragment.app.FragmentStatePagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
return when (position) {
HOME -> HomeFragment()
SHOPPING_CART -> ShoppingCartFragment()
/*COLLECTION -> CollectionFragment()
USER_SETTINGS -> UserSettingsFragment()*/
else -> HomeFragment()
}
}
override fun getCount(): Int {
return mNumbOfTabs
}
companion object {
private const val HOME = 0
private const val SHOPPING_CART = 1
private val COLLECTION = 1
private val USER_SETTINGS = 2
}
}
和 ShoppingCartFragment
class ShoppingCartFragment : Fragment() {
private var inputFragmentView: View? = null
var products: ArrayList<Any> = ArrayList()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val frameLayout = FrameLayout(this.context!!)
populateViewForOrientation(inflater,frameLayout)
return frameLayout
}
private fun RecyclerAnimator(recyclerView: RecyclerView, adapter: ProductCartViewAdapter) {
val itemAnimator = DefaultItemAnimator()
itemAnimator.addDuration = 1000
itemAnimator.removeDuration = 1000
recyclerView.itemAnimator = itemAnimator
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(context)
val divider = requireContext().resources.getDimensionPixelSize(R.dimen.divider_size_default)
recyclerView.addItemDecoration(DividerItemDecorator(divider, SPACE_BOTTOM))//SPACE_LEFT or SPACE_TOP or SPACE_RIGHT or SPACE_BOTTOM concat
}
fun checkList(){
products.clear()
products = database.retrieveShoppingCartList()
val emptyImage = inputFragmentView!!.findViewById<ImageView>(R.id.emptyList)
val recyclerList = inputFragmentView!!.findViewById<RecyclerView>(R.id.shopping_rv)
if (products.isEmpty() || products.size == 0){
recyclerList.visibility = GONE
recyclerList.removeAllViews()
emptyImage.visibility = VISIBLE
} else{
recyclerList.visibility = VISIBLE
emptyImage.visibility = GONE
val adapter = ProductCartViewAdapter(products, context!!, this)
RecyclerAnimator(recyclerList, adapter)
}
}
private fun populateViewForOrientation(inflater: LayoutInflater, viewGroup: ViewGroup) {
viewGroup.removeAllViewsInLayout()
inputFragmentView = inflater.inflate(R.layout.fragment_shopping_cart, viewGroup)
checkList()
}
}
直到这里一切顺利。这就是应用程序的外观。
但是当我重新选择包含 ShoppingCartFragment 的标签时,我需要刷新列表。具体来说,请调用函数 FragmentPagerAdapter.checkList()。
但是每次我尝试从片段中调用该函数时,由于找不到片段上下文,我都会不断收到NullPointer。 在此:
产品= database.retrieveShoppingCartList()
这就是我在getInstance中使用同步器处理那些上下文的方式
SQLiteHandler
private var instance: SQLiteHandler? = null
@Synchronized
fun getInstance(ctx: Context): SQLiteHandler {
if (instance == null) {
instance = SQLiteHandler(ctx.applicationContext)
}
return instance!!
}
// Access property for Context
val Context.database: SQLiteHandler
get() = SQLiteHandler.getInstance(applicationContext)
val androidx.fragment.app.Fragment.database: SQLiteHandler
get() = SQLiteHandler.getInstance(activity!!.applicationContext)
或在重新选择选项卡时以任何方式重新创建viewpager的片段