2

Suppose I have one OR two items in RecyclerView . How could I display them in center RecyclerView must have width and height is match_parent

AFAIK recyclerView starts from top. How could I give gravity = center to its single item. I mean if only 1 item comes it should be in the center of screen. RecylerView doesn't have gravity attribute. Is there any other way round.

I can't set it's height to wrap_content. Because I also have to add a swipeRefreshLayout.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/MatchMatch"
    android:background="@color/theme_color_gift_finder">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/activity_main_swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_gift_menu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="@dimen/margin_pad_10" />

        </android.support.v4.widget.SwipeRefreshLayout>

        <LinearLayout
            android:id="@+id/ll_bottom"
            style="@style/LinearHorizontal"
            android:layout_marginBottom="@dimen/margin_pad_16"
            android:background="@android:color/transparent"
            android:gravity="center">

            <com.netsolace.efc.utility.customviews.widgets.CustomButton
                android:id="@+id/btn_go"
                android:layout_width="@dimen/circular_go_n_done_btn_size"
                android:layout_height="@dimen/circular_go_n_done_btn_size"
                android:background="@drawable/oval_generic"
                android:text="@string/go"
                android:textColor="@color/theme_color_gift_finder"
                android:textSize="@dimen/text_size_medium" />

        </LinearLayout>

    </LinearLayout>

    <com.netsolace.efc.utility.customviews.widgets.CustomTextView
        android:id="@+id/tv_gift_no_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="No Results"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_large"
        android:textStyle="bold"
        android:visibility="gone" />

</FrameLayout>
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

3 Answers3

2

If you consider of showing single list item in the centre then I would suggest to get another layout which has a layout_height and layout_width set to match_parent and then set the list item when your list contains only one item.

So inside your onCreateViewHolder of your adapter, do something like this

// Declare a constant named SINGLE_VIEW first, inside your Adapter
private final int SINGLE_VIEW = 1;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v;

    if (viewType == SINGLE_VIEW) {
        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_single, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;

    } else {
        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;

    }
}

Then in getItemViewType you need to return proper view.

@Override
public int getItemViewType(int position) {
    if(position == 0 && yourList.size() == 1) return SINGLE_VIEW;
    else return super.getItemViewType(position);
}

Update

If you need to put your RecyclerView in the centre of the screen then you need to design your layout like this

<?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_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

The important things to pick up from above code is setting the gravity of the parent RelativeLayout to centre and set the height of the RecyclerView to wrap_content.

Here's my test application screenshot

Here's my test application screenshot

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • I want to center the item shown in recyclerView .It may be 1,2 or 5. Depend on visible item shown in a device. I want to center them. – Zar E Ahmer Jun 15 '16 at 10:59
  • Reaz Murshed I mention that I have a SwipeRefresh above RecyclerView. It wouldn't work if I use height =wrap_content. – Zar E Ahmer Jun 15 '16 at 12:52
0

You can setup the adapter with multiple viewTypes and have a special type for a solo item, where its layout_height="match_parent". Or you can customize the single item in that case by adjusting its layout params, however you will need to ensure you reset them for other items (that get recycled).

straya
  • 5,002
  • 1
  • 28
  • 35
-2

Use following:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" /> 
</LinearLayout>