How to automatically determine item fit count for RecyclerView grid

1958 views java
5

Is it possible to use a specified width (100dp) to equally space out items in a Grid (as many as possible) for a RecyclerView rather than using an exact number of columns? int numberOfColumns = 3;, int numberOfColumns = 4;, etc. will not do the job as this will lead to plenty of empty space being present on larger screens.

portrait orientation

enter image description here

landscape orientation

enter image description here

RecyclerView-related code in Fragment

                static final String[] frenchVowels = new String[]{
                    "a", "e", "i", "o", "u", "y"
                };

                RecyclerViewAdapter rvAdapterGL;
                final RecyclerView rvGL = viewHolder.itemView.findViewById(R.id.rv);
                int numberOfColumns = 2;
                rvGL.setLayoutManager(new GridLayoutManager(getActivity(), numberOfColumns));
                rvAdapterGL = new RecyclerViewAdapter(getActivity(), frenchVowels);
                rvGL.setAdapter(rvAdapterGL);

RecyclerView adapter class

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

    private String[] mData;
    private LayoutInflater mInflater;

    // data is passed into the constructor
    public RecyclerViewAdapter(Context context, String[] data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the cell layout from xml when needed
    @Override
    @NonNull
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.gridview_item, parent, false);
        return new RecyclerViewAdapter.ViewHolder(view);
    }

    // binds the data to the TextView in each cell
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        holder.myTextView.setText(mData[position]);
    }

    // total number of cells
    @Override
    public int getItemCount() {
        return mData.length;
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.item_gridview);
        }
    }
}

answered question

2 Answers

5

There are a few different ways to handle this. One thing you could do is get the screen width and divide it by your desired cell width to get the number of columns.

You could also use the Resources framework to set different column counts for different screen sizes. For example, you could have a res/values/integers.xml that sets the column count to 3, and also a res/values-sw400dp/integers.xml that sets the column count to 5, and so on.

res/values/integers.xml

<resources>
    <integer name=“column_count”>3</integer>
</resources>

res/values-sw400dp/integers.xml

<resources>
    <integer name=“column_count”>5</integer>
</resources>

Now you can replace

int numberOfColumns = 2;

with

int numberOfColumns = getResources().getInteger(R.integer.column_count);

And you can use any number of values-swXXXdp/integers.xml files with lots of different values for XXX to have lots of different column counts for lots of different screen sizes.

posted this
8

If you use GridView layout, this question is answered here.

Or, if you want to use GridLayoutManager, do it like this

if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
}
else{
     mRecycler.setLayoutManager(new GridLayoutManager(mContext, 4));
}

posted this

Have an answer?

JD

Please login first before posting an answer.