自适应文字列表布局
来源:互联网 发布:csdn黑马程序员 编辑:程序博客网 时间:2024/05/17 04:45
package com.app.hero.ui.live.adapter;import android.content.Context;import android.graphics.Rect;import android.support.v4.util.SparseArrayCompat;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.RecyclerView.LayoutManager;import android.view.View;import android.view.ViewGroup;/** * Created on 2017/9/19. */public class AutoTextContentLayoutManager extends LayoutManager { private int mTotalHeight; private int mVerticalOffset; private Pool<Rect> mItemFrames; public AutoTextContentLayoutManager(Context context) { super(); setAutoMeasureEnabled(true); mItemFrames = new Pool<>(new Pool.PoolObject<Rect>() { @Override public Rect get() { return new Rect();} }); } @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { if (getItemCount() <= 0 || state.isPreLayout()) { return;} detachAndScrapAttachedViews(recycler); updateMeasurements(recycler); fill(recycler, state); } private void fill(RecyclerView.Recycler recycler, RecyclerView.State state) { if (getItemCount() <= 0 || state.isPreLayout()) { return;} Rect displayRect = new Rect(0, mVerticalOffset, getHorizontalSpace(), getVerticalSpace() + mVerticalOffset); for (int i = 0; i < getItemCount(); i++) { Rect frame = mItemFrames.get(i); View view = recycler.getViewForPosition(i); if (Rect.intersects(displayRect, frame)) { addView(view); measureChildWithMargins(view, 0, 0); layoutDecorated(view, frame.left, frame.top - mVerticalOffset, frame.right, frame.bottom - mVerticalOffset); } else { removeAndRecycleView(view,recycler); } } } public boolean canScrollVertically() { return true; } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { detachAndScrapAttachedViews(recycler); if (mVerticalOffset + dy < 0) { dy = -mVerticalOffset; } else if (mVerticalOffset + dy > mTotalHeight - getVerticalSpace()) { dy = mTotalHeight - getVerticalSpace() - mVerticalOffset; } offsetChildrenVertical(-dy); mVerticalOffset += dy; fill(recycler, state); return dy; } public void scrollToTop() { mVerticalOffset = 0; requestLayout(); } private int getHorizontalSpace() { return getWidth() - getPaddingLeft() - getPaddingRight(); } private int getVerticalSpace() { return getHeight() - getPaddingTop() - getPaddingBottom(); } public void updateMeasurements(RecyclerView.Recycler recycler){ mTotalHeight = 0; mItemFrames.clear(); int width = getHorizontalSpace(); for (int i = 0; i < getItemCount(); i++) { Rect item = mItemFrames.get(i); View view = recycler.getViewForPosition(i); measureChildWithMargins(view, 0, 0); int itemWidth = getDecoratedMeasuredWidth(view); int itemHeight = getDecoratedMeasuredHeight(view); // 每一组的第一行 if (i == 0) { item.set(0, 0, itemWidth, itemHeight); mTotalHeight += itemHeight; } else if (i > 0) { View preView = recycler.getViewForPosition(i - 1); measureChildWithMargins(preView, 0, 0); if (mItemFrames.get(i - 1).right + itemWidth > width) { int preItemHeight = getDecoratedMeasuredHeight(preView); int top = mItemFrames.get(i - 1).top + preItemHeight; item.set(0, top, itemWidth,top + itemHeight); mTotalHeight += itemHeight; } else { item.set(mItemFrames.get(i - 1).right, mItemFrames.get(i - 1).top, mItemFrames.get(i - 1).right + itemWidth, mItemFrames.get(i - 1).top + itemHeight); } } } }}class Pool<T> { private SparseArrayCompat<T> mPool; private PoolObject<T> mPoolObject; public Pool(PoolObject<T> poolObject) { mPool = new SparseArrayCompat<>(); mPoolObject = poolObject; } public void clear() { mPool.clear(); } public T get(int key) { T res = mPool.get(key); if (res == null) { res = mPoolObject.get(); mPool.put(key, res); } return res; } public interface PoolObject<T> { T get(); }}
阅读全文
0 0
- 自适应文字列表布局
- 图文布局列表 文字垂直居中
- padding——图片文字的自适应布局
- 自适应布局
- 自适应布局
- 自适应布局
- 自适应布局
- 自适应布局
- 自适应布局---自适应宽度
- 关于TableViewCell 自适应(文字自适应)
- table 文字自适应
- UILable自适应文字高度
- uibutton文字自适应。
- 文字高度自适应
- Label文字自适应
- label文字自适应
- Label的文字自适应
- h5文字自适应
- 传输层-4、TCP协议
- 计算今年和去年的时间,一个月的都是,用来计算同比
- 2.22 网络基础
- Linux下编译安装Nginx
- bootstrap table 导出
- 自适应文字列表布局
- redis启动和停止和后台运行
- 学生系统问题
- python里怎么样求解微分方程?
- 计蒜客 16955 Islands(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F)
- mysql常用代码
- mysql存储过程详细教程
- 轻量级web server Tornado代码分析
- Android开发之时间选择器