自定义流式布局

之所以写流布局,是因为在做一个点菜APP时,遇到一个选择退菜原因时,原因长短不一,我原本是LinearLayout套LinearLayout,做出来的,经常会出现,内容出现内容显示不下的问题,不得不使用自定义GroupView.以前看见过网上的案例。
自定义流式布局主要有两点一、测量(onMeasure)二放置(onLayout)
先说下测量:
测量模式:包括UNSPECIFIED 、EXACTLY、AT_MOST
UNSPECIFIED:不对View大小进行限制,想要多大要多大
EXACTLY:精确测量,大小父布局已经测量好,mactch_parent或精确数字
AT_MOST:最大不能超过SpecSize,wrap_content
首先测量它的子View,测量子View后,根据测量模式算出大小
放置(onLayout)
放置,也就是根据自己的需求,放置子View
网上的代码很多,有一个关键点LayoutParams
/**
* 与当前ViewGroup对应的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// TODO Auto-generated method stub
return new MarginLayoutParams(getContext(), attrs);
}
在这个Group添加View时 ,一定要设置LayoutParams,否则会崩溃(原因是子元素没有LayoutParams,而测量和放置又都用到了他们
tv.setText(titles[i]);
tv.setLayoutParams(new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
flowlayout.addView(tv);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecyclerView本身并不支持布局,但是可以通过自定义LayoutManager来实现布局。下面是一个简单的实现步骤: 1. 创建一个继承自RecyclerView.LayoutManager的自定义LayoutManager。 2. 在自定义LayoutManager中重写onLayoutChildren方法,实现布局的摆放。 3. 在onMeasure方法中计算RecyclerView的宽高。 4. 在RecyclerView.Adapter中根据需要调整item的宽高,以适配布局。 以下是一个简单的示例代码: ```java public class FlowLayoutManager extends RecyclerView.LayoutManager { private int mTotalHeight; @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams( RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { if (state.getItemCount() == 0) { return; } detachAndScrapAttachedViews(recycler); int offsetX = getPaddingLeft(); int offsetY = getPaddingTop(); int lineMaxHeight = 0; int width = getWidth(); for (int i = 0; i < getItemCount(); i++) { View child = recycler.getViewForPosition(i); addView(child); measureChildWithMargins(child, 0, 0); int childWidth = getDecoratedMeasuredWidth(child); int childHeight = getDecoratedMeasuredHeight(child); if (offsetX + childWidth > width - getPaddingRight()) { offsetX = getPaddingLeft(); offsetY += lineMaxHeight; lineMaxHeight = 0; } layoutDecorated(child, offsetX, offsetY, offsetX + childWidth, offsetY + childHeight); offsetX += childWidth; lineMaxHeight = Math.max(lineMaxHeight, childHeight); } mTotalHeight = offsetY + lineMaxHeight + getPaddingBottom(); } @Override public boolean canScrollVertically() { return true; } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { if (getChildCount() == 0 || dy == 0) { return 0; } int scrolled = 0; if (dy > 0) { View lastChild = getChildAt(getChildCount() - 1); int lastVisiblePos = getPosition(lastChild); if (lastVisiblePos == getItemCount() - 1 && lastChild.getBottom() - dy < getHeight() - getPaddingBottom()) { dy = lastChild.getBottom() - getHeight() + getPaddingBottom(); } } else { View firstChild = getChildAt(0); int firstVisiblePos = getPosition(firstChild); if (firstVisiblePos == 0 && firstChild.getTop() - dy > getPaddingTop()) { dy = firstChild.getTop() - getPaddingTop(); } } offsetChildrenVertical(-dy); scrolled += dy; fill(recycler); return scrolled; } private void fill(RecyclerView.Recycler recycler) { int topOffset = getPaddingTop(); int leftOffset = getPaddingLeft(); int lineMaxHeight = 0; int width = getWidth(); for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); int childWidth = getDecoratedMeasuredWidth(child); int childHeight = getDecoratedMeasuredHeight(child); if (leftOffset + childWidth > width - getPaddingRight()) { leftOffset = getPaddingLeft(); topOffset += lineMaxHeight; lineMaxHeight = 0; } layoutDecorated(child, leftOffset, topOffset, leftOffset + childWidth, topOffset + childHeight); leftOffset += childWidth; lineMaxHeight = Math.max(lineMaxHeight, childHeight); } detachAndScrapAttachedViews(recycler); } @Override public int computeVerticalScrollOffset(RecyclerView.State state) { return computeVerticalScrollExtent(state); } @Override public int computeVerticalScrollExtent(RecyclerView.State state) { return getHeight() - getPaddingTop() - getPaddingBottom(); } @Override public int computeVerticalScrollRange(RecyclerView.State state) { return mTotalHeight; } } ``` 使用时只需要将RecyclerView的LayoutManager设置为自定义的FlowLayoutManager即可实现布局

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值