简体   繁体   English

使用LinearLayoutManager集中化RecyclerView

[英]Centralizing RecyclerView with LinearLayoutManager

I am trying to centralize a RecyclerView list in the screen. 我正在尝试将RecyclerView列表集中在屏幕中。 Its going to be a single list, so I will use the LinearLayoutManager (LLM). 它将是一个列表,因此我将使用LinearLayoutManager (LLM)。 However using the LLM its not centralizing the items, and it looks like this on the landscape mode: 但是,使用LLM不会将项目集中,并且在横向模式下看起来像这样: LLM横向模式

The XML code looks like this: XML代码如下所示:

<FrameLayout 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:orientation="vertical" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/recipe_card"
        android:layout_gravity="center"
        android:layout_width="400dp"
        android:layout_height="186dp"
        style="@style/CardViewStyle">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <ImageView
                android:id="@+id/recipe_image"
                android:layout_width="120dp"
                android:layout_height="163dp"
                app:srcCompat="@mipmap/ic_pan"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                tools:layout_editor_absoluteY="3dp" />

            <TextView
                android:id="@+id/recipe_name"
                android:layout_width="250dp"
                android:layout_height="119dp"
                android:text="TextView"
                app:layout_constraintLeft_toRightOf="@+id/recipe_image"
                android:layout_marginLeft="8dp"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="16dp" />

            <TextView
                android:id="@+id/recipe_servings"
                android:layout_width="164dp"
                android:layout_height="32dp"
                android:text="TextView"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/recipe_name"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                app:layout_constraintLeft_toRightOf="@+id/recipe_image"
                android:layout_marginLeft="94dp"
                app:layout_constraintVertical_bias="0.0" />
        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

On the Android Studio's Preview, it looks fine, but on the app, it does not. 在Android Studio的“预览”上,它看起来不错,但在应用程序上,它看起来并不理想。 I manage to fix it by changing the LLM for a GridLayoutManager with only one column. 我设法通过仅更改一列的GridLayoutManager的LLM来解决它。 But it looks ugly. 但是看起来很丑。

Does anyone knows what is going on? 有人知道发生了什么吗? Thanks. 谢谢。

Try to set the content gravity of main layout to center. 尝试将主布局的内容重心设置为居中。 There are two ways to do that: 有两种方法可以做到这一点:

  1. Add "centerHorozontal" property to "true" to your frame layout. 将“ centerHorozontal”属性添加为“ true”到您的框架布局。
  2. Change your frame layout to linear layout and set gravity of main linear layout to "center". 将框架布局更改为线性布局,并将主线性布局的重力设置为“中心”。

Hope it will work, if it doesn't then just send me the images you are using in your layout, I'll try to fix it by using some other techniques and will send you the updated code. 希望它能工作,如果不能,那么只给我发送您在布局中使用的图像,我将尝试使用其他技术来修复它,并向您发送更新的代码。

Replace constraint layout with Relative layout 用相对布局替换约束布局

Then, make the image be central, use this 然后,使图像居中,使用此

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:clickable="true"
        android:scaleType="centerCrop" />

Then also make each of those texts be central 然后还要使每个文本成为中心

android:textAlignment="center"

Please check your public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) , if you write like that: 如果您这样写,请检查您的public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, null));
}

this will make your FrameLayout disabled and CardView will be the itemView of your ViewHolder, So you should change as follow: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } 这将使您的FrameLayout禁用,而CardView将成为ViewHolder的itemView,因此,您应该进行如下更改: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); } Hope Help you. @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); }希望帮助你。

if I were you I would get my display size width and set my ViewHolder some margin accordingly. 如果我是你,我将获得显示尺寸的宽度,并相应地为ViewHolder设置一些边距。

this is my code in my application class: 这是我的应用程序类中的代码:

public class MyApp extends Application {
public static Point displaySize = new Point();
public static float density = 1;
public static Context appContext = null;
public static int height;

@Override
public void onCreate() {
    super.onCreate();
    ActiveAndroid.initialize(this);
    appContext = getApplicationContext();
    checkDisplaySize();
    density = appContext.getResources().getDisplayMetrics().density;
}

public static void checkDisplaySize() {
    try {
        WindowManager manager = (WindowManager)  
appContext.getSystemService(Context.WINDOW_SERVICE);
        if (manager != null) {
            Display display = manager.getDefaultDisplay();
            if (display != null) {
                    display.getSize(displaySize);
            }
           height = (int) (displaySize.x / 1.777f); //Aspect Ratio 16:9
        }
    } catch (Exception e) {
    }
}
}

and after all this in your adapter where you get the holder view just do the following: 并在适配器中获得“持有者”视图的所有这些之后,只需执行以下操作:

int marginWidth = (MyApp.desplaySize.x - itemView.getMeasuredWidth())/2;
LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,      
    LayoutParams.WRAP_CONTENT
);
params.setMargins(marginWidth, 0, marginWidth, 0);
itemView.setLayoutParams(params);

I am not really sure that this exact code will solve your issue but you could easily change this so it will. 我不太确定这个确切的代码是否可以解决您的问题,但是您可以轻松地对此进行更改。

在您的FrameLayout中编写此代码

    android:layout_gravity="center"

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

相关问题 未解决的参考:LinearLayoutManager/RecyclerView - Unresolved reference: LinearLayoutManager/RecyclerView 使用 LinearLayoutManager 在 RecyclerView 中滚动到顶部 - Scroll to top in RecyclerView with LinearLayoutManager 带有GridLayoutManager的RecyclerView和带有LinearLayoutManager的RecyclerView - RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager xamarin RecyclerView LinearLayoutManager StackFromEnd丢失 - xamarin RecyclerView LinearLayoutManager StackFromEnd missing RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working RecyclerView 将 MaterialToggleButtonGroup 添加到父 LinearLayoutManager - RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager ItemToucherHelper 不使用垂直 LinearLayoutManager 在 recyclerView 中滑动 - ItemToucherHelper not swipe in recyclerView with vertical LinearLayoutManager RecyclerView LinearLayoutManager设置项目数 - RecyclerView LinearLayoutManager set item count 一个 LinearLayoutManager 只能用于一个 RecyclerView? - One LinearLayoutManager is only for one RecyclerView? IllegalArgumentException:LinearLayoutManager已附加到RecyclerView - IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM