简体   繁体   English

卧式回收机查看中心的项目

[英]Horizontal RecyclerView items in center

I want to set Horizontal RecyclerView with 4 items in it. 我想设置带有4个项目的Horizo​​ntal RecyclerView。 Items should appear in center of the screen. 项目应出现在屏幕中央。 Each item has a fixed height and width. 每个项目都有固定的高度和宽度。 I want to place all items in center and make my RecyclerView match_parent. 我想将所有项目放在中间,并使我的RecyclerView match_parent。 What I achieved is using wrap_content to RecyclerView. 我实现的是对RecyclerView使用wrap_content。 But if give match_parent to recyclerView all items appear from left not in center. 但是,如果将match_parent提供给recyclerView,则所有项目均从左起不在中心位置显示。 But I want all items in center of the screen with match_parent recylcerView. 但我希望所有屏幕上的项目都具有match_parent recylcerView。

Here is what I am doing: 这是我在做什么:

<android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerDragDrop"
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:layout_gravity="bottom|center_horizontal">

Item: 项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llContainer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:gravity="bottom"
    android:visibility="visible">

    <ImageView
        android:id="@+id/imgContainer"
        android:layout_width="56dp"
        android:layout_height="90dp"
        android:layout_gravity="bottom"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/tube_delete"
        android:visibility="visible" />
</LinearLayout>

Java: Java:

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);

With above code all my items appear in middle of the screen because my recyclerview is in center and has a wrap_content property. 使用上面的代码,我的所有项目都出现在屏幕中间,因为我的recyclerview位于中间并且具有wrap_content属性。

But why I need my RecyclerView match_parent? 但是,为什么我需要我的RecyclerView match_parent?

I am using RecyclerView's Drag and Drop functionality by using ItemTouchHelper classes. 我通过使用ItemTouchHelper类使用RecyclerView的拖放功能。 Now, whenever I drag any item to end of recyclerview that item start disappering from screen or we can say item doesn't drag untill the screen width. 现在,每当我将任何项目拖到recyclerview的末尾时,该项目就会从屏幕上消失,或者我们可以说该项目直到屏幕宽度才开始拖移。 I want my items to drag until screen width or it should stop dragging whenever item reaches recyclerview's width. 我希望我的物品一直拖动到屏幕宽度为止,或者只要物品达到recyclerview的宽度,它就应该停止拖动。 If I give match parent to my recyclerview then drag works fine but it shows all items from left of the screen not in center and I want all my items to show in center of the screen. 如果我将匹配的父项提供给我的recyclerview,则拖动效果很好,但它显示了屏幕左侧的所有项目,但不在中心,并且我希望所有项目显示在屏幕的中心。

Please let me know if there is any solution available. 请让我知道是否有任何可用的解决方案。

Thanks! 谢谢!

As I am using drag and drop 当我使用拖放时

Try the column item fill the width of the column while centering everything inside: 尝试使列项目填充列的宽度,同时使所有内容居中:

    <?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>

it helps you 它可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM