如何在Recyclerview中居中放置项目?

时间:2018-09-03 15:34:46

标签: android android-recyclerview

我正在计划一个带有recyclerviews的小应用程序。 有时我只需要加载4个项目,有时需要加载200多个项目。 现在我不明白如何将自定义的recyclerviews项居中!

我有一个自定义的recyclerview(网格),我希望其中有4个(2x2)物品居中。

我所拥有的:

enter image description here

我想要什么:

enter image description here

那么我在以下代码中的错误在哪里?

fragment_single_pokemon.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/cLayTypOne"
        android:layout_width="50dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/customborder_typ"
        app:layout_constraintBottom_toTopOf="@+id/cLayTypTwo"
        app:layout_constraintEnd_toStartOf="@+id/civSprite"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/txtTypOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:textSize="8sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <View
        android:id="@+id/vLine2"
        android:layout_width="50dp"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#000000"
        app:layout_constraintBottom_toBottomOf="@+id/civSprite"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/civSprite"
        app:layout_constraintTop_toTopOf="@+id/civSprite">

    </View>

    <TextView
        android:id="@+id/txtPokemonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Pokéball"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civSprite"
        android:layout_width="148dp"
        android:layout_height="148dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:src="@mipmap/ic_launcher"
        app:civ_border_width="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtPokemonName" />

    <View
        android:id="@+id/vLine"
        android:layout_width="50dp"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#000000"
        app:layout_constraintBottom_toBottomOf="@+id/civSprite"
        app:layout_constraintEnd_toStartOf="@+id/civSprite"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/civSprite">

    </View>

    <ImageView
        android:id="@+id/imgIsRaidBoss"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/vLine2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/civSprite"
        app:srcCompat="@drawable/raid_sprite" />

    <ImageView
        android:id="@+id/imgShinyExists"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/imgIsRaidBoss"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/civSprite"
        app:srcCompat="@drawable/ic_shiny" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cLayTypTwo"
        android:layout_width="50dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/customborder_typ"
        app:layout_constraintBottom_toTopOf="@+id/vLine"
        app:layout_constraintEnd_toStartOf="@+id/civSprite"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/txtTypTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:textSize="8sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <Button
        android:id="@+id/btnPokeDexEnty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:layout_marginStart="48dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/boarder_shape"
        android:text="Pokédex Eintrag"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/civSprite" />

    <TextView
        android:id="@+id/txtPokeDexEntry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textAlignment="center"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="@+id/btnPokeDexEnty"
        app:layout_constraintStart_toStartOf="@+id/btnPokeDexEnty"
        app:layout_constraintTop_toBottomOf="@+id/btnPokeDexEnty" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rViewPokeInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:foregroundGravity="center_vertical"
        android:orientation="vertical"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtPokeDexEntry" />


</android.support.constraint.ConstraintLayout>

gridview_pokedex_pokeinfo.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rLayMainContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtPokeInfoTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtPokeInfoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPokeInfoTitle"
        android:layout_centerHorizontal="true"
        android:text="TextView" />
</RelativeLayout>

recyclerview代码

RecyclerView recyclerViewPokeInfo = (RecyclerView)rLayout_mainFragment.findViewById(R.id.rViewPokeInfo);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
recyclerViewPokeInfo.setLayoutManager(layoutManager);

CustomRecyclerAdapterPokeInfoGrid adapter = new CustomRecyclerAdapterPokeInfoGrid(getActivity(), db.getPokeInfo(intPokemonID));
recyclerViewPokeInfo.setAdapter(adapter);

recyclerview适配器

package com.spicysoftware.goexpert;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomRecyclerAdapterPokeInfoGrid extends RecyclerView.Adapter<CustomRecyclerAdapterPokeInfoGrid.ViewHolder> {
    private final ArrayList<String> strPokeInfo;
    private Context context;
    MySQLiteHelper db;
    int locationSpriteNo;
    String strCatchRate = "";
    String strEscapeRate = "";
    String strInEggs = "";
    String strKmAsFriend = "";

    public CustomRecyclerAdapterPokeInfoGrid(Context context, ArrayList<String> strPokeInfo) {
        this.strPokeInfo = strPokeInfo;
        this.context = context;
    }


    @Override
    public CustomRecyclerAdapterPokeInfoGrid.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.gridview_pokedex_pokeinfo, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CustomRecyclerAdapterPokeInfoGrid.ViewHolder viewHolder, final int i) {


        viewHolder.setClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {

            }
        });

        // load image
        try {
            db = new MySQLiteHelper(context);

            String currentString = strPokeInfo.get(i);

            if(i == 0){
                viewHolder.txtPokeInfoTitle.setText("Fangchance");
            }else if(i == 1){
                viewHolder.txtPokeInfoTitle.setText("Fluchtchance");
            }else if(i == 2){
                viewHolder.txtPokeInfoTitle.setText("In Eiern");
            }else if(i == 3){
                viewHolder.txtPokeInfoTitle.setText("Distanz als Kumpel");
            }

            viewHolder.txtPokeInfoText.setText(currentString);


        }
        catch(Exception ex) {
            Log.v("Error: ", "-> "+ex);
        }

    }

    @Override
    public int getItemCount() {
        return strPokeInfo.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
        private TextView txtPokeInfoTitle,txtPokeInfoText;
        private ItemClickListener clickListener;
        private ConstraintLayout cLayoutSingleItem;

        public ViewHolder(View view) {
            super(view);

            txtPokeInfoTitle = (TextView)view.findViewById(R.id.txtPokeInfoTitle);
            txtPokeInfoText = (TextView)view.findViewById(R.id.txtPokeInfoText);


            cLayoutSingleItem = (ConstraintLayout)view.findViewById(R.id.cLayoutSingleItem);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);

        }

        public void setClickListener(ItemClickListener itemClickListener) {
            this.clickListener = itemClickListener;
        }

        @Override
        public void onClick(View view) {
            clickListener.onClick(view, getPosition(), false);
        }

        @Override
        public boolean onLongClick(View view) {
            clickListener.onClick(view, getPosition(), true);
            return true;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

像这样将列表项布局设置为match_parent:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rLayMainContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtPokeInfoTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtPokeInfoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPokeInfoTitle"
        android:layout_centerHorizontal="true"
        android:text="TextView" />
</RelativeLayout>

答案 1 :(得分:0)

尝试在textViews中设置layout_gravity,而不是relativeLayout。