下面列出了androidx.recyclerview.widget.RecyclerView#HORIZONTAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean isScrollingView(View view) {
if (ViewCatcherUtil.isViewPager(view)
|| view instanceof HorizontalScrollView
|| view instanceof WebView) {
return true;
} else if (ViewCatcherUtil.isRecyclerView(view)) {
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager != null) {
if (manager instanceof LinearLayoutManager) {
LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
return linearManager.getOrientation() == RecyclerView.HORIZONTAL;
} else if (manager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager gridLayoutManager =
(StaggeredGridLayoutManager) manager;
return gridLayoutManager.getOrientation() == RecyclerView.HORIZONTAL;
}
}
}
return false;
}
public SimilarAppcAppsViewHolder(View view, DecimalFormat oneDecimalFormat,
PublishSubject<SimilarAppClickEvent> similarAppClick) {
super(view);
this.oneDecimalFormat = oneDecimalFormat;
this.similarAppClick = similarAppClick;
similarAppcApps = view.findViewById(R.id.similar_appc_list);
similarAppcApps.setNestedScrollingEnabled(false);
HorizontalHeaderItemDecoration similarAppcHeaderItemDecoration =
new HorizontalHeaderItemDecoration(view.getContext(), similarAppcApps,
R.layout.appview_appc_similar_header,
AptoideUtils.ScreenU.getPixelsForDip(112, view.getResources()),
AptoideUtils.ScreenU.getPixelsForDip(5, view.getResources()));
similarAppcApps.addItemDecoration(similarAppcHeaderItemDecoration);
LinearLayoutManager similarAppcLayout =
new LinearLayoutManager(view.getContext(), RecyclerView.HORIZONTAL, false);
similarAppcApps.setLayoutManager(similarAppcLayout);
SnapHelper similarSnap = new SnapToStartHelper();
similarSnap.attachToRecyclerView(similarAppcApps);
similarAppcApps.setAdapter(getSimilarAdapter());
}
private void initialize(@NonNull Context context) {
inflate(context, R.layout.mapwize_mode_view, this);
recyclerView = findViewById(R.id.mapwize_mode_recycler_view);
LinearLayoutManager lm = new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false);
recyclerView.setLayoutManager(lm);
modeViewAdapter = new ModeViewAdapter();
modeViewAdapter.setListener(this);
recyclerView.setAdapter(modeViewAdapter);
}
public static boolean canAutoLoadMore(View view) {
if (ViewCatcherUtil.isRecyclerView(view)) {
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager == null) {
return false;
}
int lastVisiblePosition = 0;
if (manager instanceof LinearLayoutManager) {
LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
return false;
}
lastVisiblePosition = linearManager.findLastVisibleItemPosition();
} else if (manager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
return false;
}
int[] lastPositions = new int[gridLayoutManager.getSpanCount()];
gridLayoutManager.findLastVisibleItemPositions(lastPositions);
for (int value : lastPositions) {
if (value > lastVisiblePosition) {
lastVisiblePosition = value;
}
}
}
RecyclerView.Adapter adapter = recyclerView.getAdapter();
return adapter != null
&& adapter.getItemCount() > 0
&& lastVisiblePosition >= 0
&& lastVisiblePosition >= adapter.getItemCount() - 1;
}
return false;
}
public static boolean canAutoRefresh(View view) {
if (ViewCatcherUtil.isRecyclerView(view)) {
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager == null) {
return false;
}
int firstVisiblePosition = -1;
if (manager instanceof LinearLayoutManager) {
LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
return false;
}
firstVisiblePosition = linearManager.findFirstVisibleItemPosition();
} else if (manager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
return false;
}
int[] firstPositions = new int[gridLayoutManager.getSpanCount()];
gridLayoutManager.findFirstVisibleItemPositions(firstPositions);
for (int value : firstPositions) {
if (value == 0) {
firstVisiblePosition = 0;
break;
}
}
}
RecyclerView.Adapter adapter = recyclerView.getAdapter();
return adapter != null && firstVisiblePosition == 0;
}
return false;
}
public void setOrientation(int orientation) {
switch (orientation) {
case RecyclerView.HORIZONTAL:
mOrientationHelper = new HorizontalHelper();
break;
case RecyclerView.VERTICAL:
mOrientationHelper = new VerticalHelper();
break;
default:
throw new IllegalArgumentException("Unknown orientation " + orientation);
}
mOrientation = orientation;
removeAllViews();
requestLayout();
}
@Override
public int computeHorizontalScrollExtent(RecyclerView.State state) {
if (getOrientation() == RecyclerView.HORIZONTAL) {
return super.computeHorizontalScrollExtent(state);
}
return computeHorizontalScrollExtent();
}
/**
* 计算水平滚动边界
*
* @return 滚动边界
*/
protected int computeHorizontalScrollExtent() {
if (getOrientation() == RecyclerView.HORIZONTAL) {
final RecyclerView view = getRecyclerView();
return view == null ? 0 : view.computeHorizontalScrollExtent();
}
return getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
}
/**
* 计算水平滚动范围
*
* @return 滚动范围
*/
protected int computeHorizontalScrollRange() {
if (getOrientation() == RecyclerView.HORIZONTAL) {
final RecyclerView view = getRecyclerView();
return view == null ? 0 : view.computeHorizontalScrollRange();
}
return getChildMaxWidth(mChildMaxWidth) + mLeftDecorationMaxWidthOfChildMaxWidth +
mRightDecorationMaxWidthOfChildMaxWidth;
}
/**
* 判断另一方向是否能够滚动
*
* @return 是否能够
*/
public boolean canScrollAnotherDirection() {
if (getOrientation() == RecyclerView.HORIZONTAL) {
return canScrollVertically();
} else {
return canScrollHorizontally();
}
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (getOrientation() == RecyclerView.HORIZONTAL) {
return super.scrollHorizontallyBy(dx, recycler, state);
}
return scrollBy(dx);
}
@Override
public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, int right, int bottom) {
if (!mCenter) {
super.layoutDecoratedWithMargins(child, left, top, right, bottom);
return;
}
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int leftDecorationWidth = getLeftDecorationWidth(child);
final int topDecorationHeight = getTopDecorationHeight(child);
final int rightDecorationWidth = getRightDecorationWidth(child);
final int bottomDecorationHeight = getBottomDecorationHeight(child);
final ViewGroup parent = (ViewGroup) child.getParent();
final int offset;
if (getOrientation() == RecyclerView.HORIZONTAL) {
final int contentHeight = parent.getMeasuredHeight() -
parent.getPaddingTop() - parent.getPaddingBottom();
offset = (contentHeight - (bottom - top)) / 2;
child.layout(left + leftDecorationWidth + lp.leftMargin,
top + topDecorationHeight + lp.topMargin + offset,
right - rightDecorationWidth - lp.rightMargin,
bottom - bottomDecorationHeight - lp.bottomMargin + offset);
} else {
final int contentWidth = parent.getMeasuredWidth() -
parent.getPaddingLeft() - parent.getPaddingRight();
offset = (contentWidth - (right - left)) / 2;
child.layout(left + leftDecorationWidth + offset + lp.leftMargin,
top + topDecorationHeight + lp.topMargin,
right - rightDecorationWidth - lp.rightMargin + offset,
bottom - bottomDecorationHeight - lp.bottomMargin);
}
}
public void init(Context context, DatePickerDialog.ScrollOrientation scrollOrientation) {
@RecyclerView.Orientation
int layoutOrientation = scrollOrientation == DatePickerDialog.ScrollOrientation.VERTICAL
? RecyclerView.VERTICAL
: RecyclerView.HORIZONTAL;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, layoutOrientation, false);
setLayoutManager(linearLayoutManager);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
setClipChildren(false);
mContext = context;
setUpRecyclerView(scrollOrientation);
}
public TrendHorizontalLinearLayoutManager(Context context, int fillCount) {
super(context, RecyclerView.HORIZONTAL, false);
this.context = context;
this.fillCount = fillCount;
}