滚动到列表末尾
Scroll past end of list
我目前有一个 RecyclerView
允许用户滚动长列表。但是,当视图到达数据集中的最后一项时,它会立即停止滚动。
问题: 我怎样才能允许用户进一步滚动,以便 RecyvlerView
中的最后一项能够进一步向上滚动屏幕?
这称为 "overscrolling." 此行为是在 Gingerbread 中引入的。
要打开它,您可以使用
public void setOverScrollMode(int mode);
来自Android docs:
public void setOverScrollMode (int mode)
Added in API level 9 Set the over-scroll mode for this view. Valid
over-scroll modes are OVER_SCROLL_ALWAYS (default),
OVER_SCROLL_IF_CONTENT_SCROLLS (allow over-scrolling only if the view
content is larger than the container), or OVER_SCROLL_NEVER. Setting
the over-scroll mode of a view will have an effect only if the view is
capable of scrolling.
Parameters mode The new over-scroll mode for this view.
您可以阅读更多相关信息 here。
在您的 recyclerViewAdapter 的 onBindViewHolder
上,捕获最后一项条件 position
与您的 list.size() - 1
匹配,然后以编程方式设置底部边距,因此:
@Override
public void onBindViewHolder(@NonNull NewsItemViewHolder holder, int position) {
if(position == getCurrentList().size() - 1){
RecyclerView.LayoutParams layoutParams =
(RecyclerView.LayoutParams) itemHolderRootView.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin,layoutParams.topMargin,layoutParams.rightMargin, 300);
}
}
您可能希望将 300
底部边距与屏幕密度相乘
DisplayMetrics metrics = getResources().getDisplayMetrics();
int bottomMargin = 300 * metrics.densityDpi;
因为它会因屏幕而异。
我目前有一个 RecyclerView
允许用户滚动长列表。但是,当视图到达数据集中的最后一项时,它会立即停止滚动。
问题: 我怎样才能允许用户进一步滚动,以便 RecyvlerView
中的最后一项能够进一步向上滚动屏幕?
这称为 "overscrolling." 此行为是在 Gingerbread 中引入的。
要打开它,您可以使用
public void setOverScrollMode(int mode);
来自Android docs:
public void setOverScrollMode (int mode)
Added in API level 9 Set the over-scroll mode for this view. Valid over-scroll modes are OVER_SCROLL_ALWAYS (default), OVER_SCROLL_IF_CONTENT_SCROLLS (allow over-scrolling only if the view content is larger than the container), or OVER_SCROLL_NEVER. Setting the over-scroll mode of a view will have an effect only if the view is capable of scrolling.
Parameters mode The new over-scroll mode for this view.
您可以阅读更多相关信息 here。
在您的 recyclerViewAdapter 的 onBindViewHolder
上,捕获最后一项条件 position
与您的 list.size() - 1
匹配,然后以编程方式设置底部边距,因此:
@Override
public void onBindViewHolder(@NonNull NewsItemViewHolder holder, int position) {
if(position == getCurrentList().size() - 1){
RecyclerView.LayoutParams layoutParams =
(RecyclerView.LayoutParams) itemHolderRootView.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin,layoutParams.topMargin,layoutParams.rightMargin, 300);
}
}
您可能希望将 300
底部边距与屏幕密度相乘
DisplayMetrics metrics = getResources().getDisplayMetrics();
int bottomMargin = 300 * metrics.densityDpi;
因为它会因屏幕而异。