简体   繁体   English

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

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

I've made a little sample of something I'm trying to do in a real app, and I can't figure out how to solve my issue.我已经制作了一些我在真实应用程序中尝试做的事情的样本,但我不知道如何解决我的问题。 The current solution looks like this.当前的解决方案如下所示。

在此处输入图像描述

So I have a vertical list of movie categories, where within each genre, I have a horizontal list of movies.所以我有一个电影类别的垂直列表,在每个类型中,我都有一个水平的电影列表。 The first row, is showing The Exorcist as the first element, but it's not centered.第一行,将 The Exorcist 显示为第一个元素,但它没有居中。 The second row in Action, shows how it looks when I've scrolled a bit to the right. Action 中的第二行显示了当我向右滚动一点时它的外观。 Last row is showing how the end of the row looks like.最后一行显示了行尾的样子。 I'd like to have the first and last of the rows, to be centered as well when they're "selected".我希望第一行和最后一行在“选中”时也可以居中。

My main activity looks like this:我的主要活动如下所示:

<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>

The movie_category_item_view looks like this: 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>

And the movie_horizontal_item_view like this:像这样的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>

Hopefully what I'm asking for makes sense, In case you want to try it out for yourself and see what I mean, it can be found on github here希望我的要求是有道理的,如果您想自己尝试一下,看看我的意思,可以在 github 上找到

use carouselview.CarouselView使用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"/>

the adapter适配器

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) {

    }

}

the activity活动

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