WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

RecyclerView Viewholder - How to set gravity after dynamically inflating size of layout?

enter image description here

I've created a RecyclerView adapter that sizes the viewholder based on 33% of the screen size.

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.viewholder_home_action, parent, false);
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        int width = (int) (parent.getWidth() * 0.33);
        layoutParams.width = width;
        layoutParams.height = width;
        itemView.setLayoutParams(layoutParams);
        return new ViewHolder(itemView);
    }

The problem is that there is alot of space on the bottom of the ViewHolders. My question is how do I set the Gravity of the ViewHolders to Center Vertically?

Steps I've tried:

Adding gravity to the RecyclerView

This is the layout for the Fragment containing the Recycler:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/darkThemeBgrColor">

    <ImageView
        android:id="@+id/iv_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.70"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_gravity="center_vertical" <------- Does not work 
        android:layout_height="0dp"
        android:paddingStart="3dp"
        android:paddingEnd="3dp"
        android:background="@color/white"
        android:clipToPadding="false"
        app:layout_constraintTop_toBottomOf="@+id/guideline_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Adding Gravity to the ViewHolder:

My ViewHolder uses a ViewGroup of ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="3dp"
    android:layout_marginEnd="3dp"
    android:layout_gravity="center_vertical"> <------ Does not work

enter image description here

This only displays the gravity working in the xml preview, but unfortunately does nothing to the viewholder.

Another thing I've tried:

Casting the LayoutParams to a Constraint Layout

ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) itemView.getLayoutParams();

but it doesn't have any methods to set the Gravity:

enter image description here

Fourth thing that I've tried:

Setting the Gravity of the ItemView:

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.viewholder_home_action, parent, false);
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        int width = (int) (parent.getWidth() * 0.33);
        layoutParams.width = width;
        layoutParams.height = width;
        itemView.setLayoutParams(layoutParams);
        //Added one extra line of code here to set the item view gravity:
 
itemView.setForegroundGravity(Gravity.CENTER_VERTICAL);
        return new ViewHolder(itemView);
    }

Also unfortunately does nothing.


A bit lost on what to do. Does anyone have an idea of how would I add gravity to my viewholders programmatically so that they're centered vertically?

Upvotes: 2

Views: 737

Answers (2)

GHH
GHH

Reputation: 2019

You need to add constrain to childView and make ViewHolder match_parent

Adapter

class TestAdapter: RecyclerView.Adapter<TestViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        val viewBinding = ItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val view = viewBinding.root
        val layoutParams: ViewGroup.LayoutParams = view.layoutParams
        val width = (parent.width * 0.33).toInt()
        layoutParams.width = width
        view.layoutParams = layoutParams

        return TestViewHolder(viewBinding, parent.context)
    }

    override fun onBindViewHolder(holder: TestViewHolder, position: Int) {

        holder.setData()
    }

    override fun getItemCount(): Int {
        return 10
    }
}

ViewHolder

class TestViewHolder(private val viewBinding: ItemBinding,
                     private val context: Context) : RecyclerView.ViewHolder(viewBinding.root) {
    fun setData() {
        val width = (context.resources.displayMetrics.widthPixels * 0.33).toInt()
        val layoutParams = viewBinding.cardView.layoutParams
        layoutParams.width = width
        layoutParams.height = width
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:padding="4dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher" />
    </androidx.cardview.widget.CardView>

...
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Hope this helps

Upvotes: 2

snachmsm
snachmsm

Reputation: 19253

you can't set gravity for RecyclerView, because it isn't needed, not in this case. your RecyclerView shoud have height set to wrap_content and no app:layout_constraintTop_toBottomOf="@+id/guideline_horizontal" attribute. keep app:layout_constraintBottom_toBottomOf="parent" for aligning to parents bottom as you wish and wrap_content will make RecyclerView draw its childerns without any empty space on bottom

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="3dp"
    android:paddingEnd="3dp"
    android:background="@color/white"
    android:clipToPadding="false"
    app:layout_constraintBottom_toBottomOf="parent"/>

Upvotes: 0

Related Questions