- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某些原因,我需要自定义一个 LayoutManager
来实现 RecyclerView
的流动布局项。代码是:
public class FlowLayoutManager extends RecyclerView.LayoutManager implements
RecyclerView.SmoothScroller.ScrollVectorProvider {
final static int LAYOUT_START = -1;
final static int LAYOUT_END = 1;
public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
public static final int VERTICAL = LinearLayout.VERTICAL;
public static final int INVALID_OFFSET = Integer.MIN_VALUE;
private static final int DEFAULT_HORIZONTAL_SPACE = 12;
private static final int DEFAULT_VERTICAL_SPACE = 12;
@IntDef({HORIZONTAL, VERTICAL})
public @interface Orientation {
}
private SparseArray<Rect> mScrapRects;
private SparseIntArray mColumnCountOfRow;
private SparseArray<LayoutParams> mScrapSites;
private int mOffsetX;
private int mOffsetY;
private int mItemCount;
private int mLeft, mTop, mRight, mBottom;
private int mWidth, mHeight;
private int mTotalWidth;
private int mTotalHeight;
private int mScrollOffsetX;
private int mScrollOffsetY;
private int mVerticalSpace = DEFAULT_VERTICAL_SPACE;
private int mHorizontalSpace = DEFAULT_HORIZONTAL_SPACE;
int mPendingScrollPositionOffset = INVALID_OFFSET;
@Orientation
private int mOrientation = VERTICAL;
private RecyclerView.Recycler mRecycler;
private RecyclerView.State mState;
public FlowLayoutManager() {
this(VERTICAL);
}
public FlowLayoutManager(@Orientation int orientation) {
this(orientation, DEFAULT_VERTICAL_SPACE, DEFAULT_HORIZONTAL_SPACE);
}
public FlowLayoutManager(@Orientation int orientation, int verticalSpace, int horizontalSpace) {
setOrientation(orientation);
setSpace(verticalSpace, horizontalSpace);
setAutoMeasureEnabled(true);
}
public FlowLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes);
setOrientation(properties.orientation);
setReverseLayout(properties.reverseLayout);
setStackFromEnd(properties.stackFromEnd);
setAutoMeasureEnabled(true);
}
public void setSpace(int verticalSpace, int horizontalSpace) {
if (verticalSpace == mVerticalSpace && horizontalSpace == mHorizontalSpace) {
return;
}
this.mVerticalSpace = verticalSpace;
this.mHorizontalSpace = horizontalSpace;
requestLayout();
}
public void setOrientation(@Orientation int orientation) {
if (orientation == mOrientation) {
return;
}
this.mOrientation = orientation;
requestLayout();
}
public void setReverseLayout(boolean reverseLayout) {
}
public void setStackFromEnd(boolean stackFromEnd) {
}
@Orientation
public int getOrientation() {
return mOrientation;
}
@Override
public LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return new LayoutParams(c, attrs);
}
@Override
public LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
if (lp instanceof ViewGroup.MarginLayoutParams) {
return new LayoutParams((ViewGroup.MarginLayoutParams) lp);
} else {
return new LayoutParams(lp);
}
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return lp instanceof LayoutParams;
}
@Override
public boolean supportsPredictiveItemAnimations() {
return true;
}
@Override
public void scrollToPosition(int position) {
if (position >= getItemCount()) {
return;
}
View view = findViewByPosition(position);
if (view != null) {
if (canScrollVertically()) {
scrollVerticallyBy((int) (view.getY() - mTop), mRecycler, mState);
} else if (canScrollHorizontally()) {
scrollHorizontallyBy((int) (view.getX() - mLeft), mRecycler, mState);
}
}
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext());
scroller.setTargetPosition(position);
startSmoothScroll(scroller);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
final int direction = calculateScrollDirectionForPosition(targetPosition);
PointF outVector = new PointF();
if (direction == 0) {
return null;
}
if (canScrollHorizontally()) {
outVector.x = direction;
outVector.y = 0;
} else if (canScrollVertically()) {
outVector.x = 0;
outVector.y = direction;
}
return outVector;
}
@Override
public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) {
mPendingScrollPositionOffset = INVALID_OFFSET;
removeAllViews();
}
@Override
public void onLayoutCompleted(RecyclerView.State state) {
// mPendingScrollPositionOffset = INVALID_OFFSET;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
mRecycler = recycler;
mState = state;
mItemCount = getItemCount();
if (mItemCount == 0) {
detachAndScrapAttachedViews(recycler);
return;
}
if (getChildCount() == 0 && state.isPreLayout()) {
return;
}
mScrapRects = new SparseArray<>(mItemCount);
mScrapSites = new SparseArray<>(mItemCount);
mColumnCountOfRow = new SparseIntArray();
mScrollOffsetX = 0;
mScrollOffsetY = 0;
mWidth = getWidth();
mHeight = getHeight();
mLeft = getPaddingLeft();
mTop = getPaddingTop();
mRight = getPaddingRight();
mBottom = getPaddingBottom();
mOffsetX = mLeft;
mOffsetY = mTop;
detachAndScrapAttachedViews(recycler);
if (canScrollVertically()) {
mTotalHeight = calculateVerticalChildrenSites(recycler);
scrollVerticallyBy(mPendingScrollPositionOffset, recycler, state);
} else {
mTotalWidth = calculateHorizontalChildrenSites(recycler);
scrollHorizontallyBy(mPendingScrollPositionOffset, recycler, state);
}
// fillAndRecycleView(recycler, state);
}
private int calculateVerticalChildrenSites(RecyclerView.Recycler recycler) {
final int[] maxRowHeight = {0};
final int[] totalHeight = {0};
final Point point = new Point();
for (int i = 0; i < mItemCount; i++) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
final int width = getDecoratedMeasurementHorizontal(scrap);
final int height = getDecoratedMeasurementVertical(scrap);
if (mOffsetX + width + mHorizontalSpace > mWidth - mRight) {
mOffsetX = mLeft;
mOffsetY += maxRowHeight[0] + (i == 0 ? 0 : mVerticalSpace);
maxRowHeight[0] = 0;
point.x = 0;
point.y++;
}
maxRowHeight[0] = Math.max(height, maxRowHeight[0]);
LayoutParams lp = (LayoutParams) scrap.getLayoutParams();
lp.column = point.x++;
lp.row = point.y;
if (lp.column != 0) {
mOffsetX += mHorizontalSpace;
}
mScrapSites.put(i, lp);
mColumnCountOfRow.put(lp.row, lp.column + 1);
Rect frame = mScrapRects.get(i);
if (frame == null) {
frame = new Rect();
}
frame.set(mOffsetX, mOffsetY, mOffsetX = mOffsetX + width, mOffsetY + height);
mScrapRects.put(i, frame);
totalHeight[0] = Math.max(totalHeight[0], mOffsetY + height);
layoutDecoratedWithMargins(scrap, frame.left, frame.top, frame.right, frame.bottom);
}
return Math.max(totalHeight[0] - mTop, getVerticalSpace());
}
private int calculateHorizontalChildrenSites(RecyclerView.Recycler recycler) {
final int[] maxColumnWidth = {0};
final int[] totalWidth = {0};
final Point point = new Point();
for (int i = 0; i < mItemCount; i++) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
final int width = getDecoratedMeasurementHorizontal(scrap);
final int height = getDecoratedMeasurementVertical(scrap);
if (mOffsetY + height + mVerticalSpace > mHeight - mBottom) {
mOffsetY = mTop;
mOffsetX += maxColumnWidth[0] + (i == 0 ? 0 : mHorizontalSpace);
maxColumnWidth[0] = 0;
point.x++;
point.y = 0;
}
maxColumnWidth[0] = Math.max(width, maxColumnWidth[0]);
LayoutParams lp = (LayoutParams) scrap.getLayoutParams();
lp.column = point.x;
lp.row = point.y++;
if (lp.row != 0) {
mOffsetY += mVerticalSpace;
}
mScrapSites.put(i, lp);
mColumnCountOfRow.put(lp.row, lp.column + 1);
Rect frame = mScrapRects.get(i);
if (frame == null) {
frame = new Rect();
}
frame.set(mOffsetX, mOffsetY, mOffsetX + width, mOffsetY = mOffsetY + height);
mScrapRects.put(i, frame);
totalWidth[0] = Math.max(totalWidth[0], mOffsetX + width);
layoutDecoratedWithMargins(scrap, frame.left, frame.top, frame.right, frame.bottom);
}
return Math.max(totalWidth[0] - mLeft, getHorizontalSpace());
}
@SuppressWarnings("unused")
private void fillAndRecycleView(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (mItemCount == 0 || state.isPreLayout()) {
return;
}
Rect displayFrame = canScrollVertically()
? new Rect(mLeft, mScrollOffsetY, mWidth, mScrollOffsetY + mHeight)
: new Rect(mScrollOffsetX, mTop, mScrollOffsetX + mWidth, mHeight);
for (int i = 0; i < mItemCount; i++) {
Rect rect = mScrapRects.get(i);
if (!Rect.intersects(displayFrame, rect)) {
View scrap = getChildAt(i);
if (scrap != null) {
removeAndRecycleView(scrap, recycler);
}
return;
}
View scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
if (canScrollVertically()) {
layoutDecoratedWithMargins(scrap, rect.left, rect.top - mScrollOffsetY, rect.right, rect.bottom - mScrollOffsetY);
} else {
layoutDecoratedWithMargins(scrap, rect.left - mScrollOffsetX, rect.top, rect.right - mScrollOffsetX, rect.bottom);
}
}
}
@Override
public boolean canScrollVertically() {
return mOrientation == VERTICAL;
}
@Override
public boolean canScrollHorizontally() {
return mOrientation == HORIZONTAL;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (dy == 0 || mItemCount == 0) {
return 0;
}
int travel = dy;
if (mScrollOffsetY + travel < 0) {
travel = -mScrollOffsetY;
} else if (mScrollOffsetY + travel > mTotalHeight - getVerticalSpace()) {//如果滑动到最底部
travel = mTotalHeight - getVerticalSpace() - mScrollOffsetY;
}
mPendingScrollPositionOffset = (mScrollOffsetY += travel);
offsetChildrenVertical(-travel);
return travel;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (dx == 0 || mItemCount == 0) {
return 0;
}
int travel = dx;
if (mScrollOffsetX + travel < 0) {
travel = -mScrollOffsetX;
} else if (mScrollOffsetX + travel > mTotalWidth - getHorizontalSpace()) {//如果滑动到最底部
travel = mTotalWidth - getHorizontalSpace() - mScrollOffsetX;
}
mPendingScrollPositionOffset = (mScrollOffsetX += travel);
// detachAndScrapAttachedViews(recycler);
offsetChildrenHorizontal(-travel);
// fillAndRecycleView(recycler, state);
return travel;
}
@Nullable
public LayoutParams getLayoutParamsByPosition(int position) {
return mScrapSites.get(position);
}
public int getRow(int position) {
LayoutParams params = getLayoutParamsByPosition(position);
return params != null ? params.row : 0;
}
public int getColumn(int position) {
LayoutParams params = getLayoutParamsByPosition(position);
return params != null ? params.column : 0;
}
public int getColumnCountOfRow(int row) {
return mColumnCountOfRow.get(row, 1);
}
private int calculateScrollDirectionForPosition(int position) {
if (getChildCount() == 0) {
return LAYOUT_START;
}
return position < getFirstChildPosition() ? LAYOUT_START : LAYOUT_END;
}
int getFirstChildPosition() {
final int childCount = getChildCount();
return childCount == 0 ? 0 : getPosition(getChildAt(0));
}
private int getDecoratedMeasurementHorizontal(View view) {
final LayoutParams params = (LayoutParams) view.getLayoutParams();
return getDecoratedMeasuredWidth(view) + params.leftMargin + params.rightMargin;
}
private int getDecoratedMeasurementVertical(View view) {
final LayoutParams params = (LayoutParams) view.getLayoutParams();
return getDecoratedMeasuredHeight(view) + params.topMargin + params.bottomMargin;
}
private int getVerticalSpace() {
return mHeight - mBottom - mTop;
}
private int getHorizontalSpace() {
return mWidth - mLeft - mRight;
}
public static class LayoutParams extends RecyclerView.LayoutParams {
//Current row in the grid
public int row;
//Current column in the grid
public int column;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(RecyclerView.LayoutParams source) {
super(source);
}
@Override
public String toString() {
return "LayoutParams = {"
+ "width=" + width
+ ",height=" + height
+ ",row=" + row
+ ",column=" + column + "}";
}
}
之后,将这个FlowlayoutManager
设置为一个RecyclerView
,然后我发现RecyclerView
不显示滚动条,而设置LinearLayoutManager
它显示。
这是 xml 文件中的 RecyclerView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="vertical"/>
具有 400dp
高度和滚动条属性。
问题似乎是我自定义的FlowLayoutManager
,但我真的不知道这个问题,谁能帮忙,谢谢!
最佳答案
查看 RecyclerView
和 LinearLayoutManager
的源代码,似乎自定义 LayoutManager 必须实现这些方法才能显示滚动条:
关于android - 带有自定义 LayoutManager 的 RecyclerView 不显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56113863/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!