下面列出了androidx.recyclerview.widget.RecyclerView#VERTICAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean isLastColumn(int orientation, int position, int columnCount, int childCount) {
if (orientation == RecyclerView.VERTICAL) {
if (columnCount == 1) return true;
return (position + 1) % columnCount == 0;
} else {
if (columnCount == 1) {
return position + 1 == childCount;
} else {
int lastRawItemCount = childCount % columnCount;
int rawCount = (childCount - lastRawItemCount) / columnCount + (lastRawItemCount > 0 ? 1 : 0);
int rawPositionJudge = (position + 1) % columnCount;
if (rawPositionJudge == 0) {
int positionRaw = (position + 1) / columnCount;
return rawCount == positionRaw;
} else {
int rawPosition = (position + 1 - rawPositionJudge) / columnCount + 1;
return rawCount == rawPosition;
}
}
}
}
@Override
public int computeVerticalScrollRange(RecyclerView.State state) {
if (getOrientation() == RecyclerView.VERTICAL) {
return super.computeVerticalScrollRange(state);
}
return computeVerticalScrollRange();
}
/**
* 计算垂直滚动范围
*
* @return 滚动范围
*/
protected int computeVerticalScrollRange() {
if (getOrientation() == RecyclerView.VERTICAL) {
final RecyclerView view = getRecyclerView();
return view == null ? 0 : view.computeVerticalScrollRange();
}
return getChildMaxHeight(mChildMaxHeight) + mTopDecorationMaxWidthOfChildMaxHeight +
mBottomDecorationMaxWidthOfChildMaxHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (mParent == null) {
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
if (((LinearLayoutManager) parent.getLayoutManager()).getOrientation()
!= RecyclerView.VERTICAL) {
throw new IllegalArgumentException("Only support LinearLayoutManager.VERTICAL");
}
}
mParent = parent;
initListener();
}
}
/**
* 计算垂直滚动边界
*
* @return 滚动边界
*/
protected int computeVerticalScrollExtent() {
if (getOrientation() == RecyclerView.VERTICAL) {
final RecyclerView view = getRecyclerView();
return view == null ? 0 : view.computeVerticalScrollExtent();
}
return getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
}
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);
}
@Override
public void fillView(IMELanguageWrapper entity, int size) {
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), RecyclerView.VERTICAL,false);
binding.recyclerView.setLayoutManager(layoutManager);
binding.layoutSetName.setText(entity.getMultiIMELanguage().getLayoutName());
if(size <= 1){
binding.removeBtn.setVisibility(View.GONE);
}else{
binding.removeBtn.setVisibility(View.VISIBLE);
}
adapter = new LanguageSettingAdapter();
binding.recyclerView.setAdapter(adapter);
adapter.setDataSet(entity.getMultiIMELanguage().getIMELanguageWrappers());
binding.removeBtn.setOnClickListener(v -> {
if(languageListener == null){
return;
}
languageListener.onClickRemove(entity);
});
binding.recyclerView.setOnTouchListener((v, event) -> itemView.onTouchEvent(event));
itemView.setOnClickListener(v -> {
if(languageListener == null){
return;
}
List<IMELanguage> imeList = entity.getMultiIMELanguage().getSubtypeIMEList();
if (imeList.size() > 0) {
languageListener.onClickChangeLayoutSet(imeList.get(0));
}
});
}
@SuppressWarnings("unchecked")
private void applySectionGap(Rect outRect, RecyclerView.Adapter adapter, int position, int orientation) {
// Section Gap Offset
if (mSectionOffset > 0 && adapter instanceof FlexibleAdapter) {
FlexibleAdapter flexibleAdapter = (FlexibleAdapter) adapter;
IFlexible nextItem = flexibleAdapter.getItem(position + 1);
// IMPORTANT: the check must be done on the BOTTOM of the section,
// otherwise the sticky header will jump!
if (flexibleAdapter.isHeader(nextItem)) {
//Log.v("applySectionGap position=%s", position);
if (orientation == RecyclerView.VERTICAL) {
outRect.bottom += mSectionOffset;
} else {
outRect.right += mSectionOffset;
}
}
if (position >= adapter.getItemCount() - mSectionGapOnLastItem) {
//Log.v("applySectionGapOnLastPosition position=%s", position);
if (orientation == RecyclerView.VERTICAL) {
outRect.bottom += mSectionOffset;
} else {
outRect.right += mSectionOffset;
}
}
}
}
private boolean directionVertical() {
switch (LAYOUT_MODE){
case LINEAR_LAYOUT:
return ((LinearLayoutManager)mLayoutManager).getOrientation() == RecyclerView.VERTICAL;
case GRID_LAYOUT:
return ((GridLayoutManager)mLayoutManager).getOrientation() == RecyclerView.VERTICAL;
case STAGGERED_LAYOUT:
return ((StaggeredGridLayoutManager)mLayoutManager).getOrientation() == RecyclerView.VERTICAL;
}
return true;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!checkPermissions(this, requiredPermissions()))
return;
setContentView(R.layout.activity_media_scrolling);
final Intent callingIntent = getIntent();
mediaChoose =
callingIntent != null &&
(Intent.ACTION_GET_CONTENT.equals(callingIntent.getAction()) || Intent.ACTION_PICK.equals(callingIntent.getAction()));
baldTitleBar = findViewById(R.id.bald_title_bar);
recyclerView = findViewById(R.id.child);
baldTitleBar.setTitle(title());
final Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
final int num = (point.x / point.y) > 0 ? 6 : 3;
final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, num, RecyclerView.VERTICAL, REVERSED);
recyclerView.setLayoutManager(gridLayoutManager);
width = (int) ((recyclerView.getWidth() / 3) - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
cursor = cursor(getContentResolver());
setupBeforeAdapter();
adapter = new MediaRecyclerViewAdapter();
recyclerView.setAdapter(adapter);
}
@Override
public int computeVerticalScrollOffset(RecyclerView.State state) {
if (getOrientation() == RecyclerView.VERTICAL) {
return super.computeVerticalScrollOffset(state);
}
return computeVerticalScrollOffset();
}
@Override
public int computeVerticalScrollExtent(RecyclerView.State state) {
if (getOrientation() == RecyclerView.VERTICAL) {
return super.computeVerticalScrollExtent(state);
}
return computeVerticalScrollExtent();
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
if (mDivider == null) {
outRect.set(0, 0, 0, 0);
return;
}
int pos = parent.indexOfChild(view);
if (pos != -1 && isDecorated(pos)) {
if (mOrientation == RecyclerView.VERTICAL)
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
else outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
protected void draw(Canvas c, RecyclerView parent) {
if (parent.getLayoutManager() == null) {
return;
}
if (LayoutUtils.getOrientation(parent) == RecyclerView.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (mColumnSizeChanged && mColumnSize > 0) {
int totalSpace;
if (getOrientation() == RecyclerView.VERTICAL) {
totalSpace = getWidth() - getPaddingRight() - getPaddingLeft();
} else {
totalSpace = getHeight() - getPaddingTop() - getPaddingBottom();
}
int spanCount;
switch (mStrategy) {
default:
case STRATEGY_MIN_SIZE:
spanCount = getSpanCountForMinSize(totalSpace, mColumnSize);
break;
case STRATEGY_SUITABLE_SIZE:
spanCount = getSpanCountForSuitableSize(totalSpace, mColumnSize);
break;
}
setSpanCount(spanCount);
mColumnSizeChanged = false;
if (null != mListeners) {
for (int i = 0, n = mListeners.size(); i < n; i++) {
mListeners.get(i).onUpdateSpanCount(spanCount);
}
}
}
super.onLayoutChildren(recycler, state);
}
private boolean isFirstColumn(int orientation, int position, int columnCount, int childCount) {
if (orientation == RecyclerView.VERTICAL) {
if (columnCount == 1) return true;
return position % columnCount == 0;
} else {
return position < columnCount;
}
}
@NonNull
protected LinearLayoutManager createLayoutManager(@NonNull RecyclerView recyclerView) {
return new LinearLayoutManager(recyclerView.getContext(), RecyclerView.VERTICAL, false);
}
@Override
public RecyclerView.LayoutManager getLayoutManager() {
return new GridLayoutManager(mContext, 3, RecyclerView.VERTICAL, false);
}
private void setupLayout() {
final LinearLayoutManager llm = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
llm.setItemPrefetchEnabled(true);
mHomeRv.setLayoutManager(llm);
mHomeRv.setHasFixedSize(true);
adapter = new HomeAdapter(this);
mHomeRv.setAdapter(adapter);
adapter.setOnHomeClickedListener(position -> {
Bundle transitionbundle = ActivityOptions.makeSceneTransitionAnimation(this).toBundle();
switch (position) {
default:
case 0:
startActivity(new Intent(this, PieActivity.class), transitionbundle);
break;
case 1:
startActivity(new Intent(this, RingActivity.class), transitionbundle);
break;
case 2:
startActivity(new Intent(this, FillActivity.class), transitionbundle);
break;
case 3:
startActivity(new Intent(this, AdaptiveColorsActivity.class), transitionbundle);
break;
case 4:
startActivity(new Intent(this, GradientColorsActivity.class), transitionbundle);
break;
case 5:
startActivity(new Intent(this, TextFormatterActivity.class), transitionbundle);
break;
case 6:
Uri url = Uri.parse(getString(R.string.github_url));
Intent intent = new Intent(Intent.ACTION_VIEW, url);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
break;
}
});
}
private void initViews(View view) {
stopwatch = view.findViewById(R.id.stopwatch);
stopwatch.setStopwatchListener(this);
savedItemList = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, true);
StopWatchAdapter stopWatchAdapter = new StopWatchAdapter(savedItemList);
recyclerView = view.findViewById(R.id.rv_values);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(stopWatchAdapter);
startBtn = view.findViewById(R.id.btn_start);
startBtn.setOnClickListener(this);
stopBtn = view.findViewById(R.id.btn_stop);
stopBtn.setOnClickListener(this);
saveBtn = view.findViewById(R.id.btn_save);
saveBtn.setOnClickListener(this);
}