Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView grid layout manager doesn't center items

enter image description here

I inherited some big project with a lot of legacy code and now I'm facing some weird stuff..

I need to make this screen have recyclerview with grid layout manager, 2 columns. This is what I get. Is there a way to center those icons in the middle of the screen? I tried with gravity, but nothing works. Maybe there is some thing inside all that legacy code that is making problem or this is just recyclerView's issue?

This is the item's layout (terrible, don't ask..)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_view_controller_item_background"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewSceneKK"
        android:layout_width="match_parent"
        android:layout_height="@dimen/room_button_height"
        android:layout_gravity="center"
        android:layout_marginLeft="@dimen/row_filter_text_margin_left"
        android:layout_marginRight="@dimen/row_filter_text_margin_left"
        android:gravity="center"
        android:shadowDx="-1"
        android:shadowDy="-1"
        android:shadowRadius="1"
        android:textSize="@dimen/row_scene_kk_text_size" />

    <TextView
        android:id="@+id/textViewSceneName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/row_filter_text_margin_bottom"
        android:layout_marginLeft="@dimen/row_filter_text_margin_left"
        android:layout_marginRight="@dimen/row_filter_text_margin_left"
        android:layout_marginTop="@dimen/row_filter_text_margin_top"
        android:clickable="false"
        android:gravity="center"
        android:longClickable="false"
        android:textColor="@color/main_text_color"
        android:textSize="@dimen/row_browser_right_name_text_size" />
</LinearLayout>

<!--<View-->
<!--android:id="@+id/filterView"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:clickable="false"-->
<!--android:longClickable="false" />-->

<View
    android:id="@+id/filterViewClick"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:selectableItemBackground"
    android:focusable="false"
    android:focusableInTouchMode="false" />

And fragment't layout:

<customview.CustomRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />

And the code:

 customRecyclerView.setHasFixedSize(false);
    customRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
    customRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
            R.drawable.line_separator_empty, DividerItemDecoration.VERTICAL_LIST));

customRecyclerView.setAdapter(adapter);

CustomRecyclerView.java

public class CustomRecyclerView extends RecyclerView {

    private boolean enableScroll = true;

    public CustomRecyclerView(Context context) {
        super(context);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public boolean isEnableScroll() {
        return enableScroll;
    }

    public void setEnableScroll(boolean enableScroll) {
        this.enableScroll = enableScroll;
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (enableScroll) {
            return super.onInterceptTouchEvent(e);
        }
        return false;
    }
}
like image 950
joe Avatar asked May 09 '17 10:05

joe


People also ask

How do I center items in a RecyclerView?

How do I center items in a RecyclerView? Take LinearLayout in your RecyclerView 's item row layout then give android:layout_gravity="center" to LinearLayout . For each row of images you have to take different LinearLayout . Save this answer.

What is GridLayout manager?

Android GridLayoutManager is the RecyclerView. LayoutManager implementation to lay out items in a grid. In this tutorial, we'll create an application that displays CardViews inside a RecyclerView in the form of a GridLayout.

What are types of layout manager for RecyclerView?

RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.


1 Answers

try this: it's working for me

<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                >

</androidx.recyclerview.widget.RecyclerView>
like image 183
Mohamed Wardan Avatar answered Oct 02 '22 17:10

Mohamed Wardan