如何在片段返回的数据为空时动态操作视图分页中的片段。

huangapple go评论69阅读模式
英文:

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

问题

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

我取两个片段实例是因为API返回两个数据列表,需要以选项卡方式显示(一个列表在一个选项卡中,另一个在另一个选项卡中)。但是当ViewPager适配器返回片段时,如果一个数据列表为空,则在选项卡-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
    }
}

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
    }
}
英文:

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&lt;HistoryTabItem&gt;
    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&lt;ViewPager&gt;(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&lt;HistoryTabItem&gt;) : 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&lt;Any&gt;

    @Inject
    lateinit var infoPresenter: HoldInfoPresenter
    val infoAdapter: InfoAdapter by lazy { 
  InfoAdapter(ArrayList()) }
    lateinit var fakelist: ArrayList&lt;Any&gt;

    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&lt;Any&gt;()
        depositHistory?.let {
            resultList = combinedTransactionList(it.sellerData as ArrayList&lt;SellerDataItem&gt;, it.buyerData as ArrayList&lt;BuyerDataItem&gt;)
            isTickerShow = it.tickerMessageIsshow
            tickerMessage = it.tickerMessageId
        }

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

    fun combinedTransactionList(arrayList: ArrayList&lt;SellerDataItem&gt;, arrayList1: ArrayList&lt;BuyerDataItem&gt;): ArrayList&lt;Any&gt; {
        allTransactionList = ArrayList()
        allTransactionList.clear()
        allTransactionList.addAll(arrayList)
        allTransactionList.addAll(arrayList1)

        return allTransactionList
    }
}

答案1

得分: 1

最好的选择是在活动中获取数据,然后显示带有2个选项卡或只有一个选项卡的标签布局。您可以使用共享视图模型,但我看到您在这里没有使用视图模型。
您还可以在片段的createInstance()方法中设置列表,当它不为空时。
第三个选项是在片段中获取数据,然后向活动发送列表为空的信息,并隐藏特定的选项卡。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月7日 02:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定