How to Auto Fit Recyclerview Items to the Width of Screen Android

How to auto fit recyclerview items to the width of screen android?

This custom class will help you https://gist.github.com/heinrichreimer/39f9d2f9023a184d96f8

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class SpanningLinearLayoutManager extends LinearLayoutManager {

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

public SpanningLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}

public SpanningLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return spanLayoutSize(super.generateDefaultLayoutParams());
}

@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return spanLayoutSize(super.generateLayoutParams(c, attrs));
}

@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return spanLayoutSize(super.generateLayoutParams(lp));
}

@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return super.checkLayoutParams(lp);
}

private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
if(getOrientation() == HORIZONTAL){
layoutParams.width = (int) Math.round(getHorizontalSpace() / (double) getItemCount());
}
else if(getOrientation() == VERTICAL){
layoutParams.height = (int) Math.round(getVerticalSpace() / (double) getItemCount());
}
return layoutParams;
}

@Override
public boolean canScrollVertically() {
return false;
}
@Override
public boolean canScrollHorizontally() {
return false;
}

private int getHorizontalSpace() {
return getWidth() - getPaddingRight() - getPaddingLeft();
}

private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}

Set RecyclerView layoutManager to SpanningLinearLayoutManager to instead of LinearLayoutManager

How to fill item width children in reyclerview android

You can change the width of the RecyclerView item views in onCreateViewHolder(). The ViewGroup parent is the RecyclerView.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
val lp = view.layoutParams
// Change the width of the item view here
lp.width = parent.measuredWidth / 3 // width is 1/3 of the width of the RecyclerView
return ItemViewHolder(view)
}

How to set recycler view item width size base on screen size?

Pls Use ConstraintLayout inside recyclerview item .. Constraints are updated and Better than Relative and Linear Layouts.. Constraint Layout will automatically adjusts based on Screens...

GridLayoutManager - how to auto fit columns?

You can calculate available number of columns, given a desired column width, and load the image as calculated. Define a static funtion to calculate as:

public class Utility {
public static int calculateNoOfColumns(Context context, float columnWidthDp) { // For example columnWidthdp=180
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float screenWidthDp = displayMetrics.widthPixels / displayMetrics.density;
int noOfColumns = (int) (screenWidthDp / columnWidthDp + 0.5); // +0.5 for correct rounding to int.
return noOfColumns;
}
}

And then when using it in the activity or fragment you can do like this :

int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

............
mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);

Recyclerview item doesn't take all width of the screen while giving it match parent

In your ReviewsRvAdapter class, inside the onCreateViewHolder() method, you have this line of code:

View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_user_review, null);

Right now, you're passing null as the parent when inflating the view. This makes it impossible for the created view to "match parent" for the width.

Change that to this instead:

View layoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_user_review, parent, false);

Now you will have a non-null parent when you inflate the view, and so the width will be set correctly.



Related Topics



Leave a reply



Submit