繁体   English   中英

带有 snaphelper 的水平回收器视图,如何将第一个和最后一个元素居中?

[英]Horizontal recyclerview with snaphelper, how to center first and last element?

我已经制作了一些我在真实应用程序中尝试做的事情的样本,但我不知道如何解决我的问题。 当前的解决方案如下所示。

在此处输入图像描述

所以我有一个电影类别的垂直列表,在每个类型中,我都有一个水平的电影列表。 第一行,将 The Exorcist 显示为第一个元素,但它没有居中。 Action 中的第二行显示了当我向右滚动一点时它的外观。 最后一行显示了行尾的样子。 我希望第一行和最后一行在“选中”时也可以居中。

我的主要活动如下所示:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/moviesListRecyclerView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:listitem="@layout/movie_category_item_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

movie_category_item_view 如下所示:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/movieListLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/movieCategoryTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="40dp"
    android:textColor="@android:color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Horror" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/moviesRecyclerView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:orientation="horizontal"
    android:visibility="visible"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/movieCategoryTitle"
    tools:listitem="@layout/movie_horizontal_item_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

像这样的movie_horizontal_item_view:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/movieTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Horror" />

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/image"
    android:layout_width="237dp"
    android:layout_height="209dp"
    android:background="@android:color/black"
    android:src="@drawable/ic_launcher_foreground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/movieTitle"
    tools:src="@drawable/ic_launcher_foreground" />

</androidx.constraintlayout.widget.ConstraintLayout>

希望我的要求是有道理的,如果您想自己尝试一下,看看我的意思,可以在 github 上找到

使用carouselview.CarouselView

 <alirezat775.lib.carouselview.CarouselView
                android:id="@+id/carousel_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"/>

适配器

class MyAdapter(var ctx: Context) : CarouselAdapter() {

    private var vh: CarouselViewHolder? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarouselViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val v = inflater.inflate(R.layout.home_list_item, parent, false)
        vh = MyViewHolder(v)
        return vh as MyViewHolder
    }

    override fun onBindViewHolder(holder: CarouselViewHolder, position: Int) {

    }

    inner class MyViewHolder(itemView: View) : CarouselViewHolder(itemView) {

    }

}

活动

class MainActivity : AppCompatActivity() {

    var carousel: Carousel? = null
    val adapter = MyAdapter(this)

    override fun onCreate(savedInstanceState: Bundle?) {


        carousel = Carousel(this, carousel_view, adapter)
        carousel!!.setOrientation(CarouselView.HORIZONTAL, false)
        //  carousel!!.autoScroll(true, 5000, true)
        //    carousel!!.scaleView(true)

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM