RecyclerView 项目不在 GridLayoutManager 的中心

Posted

技术标签:

【中文标题】RecyclerView 项目不在 GridLayoutManager 的中心【英文标题】:RecyclerView items not in center with GridLayoutManager 【发布时间】:2021-05-17 11:29:32 【问题描述】:

我有一个RecyclerView 和一个GridLayoutManager,它可以加载这样的项目:

如您所见,还有一个余量。我没有向 recyclerview 添加任何类型的ItemDecoration,也没有在此处的任何布局中添加任何边距。

我已经试过了:

android:layout_
android:layout_gravity="center_horizontal"

并使用LinearSnapHelper,但他们都没有修复它。

项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_gravity="center_horizontal"
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout

        android:layout_
        android:layout_
        android:orientation="vertical"
        android:gravity="center_horizontal">


        <androidx.cardview.widget.CardView
            app:cardPreventCornerOverlap="true"
            app:cardElevation="@dimen/_5sdp"
            app:cardCornerRadius="@dimen/_3sdp"
            android:layout_
            android:layout_
            android:layout_marginTop="@dimen/_20sdp">
            <com.makeramen.roundedimageview.RoundedImageView
                android:id="@+id/choosestorycoverimage"
                android:layout_
                android:layout_
                app:riv_corner_radius="@dimen/_3sdp"
                android:scaleType="fitXY"
                android:src="@drawable/test"/>
            <com.makeramen.roundedimageview.RoundedImageView
                android:id="@+id/choosestorycoverimagelayer"
                android:layout_
                android:layout_
                app:riv_corner_radius="@dimen/_3sdp"
                android:visibility="gone"
                android:scaleType="fitXY"
                android:src="@drawable/choosen_story_layer"/>
        </androidx.cardview.widget.CardView>
        <TextView
            android:id="@+id/choosestorynametext"

            android:layout_marginTop="@dimen/_3sdp"
            android:layout_
            android:layout_
            android:textSize="@dimen/_15sdp"
            android:text="text"/>
    </LinearLayout>


</RelativeLayout>

活动布局:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_

    tools:context=".ChooseStoryActivity">



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/choosestoryrecyclerview"
        android:layout_alignParentTop="true"
        android:layout_
        android:layout_gravity="center_horizontal"

        android:layout_ />

</RelativeLayout>


recyclerview 适配器:

public class ChooseStoryRecyclerViewAdapter extends RecyclerView.Adapter<ChooseStoryRecyclerViewAdapter.ViewHolder> 
    private static final String TAG = "LibraryRecyclerViewAdap";
    private ArrayList<String> coverImageUrlList = new ArrayList<>();
    private ArrayList<String> nameTextList = new ArrayList<>();

    private ArrayList<String> shelfNameList = new ArrayList<>();
    private ArrayList<HashSet<String>> shelfStoryIDList = new ArrayList<>();

    private HashSet<String> shelfStoryIDSet = new HashSet<>();
    private Context context;

    public ChooseStoryRecyclerViewAdapter(ArrayList<String> coverImageUrlList, ArrayList<String> nameTextList,Context context) 
        this.coverImageUrlList = coverImageUrlList;
        this.nameTextList = nameTextList;
        this.context = context;
    

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_story_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    
    //***********the main method***********
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) 
        Glide.with(context)
                .asBitmap()
                .load(coverImageUrlList.get(position))
                .into(holder.coverImage);
        holder.coverImage.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(holder.coverImageLayer.getVisibility() == View.VISIBLE)
                    holder.coverImageLayer.setVisibility(View.GONE);
                
                else
                    holder.coverImageLayer.setVisibility(View.VISIBLE);
                
            
        );
        holder.nameText.setText(nameTextList.get(position));




    

    @Override
    public int getItemCount() 
        return coverImageUrlList.size();
    

    public class ViewHolder extends RecyclerView.ViewHolder
        RoundedImageView coverImage;
        TextView nameText;
        RoundedImageView coverImageLayer;

        public ViewHolder(@NonNull View itemView) 
            super(itemView);
            coverImage = itemView.findViewById(R.id.choosestorycoverimage);
            nameText =itemView.findViewById(R.id.choosestorynametext);
            coverImageLayer = itemView.findViewById(R.id.choosestorycoverimagelayer);
        
    



【问题讨论】:

【参考方案1】:

从你的 recyclerView 标签中移除这些不必要的属性:

android:layout_alignParentTop="true" android:layout_gravity="center_horizo​​ntal"

您应该更改 item 布局 的父 RelativeLayout 的 width 以使用 ma​​tch_parent 而不是固定宽度,然后使用GridLayoutManagerspanCount 属性,如果您想要更小的项目布局

如果您真的不需要任何自定义 GridLayoutManager,您还应该直接在您的 activity_main.xml 中添加 GridLayoutManager。 为此,如果您有 GridLayoutManager,请从 MainActivity.java 中删除,并将这两行添加到您的 recyclerView 标签中

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" app:spanCount="3"

【讨论】:

以上是关于RecyclerView 项目不在 GridLayoutManager 的中心的主要内容,如果未能解决你的问题,请参考以下文章

滚动时单击不在 CoordinatorLayout 中的 RecyclerView 上工作

Android:LoadStateAdapter 不在 recyclerview 网格布局中居中

多个 RecyclerView 不在同一个片段中工作

在 RecyclerView 中的单元格之间添加空间? [复制]

ImageView 不在设备中查看,但在设计预览中显示

如何从RecyclerView项中获取Value