Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

android - 如何在RecyclerView中居中放置项目(How to center items in RecyclerView)

I want to have a horizontal RecyclerView that I can insert or remove items from. (我想要一个水平的RecyclerView,可以从中插入或删除项目。) The items should be aligned to the center when the RV width is smaller than the screen's. (当RV宽度小于屏幕宽度时,项目应与中心对齐。)

Note: I cannot use android:layout_width=wrap_content , as suggested in other questions, since a change in the RV size will cause misbehaving animations (ie items sliding in from "outside the RV" on item removal). (注意:我不能使用android:layout_width=wrap_content ,如其他问题所建议的那样,因为RV大小的更改会导致动画出现异常(即,在移除项目时从“ RV外部”滑入的项目)。)

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);        
recyclerView.setLayoutManager(layoutManager);

... (...)

Any ideas? (有任何想法吗?)

  ask by lea.cotan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let your RecyclerView width match_parent if nothing else is to share the space with it. (如果没有其他可共享的空间,则让您的RecyclerView宽度match_parent。)

Like below (像下面)

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:padding="10dp"
            android:layout_marginBottom="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

Then the item layout that you're using in the adapter can look like below (然后,您在适配器中使用的项目布局如下所示)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/ic_card1"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="200dp"> <!--this can be any size-->

    <TextView
        style="@style/regularText"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:text="@string/cardNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

The good thing is that your recycler view only render one itemlayout per column and so what we did is to extend the item layout to fill the whole column. (好消息是,您的回收站视图每列仅呈现一个itemlayout,因此我们要做的是扩展项目布局以填充整个列。)

ANY QUESTION? (任何问题?)

OPP!!! (OPP !!!) I think I miss translate your question!!!* (我想我想念您的问题!!! *)

NEW ANSWER JUST IF I ACTUALLY MISUNDERSTAND IN THE FIRST ONE (如果我确实误解了第一个答案)

I think you need a GridLayoutManager. (我认为您需要一个GridLayoutManager。)

Meanwhile, you need to know the amount of item that will be displayed in a row. (同时,您需要知道将连续显示的项目数量。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...