Centering Recyclerview Items with Flexboxlayoutmanager

Centering RecyclerView items with FlexboxLayoutManager

Try this

ACTIVITY CODE

public class TestActivity extends AppCompatActivity {

RecyclerView recyclerView;
ArrayList<String> arrayList = new ArrayList<>();
FlexboxAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

recyclerView = findViewById(R.id.recyclerView);
initArray();
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.CENTER);
layoutManager.setAlignItems(AlignItems.CENTER);
recyclerView.setLayoutManager(layoutManager);
adapter = new FlexboxAdapter(this, arrayList);
recyclerView.setAdapter(adapter);

}

private void initArray() {

arrayList.add("Nileshfgfdgfdgfdggfgfgfdgvcb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nileshfgfdgfdgfdggfgcvbcvb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nileshfgfdgfdgfdggfgfdgdfgcvb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nileshfgfdgfdgfdggfgcvb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nileshfgfdgfdgfdggfgdfgdfgcvb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nilesh");
arrayList.add("Nileshfgfdgfdgfdggfgdfgcvb");
arrayList.add("Nilesh");
arrayList.add("Nilesh");

}
}

ADAPTER CODE

public class FlexboxAdapter extends RecyclerView.Adapter<FlexboxAdapter.ViewHolder> {

Context context;
ArrayList<String> arrayList = new ArrayList<>();

public FlexboxAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}

@Override
public FlexboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(FlexboxAdapter.ViewHolder holder, int position) {

holder.title.setText(arrayList.get(position));

}

@Override
public int getItemCount() {
return arrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;

public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tvTitle);
}
}
}

CUSTOM LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="5dp"
android:text="Nilesh"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>

ACTIVITY LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".TestActivity">

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

</LinearLayout>

OUTPUT

enter image description here

How to center items of a recyclerview?

Achieved with:

recyclerView.adapter = MyAdapter()
val layoutManager = FlexboxLayoutManager(context).apply {
justifyContent = JustifyContent.CENTER
alignItems = AlignItems.CENTER
flexDirection = FlexDirection.ROW
flexWrap = FlexWrap.WRAP
}
recyclerView.layoutManager = layoutManager

You need to add FlexboxLayout to your gradle:

implementation 'com.google.android.flexbox:flexbox:3.0.0'

enter image description here

RecyclerView items with different heights with FlexboxLayoutManager

Try something like this as your RecyclerView layout manager:

recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

How to move items in a grid layout to center, if a row has less number of colums in android kotlin?

This might or might not be possible via GridLayoutManager, but there is no direct solution to get this using GridLayoutManager. I suggest you take a look at Flexbox layout manager here:

https://github.com/google/flexbox-layout

This is from Google and what you want can easily be done using it instead. You might need to play around with the properties a bit, but I think it can be done using the justifyContent property.

A rough code snippet would be something along the lines of

RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND);
recyclerView.setLayoutManager(layoutManager);


Related Topics



Leave a reply



Submit