视图未显示在ViewPager

时间:2017-06-05 20:53:25

标签: android kotlin

我有一个简单的ViewPager支持PagerAdapter,但是我实例化的视图永远不会显示在视图寻呼机中。

我要求纯粹在代码中开发UI。所以我不能在instantiateItem方法中扩展xml布局。不幸的是,几乎所有我见过的例子都在膨胀xml,这显然不起作用。

所以我有一个PagerAdapter,(我不想使用片段)和ViewPager,如下所示。适配器需要一些数据

val viewAdapter = ViewPagerAdapter(data)
viewPager.adapter = viewAdapter

ViewPagerAdapter如下所示:

inner class ViewPagerAdapter(val data: List<Item>) : PagerAdapter() {
    override fun isViewFromObject(view: View?, obj: Any?): Boolean {
        return obj == view
    }

    override fun getCount(): Int {
        return data.count()
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        val rootLayout = LinearLayout(container?.context)
        rootLayout.layoutParams = rootLayout.setDimensions(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        rootLayout.orientation = LinearLayout.HORIZONTAL

        val title = Button(container?.context)
        title.text = "Title"
        title.textSize = 13f
        title.setTextColor(Color.WHITE)
        rootLayout.addView(title)

        val subtitle = Button(container?.context)
        subtitle.text = "Subtitle"
        subtitle.textSize = 13f
        subtitle.setTextColor(Color.WHITE)
        rootLayout.addView(subtitle)

        (container as ViewPager).addView(rootLayout)
        return rootLayout
    }

    override fun getPageTitle(position: Int): CharSequence {
        return data[position].title
    }

    override fun destroyItem(container: ViewGroup?, position: Int, obj: Any?) {
        if (obj == null) return
           container?.removeView(obj as View?)
        }
    }
}

对于我的生活,我无法看清这段代码的错误。我尝试过将布局,textview更改为按钮,但没有任何效果。

我希望有人能指出我正确的方向吗?

谢谢!

更新:

我创建了一个简单的应用来证明我没有做任何愚蠢的事情。如果你已经添加了相关的库,那么应该可以复制并粘贴它并运行它。

import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.TabLayout
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val rootLayout = CoordinatorLayout(this)
    rootLayout.layoutParams = CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT)

    val appBarLayout = AppBarLayout(this)
    rootLayout.addView(appBarLayout)
    appBarLayout.layoutParams = CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT)

    val tabLayout = TabLayout(this)

    tabLayout.tabMode = TabLayout.MODE_FIXED
    tabLayout.tabGravity = TabLayout.GRAVITY_FILL
    appBarLayout.addView(tabLayout)
    tabLayout.layoutParams = AppBarLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)

    //add viewpager
    val viewPager = ViewPager(this)
    viewPager.id = 11122

    val viewAdapter = ViewPagerAdapter()
    viewPager.adapter = viewAdapter

    rootLayout.addView(viewPager)
    viewPager.layoutParams = CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT)
    tabLayout.setupWithViewPager(viewPager)

    rootLayout.setBackgroundColor(Color.BLACK)
    setContentView(rootLayout)
}


inner class ViewPagerAdapter : PagerAdapter() {

    override fun isViewFromObject(view: View?, obj: Any?): Boolean {
        return obj == view
    }

    override fun getCount(): Int {
        return 3
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        val rootLayout = LinearLayout(container?.context)
        rootLayout.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        rootLayout.orientation = LinearLayout.HORIZONTAL

        val title = Button(container?.context)
        title.text = "Title"
        title.textSize = 13f
        title.setTextColor(Color.WHITE)
        rootLayout.addView(title)

        val subtitle = Button(container?.context)
        subtitle.text = "Subtitle"
        subtitle.textSize = 13f
        subtitle.setTextColor(Color.WHITE)
        rootLayout.addView(subtitle)

        val viewPager = container as ViewPager
        viewPager.addView(rootLayout, 0)
        return rootLayout
    }

    override fun getPageTitle(position: Int): CharSequence {
        return "Title: $position"
    }

    override fun destroyItem(container: ViewGroup?, position: Int, obj: Any?) {
        if (obj == null) return
        when (obj) {
            is ArrayList<*> -> for (item in obj) {
                container?.removeView(item as View?)
            }
            else -> container?.removeView(obj as View?)
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

查看完整的活动代码。您似乎在视图寻呼机上设置了CoordinatorLayout布局参数,但是您忘记设置行为,看到层次结构中也有AppBarLayout。因此,视图寻呼机内容正在标签布局后面绘制。您可以使用AS中的布局检查器验证这一点。

执行以下操作可以解决您的问题

val pagerParams = CoordinatorLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
pagerParams.behavior = AppBarLayout.ScrollingViewBehavior()
viewPager.layoutParams = pagerParams

您还可以利用Kotlin的apply功能

viewPager.layoutParams = CoordinatorLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT).apply {
    behavior = AppBarLayout.ScrollingViewBehavior()
}

希望有所帮助