ViewPager2设置高度然后滑动时不起作用

时间:2019-11-25 11:45:56

标签: android android-viewpager2

Android Studio 3.6

我想使用 ViewPager2 滑动图像:

此处xml布局:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/waitressCallMainContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            android:title='@{@string/articles}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#bbccaa"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

此处适配器项的布局( article_item_tour.xml ):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/imageTourPageMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/tourArticleImageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的自定义适配器:

class MyAdapter : RecyclerView.Adapter<MyAdapter .ArticleViewHolder>() {
    var articleList: List<Article> = listOf()



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
        return ArticleViewHolder(parent)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(articleList[position], position)
    }

    fun setList(articleList: List<Article>) {
        this.articleList = articleList
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = articleList.size

    class MyViewHolderconstructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
        constructor(parent: ViewGroup) :
                this(
                    LayoutInflater.from(parent.context).inflate(
                        R.layout.article_item_tour,
                        parent,
                        false
                    )
                )

        fun bind(article: Article, number: Int) {
            Glide.with(itemView.tourArticleImageView.getContext())
                .load("http://www.gravatar.com/avatar/$number?s=200x200&d=identicon")
                .apply(RequestOptions().error(R.drawable.default_image))
                .into(itemView.tourArticleImageView)
        }
    }
}

结果如下:

enter image description here enter image description here

您可以看到ViewPager2成功刷卡了2张图像。很好。

但是我要设置viewpager的高度= 300 dp。我不需要在整个屏幕上显示图像。

因此,这里发生了变化:

 <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="0dp"
            android:layout_height="300dp"
            android:background="#bbccaa"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

但是现在图像未显示。仅显示ViewPager2(绿色背景)。结果没有滑动。

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

 <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#bbccaa"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

和article_item_tour_xml

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/imageTourPageMainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/tourArticleImageView"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果如下:

enter image description here