2016-12-05 213 views
0

我想通过GridLayoutManager使用setSpanSizeLookup进行这种布局。 有人知道如何通过计算项目宽度自动调整项目并自动调整像图像? 如果还有其他方法,那么请给我建议。由于如何使用setSpanSizeLookup自动调整项目?

下面是一些代码:

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

检查图像

+0

检查该[库]( https://github.com/DavidPizarro/AutoLabelUI) –

+0

我想显示大量的记录,所以我更喜欢使用recyclerView,我已经应用了一些l但他们freez视图等。我接近上述方法,但有时计算出错。我认为** setSpanSizeLookup **是正确的方式..期待..谢谢你的建议库@BurakCakir –

+0

如果视图不冻结在你正在申请的方法,只有问题在计算中,你可以应用上述库的计算到你自己的代码。 –

回答

1

我们的FlexboxLayout的选择了。

对于布局,

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

我们还为动态的手柄布局管理器通过recyclerView

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(); 

享受.. !!

+0

尽管此链接可能回答此问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/17261781) – Antti29

+0

@ Antti29谢谢你的指导。每个人在某个阶段都是新的。编辑答案。 –

0

添加Flexbox的依赖

编译 'com.google.android:flexbox:0.3.1'

并设置FlexboxLayoutManager

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context); 
    layoutManager.setFlexDirection(FlexDirection.ROW); 
    layoutManager.setJustifyContent(JustifyContent.CENTER); 
    recyclerview.setLayoutManager(layoutManager);