自定义ViewPager不会出现

时间:2019-02-24 04:50:59

标签: android kotlin android-viewpager android-custom-view

我试图定义一个扩展ViewPager的自定义视图。
首先,我定义了2个xmls:
view_media_slider_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:orientation="vertical"
        android:background="@color/colorAccent"
        android:gravity="center">

    <ImageView
            android:id="@+id/image"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/science"/>

    <TextView
            android:padding="16dp"
            android:id="@+id/text"
            android:textStyle="bold"
            android:textSize="30sp"
            android:text="Title Here"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

view_media_slider.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </androidx.viewpager.widget.ViewPager>

</LinearLayout>

然后,我定义了一个扩展PagerAdapter的适配器,并且还对适配器中的某些数据进行了硬编码。

class MediaSliderViewAdapter: PagerAdapter() {

    private val TAG = MediaSliderViewAdapter::class.simpleName

    private var mediaList = listOf(
        Pair(1, "ha"),
        Pair(2, "hsadfa"),
        Pair(1, "hasad"),
        Pair(1, "hasdaf"),
        Pair(1, "haqw12"),
        Pair(1, "hadsf"),
        Pair(1, "ha23"),
        Pair(1, "hadsaxc")
    )

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        Log.d(TAG, mediaList.size.toString())
        return mediaList.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val inflater = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) as LayoutInflater
        val rootView = inflater.inflate(R.layout.view_media_slider_item, container, false)
        rootView.findViewById<ImageView>(R.id.image).setImageResource(R.drawable.science)
        rootView.findViewById<TextView>(R.id.text).text = mediaList[position].second
        container.addView(rootView)
        return rootView
    }

    fun setMediaData(mediaData: List<Pair<Int, String>>) {
        mediaList = mediaData
        notifyDataSetChanged()
    }

这是我要构建的自定义视图,名为MediaSliderView

class MediaSliderView : ViewPager {
    @JvmOverloads
    constructor(
        context: Context,
        attrs: AttributeSet? = null
    ) : super(context, attrs)

    private val viewPager: ViewPager
    private val mediaSliderViewAdapter = MediaSliderViewAdapter()

    init {
        View.inflate(context, R.layout.view_media_slider, this) // inflate first before set below attrs, otherwise NPE
        viewPager = findViewById(R.id.viewPager)
        viewPager.adapter = mediaSliderViewAdapter
    }

    fun setMedia(urls: List<Pair<Int, String>>) {
        mediaSliderViewAdapter.setMediaData(urls)
    }
}

最后,我将此自定义视图放在名为view_search.xml的片段中,由于某种原因,视图分页器不显示任何数据。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerHorizontal="true">

    <io.keyu.urekalite.view.MediaSliderView
            android:background="@color/colorPrimary"
            android:layout_width="500dp"
            android:layout_height="500dp">

    </io.keyu.urekalite.view.MediaSliderView>

</LinearLayout>

数据已经在适配器中初始化,所以我认为它至少应该显示一些东西,但事实并非如此。

1 个答案:

答案 0 :(得分:0)

您可以为view_media_slider_item.xml创建一个类片段 并使用FragmentPagerAdapter代替PagerAdapter

viewPager.setAdapter(new FragmentPagerAdapter() {
    @Override
    Fragment getItem(position: Int) {
        return //Fragment class of view_media_slider_item
    }

    @Override
    int getCount() {
       return 1
    }
})