0

I am trying to make this type of layout using setSpanSizeLookup by GridLayoutManager.

Does anyone know how to autofit an item by calculating width of the item and auto adjust like image?

If there is some another way then please suggest me.

Here is some code:

int maxWidth = 1040; // device width
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        LayoutInflater vi = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.row_favorite, null);
        final TextView textView = (TextView) v.findViewById(R.id.tv_name);
        int itemWidth = (int) (textView.getPaint().measureText(favoriteList.get(position).getName()));

        if ((maxWidth / 2) / 3 > itemWidth) {
            return 1;
        } else if ((maxWidth / 2) / 3 < itemWidth && maxWidth / 3 > itemWidth) { 
            return 2;
        } else if (maxWidth / 2 > itemWidth && maxWidth / 3 < itemWidth) {
            return 3;
        }
    }
});

Please check image

Antti29
  • 2,953
  • 12
  • 34
  • 36
  • Check this [library](https://github.com/DavidPizarro/AutoLabelUI) – Burak Cakir Dec 05 '16 at 08:44
  • I want to show large amount of record so I prefer to use recyclerView, I have applied some library but they freez view and etc. I am near with above method but sometime calculation goes wrong. I think **setSpanSizeLookup** is proper way.. looking forward.. Thanks for suggest library @BurakCakir – Deep Bhavsar Jan 05 '17 at 12:22
  • If the view doesn't freeze in the method you are applying and only the problem is in the calculation, you can apply the above library's calculation to your own code. – Burak Cakir Jan 05 '17 at 12:50
  • 1
    now it is available.. may it will help us. https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html – Deep Bhavsar Feb 25 '17 at 12:54

2 Answers2

1

We have the option of FlexboxLayout now.

For layout,

<com .google.android.flexbox.flexboxlayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:flexwrap="wrap">

We have also layout manager for dynamic handle by recyclerView

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();

Enjoy..!!

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/17261781) – Antti29 Sep 07 '17 at 11:39
  • @Antti29 Thank you for guide me. everyone is new at some stage. Edited answer. – Deep Bhavsar Sep 07 '17 at 12:50
0

Add flexbox dependency

compile 'com.google.android:flexbox:0.3.1'

And set FlexboxLayoutManager

    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
    layoutManager.setFlexDirection(FlexDirection.ROW);
    layoutManager.setJustifyContent(JustifyContent.CENTER);
    recyclerview.setLayoutManager(layoutManager);
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159