Android 水平回收器在中心查看项目
我想设置水平回收视图,其中包含4个项目。项目应显示在屏幕中央。每个项目都有固定的高度和宽度。我想把所有的项目都放在中心,让我的RecyclerView与父母匹配。我实现的是使用wrap_内容来回收视图。但如果将match_parent指定给recyclerView,则所有项目将从左侧显示,而不是在中间。但我希望所有项目都在屏幕中央,并带有match_parent recylcerView 以下是我正在做的:Android 水平回收器在中心查看项目,android,android-recyclerview,drag-and-drop,center,itemtouchhelper,Android,Android Recyclerview,Drag And Drop,Center,Itemtouchhelper,我想设置水平回收视图,其中包含4个项目。项目应显示在屏幕中央。每个项目都有固定的高度和宽度。我想把所有的项目都放在中心,让我的RecyclerView与父母匹配。我实现的是使用wrap_内容来回收视图。但如果将match_parent指定给recyclerView,则所有项目将从左侧显示,而不是在中间。但我希望所有项目都在屏幕中央,并带有match_parent recylcerView 以下是我正在做的: <android.support.v7.widget.RecyclerView
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerDragDrop"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="bottom|center_horizontal">
在上面的代码中,我的所有项目都显示在屏幕中间,因为我的recyclerview位于中间,并且具有wrap_内容属性
但是为什么我需要我的父母
我通过使用ItemTouchHelper类使用RecyclerView的拖放功能。现在,每当我将任何项目拖动到recyclerview的末尾时,该项目就会从屏幕上消失,或者我们可以说该项目直到屏幕宽度为止都不会拖动。我希望我的项目拖动到屏幕宽度,或者当项目达到recyclerview的宽度时,它应该停止拖动。若我将match parent指定给我的recyclerview,那个么拖动可以正常工作,但它会显示屏幕左侧的所有项目,而不是显示在屏幕中心,我希望所有项目都显示在屏幕中心
请让我知道是否有任何可用的解决方案
谢谢
当我使用拖放时,尝试使用列项目填充列的宽度,同时将所有内容居中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="@+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/movie_column_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
它可以帮助您
android:gravity=“center”
访问我发布的我的项目xml文件中的回收服务。你能告诉我如何做到这一点吗?我没有正确地理解你,这就是为什么。就像我的xml中提到的图像视图文本视图一样,任何你想在重心对齐的东西,不要添加回收器视图重力,只要你可以添加scrol水平如果你将match_parent设置到你的项目布局文件中,它只会将整个屏幕填充到一个项目中。因此,项布局文件的父项不应是匹配的父项。
final ContainerItemAdapter mAdapter = new ContainerItemAdapter(MainActivity.this, containers, this, mLayoutManager);
ItemTouchHelper.Callback callback =
new EditItemTouchHelperCallback(mAdapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(mRecyclerContainer);
mAdapter.setOnItemClickListener(new ContainerItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
showToastLay(position);
}
});
mRecyclerContainer.setAdapter(mAdapter);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="@+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/movie_column_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>