Android Centering Item in RecyclerView
2022-09-01 04:24:19
我是机器人新手。我不确定为什么我不能在RecreneckerView中居中放置我的项目。
我想要的是如下图所示:-
什么机器人渲染如下图所示:-
有没有办法将 RecyclerView 中的项目推送到中心?所以它看起来像这样:-
我还提供了如下布局文件:-
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代码:-
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);