RecyclerView中的Android中心项目

RecyclerView中的Android中心项目

问题描述:

我是android的新手。我不知道为什么我不能将我的物品放在RecyclerView中。

Im a newbie in android. Im not sure why I cant center my item in RecyclerView.

我想要的是如下图所示: -

What I want is like below image :-

下面是什么android渲染图像: -

What android render is like below image :-

有没有办法将RecyclerView中的项目推送到中心?所以它看起来像这样: -

Is there a way to push items in RecyclerView to center? So it will look like this :-

我还提供如下布局文件: -

I also provide the layout files as below :-

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/calendar_itemContainer">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="April"
        android:id="@+id/calendar_txtMonth"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#ff58636d"
        android:textSize="16sp"
        android:gravity="center|center_vertical|center_horizontal"
        android:paddingTop="8dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="21"
        android:id="@+id/calendar_txtDay"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/calendar_txtMonth"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#ff58636d"
        android:textSize="40sp"
        android:layout_marginTop="-10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="5dp"
        android:gravity="center|center_vertical|center_horizontal" />
</RelativeLayout>

fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/calendar_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal"
        android:background="#ff2c3e50" />
</RelativeLayout>

和java代码: -

and the java codes :-

CalendarAdapter mAdapter = new CalendarAdapter(mDataset);
mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter.setCalendarCallbacks(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
selectItem(mCurrentSelectedPosition);


我试图做我认为相同的事情当我遇到这篇文章时你会问的事情。这是我最终使用的解决方案。

I was trying to do what I think is the same thing you're asking when I came across this post. Here's the solution I ended up using.

创建自己的LinearLayoutManager子类并覆盖getPaddingLeft()和getPaddingRight(),如此。

Create your own subclass of LinearLayoutManager and override getPaddingLeft() and getPaddingRight() like so.

public class CustomLayoutManager extends LinearLayoutManager {
    private int mParentWidth;
    private int mItemWidth;

    public CustomLayoutManager(Context context, int parentWidth, int itemWidth) {
        super(context);
        mParentWidth = parentWidth;
        mItemWidth = itemWidth;
    }

    @Override
    public int getPaddingLeft() {
        return Math.round(mParentWidth / 2f - mItemWidth / 2f);
    }

    @Override
    public int getPaddingRight() {
        return getPaddingLeft();
    }
}

parentWidth 应该是 RecyclerView 的宽度, itemWidth 应该是每个子视图的宽度,两者都是像素。显然,这只有在您知道所有子视图宽度相同的情况下才有效。

parentWidth should be the width of the RecyclerView and itemWidth should be the width of each of your child views, both in pixels. Obviously, this only works if you know that all your child views will be the same width.

然后,只需将此布局管理器与 RecyclerView一起使用

Then, just use this layout manager with your RecyclerView