当片段返回的数据为空时,如何在视图寻呼机中动态操作片段

How to manipulate fragment dynamically in view pager when data returned by fragment is empty

在我的 activity 中,我正在设置 ViewPagerTabLayout 并在列表中添加两个片段实例。然后我将该列表传递给 ViewPagerAdapter。片段的职责是从 api 调用中获取数据并将其显示在列表中。

我正在使用片段的两个实例,因为 api returns 需要以选项卡方式显示的两个数据列表(一个列表在一个选项卡中,一个列表在另一个选项卡中)。但是当 viewpager 适配器 returns 片段时,如果一个数据列表是空的,那么我在 Tab-0 中得到空屏幕。

如何动态检测数据大小(此处混淆,因为需要调用片段)并基于此填充选项卡。

ActivityOne.kt

class ActivityOne : BaseActivity() {

    lateinit var item: ArrayList<HistoryTabItem>
    lateinit var tabLayout: Tabs

    val InfoViewpagerAdapter:InfoVIewPagerAdapter by lazy { InfoVIewPagerAdapter(supportFragmentManager, ArrayList()) }

    fun newInstance(context: Context): Intent {
        return Intent(context, InfoFragment::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.info_tabview)
        getFragments()
        InfoViewpagerAdapter.arrayList = item

        tabLayout = findViewById(R.id.tabsnfo_type)
        val viewPager = findViewById<ViewPager>(R.id.view_pager_info_type)
        viewPager.adapter = InfoViewpagerAdapter
        tabLayout.setupWithViewPager(viewPager)

    }



    fun getFragments() {

        item = ArrayList()

        val HistoryTabItemSeller = HistoryTabItem()
        HistoryTabItemSeller.fragment = InfoFragment.createInstance()
        item.add(HistoryTabItemSeller)

      val HistoryTabItemBuyer = HistoryTabItem()
        HistoryTabItemBuyer.fragment = InfoFragment.createInstance()
        item.add(HistoryTabItemBuyer)

    }
}

InfoViewPageradapter

class InfoVIewPagerAdapter(fm: FragmentManager, var arrayList: ArrayList<HistoryTabItem>) : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        return arrayList[position].fragment
    }

    override fun getCount(): Int {
        return arrayList.size
    }
}

片段

class InfoFragment : BaseDaggerFragment(), InfoContract.View  {

    var isTickerShow: Boolean? = false
    var tickerMessage: String? = null
    lateinit var allTransactionList: ArrayList<Any>

    @Inject
    lateinit var infoPresenter: HoldInfoPresenter
    val infoAdapter: InfoAdapter by lazy { 
  InfoAdapter(ArrayList()) }
    lateinit var fakelist: ArrayList<Any>

    companion object {
        fun createInstance(): Fragment {
            return InfoFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_container_info, container, false)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        infoPresenter.attachView(this)
        infoPresenter.getInfo()
    }

    fun initView() {
        rv_container.layoutManager = LinearLayoutManager(context)
        rv_container.adapter = infoAdapter
    }

    override fun renderInfo(depositHistory: DepositHistory?) {
        var resultList = ArrayList<Any>()
        depositHistory?.let {
            resultList = combinedTransactionList(it.sellerData as ArrayList<SellerDataItem>, it.buyerData as ArrayList<BuyerDataItem>)
            isTickerShow = it.tickerMessageIsshow
            tickerMessage = it.tickerMessageId
        }


        infoAdapter.list.clear()
        infoAdapter.list.addAll(resultList)
        infoAdapter.notifyDataSetChanged()
    }

    fun combinedTransactionList(arrayList: ArrayList<SellerDataItem>, arrayList1: ArrayList<BuyerDataItem>): ArrayList<Any> {
        allTransactionList = ArrayList()
        allTransactionList.clear()
        allTransactionList.addAll(arrayList)
        allTransactionList.addAll(arrayList1)

        return allTransactionList
    }
}

最好的选择是在 activity 中获取数据,然后显示具有 2 个选项卡或仅一个选项卡的选项卡布局。您可以将它与共享视图模型一起使用,但我看到您在这里不使用视图模型。 当列表不为空时,您也可以在 Fragment 的 createInstance() 方法中设置列表。 第三种选择是在Fragment中获取数据,然后将信息发送给activity列表为空并隐藏特定选项卡。