Search code examples
androidandroid-fragmentsandroid-viewpagermvp

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


In my activity I am setting ViewPager,TabLayout and adding two fragment instance in a list. Then i am passing that list to ViewPagerAdapter .Responsibility of fragment is to fetch data from api call and show it in a list .

I am taking two instance of fragment because api returns two list of data that need to be show in tab fashion(One list in one tab and one in another). But when viewpager adapter returns fragment , if one data list is empty then I am getting empty screen in Tab-0 .

How to dynamically detect data size (here confused , because need to call fragment) and populate tab based on that.

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
    }
}

Fragment

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
    }
}

Solution

  • The best option is to fetch data in the activity and then show that tab layout with 2 tabs or just one tab. You would use it with a shared view model, but I see you don't use view models here. You can also just set the list in createInstance() method in Fragment when it isn't empty. The third option is to fetch data in Fragment and then send information to activity that the list is empty and hide the specific tab.