java 在 Recycler View 的情况下,等效的 listview.setSelection 是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27377830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent listview.setSelection in case of Recycler View
提问by user3008777
In the case of a ListView
if we want to make a particular item selected we use the setSelection
method. How do we do this in case of RecyclerView
?
在 a 的情况下,ListView
如果我们想要选择特定项目,我们使用该setSelection
方法。在这种情况下,我们如何做到这一点RecyclerView
?
回答by Libin
Use RecyclerView
LayoutManager
to scroll item at position
用于RecyclerView
LayoutManager
在位置滚动项目
recyclerView.getLayoutManager().scrollToPosition(position)
recyclerView.getLayoutManager().scrollToPosition(position)
回答by Pramod
Check
查看
scrollToPositionWithOffset(int position, int offset)
scrollToPositionWithOffset(5,0);
from LinearLayoutManager Scroll to the specified adapter position with the given offset from resolved layout start.
从 LinearLayoutManager 滚动到指定的适配器位置,从解析的布局开始具有给定的偏移量。
pass offset as 0 if you want selection at top
如果您想在顶部选择,则将偏移量传递为 0
This worked for me
这对我有用
回答by Reaz Murshed
Check
查看
recyclerView.scrollToPosition(cursor.getcount() - 1);
回答by HaimS
ListView.setSelected() does (at least) two things:
ListView.setSelected() 做(至少)两件事:
- It sets the item in the list to be selected (while removing the selection from another item - if such exists)
- It scrolls the list so that the item will be visible on the screen.
- 它设置要选择的列表中的项目(同时从另一个项目中删除选择 - 如果存在)
- 它滚动列表,以便该项目在屏幕上可见。
To achieve 2. either call scrollToPosition() method of RecyclerView (as indicated by Loser), or call one of the scrolling methods of the LayoutManager object depending on your desired scrolling behavior.
实现 2. 要么调用 RecyclerView 的 scrollToPosition() 方法(如 Loser 所示),要么根据您想要的滚动行为调用 LayoutManager 对象的滚动方法之一。
For example, recyclerView.getLayoutManager().smoothScrollToPosition()
例如,recyclerView.getLayoutManager().smoothScrollToPosition()
You may want to scroll the minimum so that the selected item shows on the screen. If so and you are using LinearLayoutManager or GridLayoutManager, you can build such scroll logic based on findFirstCompletelyVisibleItemPosition() and findLastCompletelyVisibleItemPosition() defined in these classes.
您可能希望滚动最小值,以便所选项目显示在屏幕上。如果是这样,并且您正在使用 LinearLayoutManager 或 GridLayoutManager,则可以基于这些类中定义的 findFirstCompletelyVisibleItemPosition() 和 findLastCompletelyVisibleItemPosition() 构建此类滚动逻辑。
Achieving 1. is more tricky. You may want to use the following recipe:
实现 1. 更加棘手。您可能需要使用以下配方:
First define a background color in colors.xml, item_state_selected_color, to be used when an item is selected. In your onCreateViewHolder() implementation create a StateListDrawalbe and set it as the background of the view. Say something like this:
首先在colors.xml 中定义一个背景颜色,item_state_selected_color,以在选择项目时使用。在您的 onCreateViewHolder() 实现中,创建一个 StateListDrawalbe 并将其设置为视图的背景。像这样说:
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// inflate the item view
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(itemResourceId,viewGroup, false);
// create color drawable by a resorce id
ColorDrawable colorDrawableSelected =
new ColorDrawable(resources.getColor(R.color.item_state_selected_color));
// create StateListDrawable object and define its states
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, colorDrawableSelected);
stateListDrawable.addState(StateSet.WILD_CARD, null);
// set the StateListDrawable as background of the item view
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
itemView.setBackgroundDrawable(stateListDrawable);
}
else {
itemView.itemView.setBackground(stateListDrawable);
}
// create view holder object providing it with the item view
return new YourViewHolder(itemView);
}
In YourAdapter object (or elsewhere) save a variable, mCurrentSelectedPosition (probably initialized to -1) that holds the current selected position. Still in the adapter, define handler for clicks on recycler view items, depending on your click logic. For example:
在 YourAdapter 对象(或其他地方)中保存一个变量 mCurrentSelectedPosition(可能初始化为 -1),用于保存当前选定的位置。仍然在适配器中,根据您的点击逻辑,为点击回收器视图项定义处理程序。例如:
void onItemClick(int position) {
YourViewHolder yourViewHolder;
int oldSelectedPosition = mCurrentSelectedPosition;
if (position != mCurrentSelectedPosition) {
mCurrentSelectedPosition = position;
if (oldSelectedPosition != -1) {
yourViewHolder = findViewHolderForPosition(oldSelectedPosition);
yourViewHolder.itemView.setSelected(false);
}
yourViewHolder = findViewHolderForPosition(mCurrentSelectedPosition);
yourViewHolder.itemView.setSelected(true);
}
}
Next, in the constructor of YourViewHolder set listener to clicks on the item:
接下来,在 YourViewHolder 的构造函数中将监听器设置为单击该项目:
public YourViewHolder(View itemView,YourAdapter adapter) {
mAdapter = adapter;
// ... other code here ...
itemView.setOnClickListener(this);
}
Still in YourViewHolder override the onClick() method to delegate handling to the adapter. like this
仍在 YourViewHolder 中覆盖 onClick() 方法以将处理委托给适配器。像这样
@Override
public void onClick(View v) {
mAdapter.onItemClick(getPosition());
}
Now there is just last problem to solve - we need to keep track of the selected item with respect to recycling.
现在只有最后一个问题需要解决——我们需要跟踪所选项目的回收情况。
@Override
public void onBindViewHolder(YourViewHolder yourViewHolder, int position) {
if (position == mCurrentSelectedPosition) {
yourViewHolder.itemView.setSelected(true);
}
else {
yourViewHolder.itemView.setSelected(false);
}
// ... other code here ...
}
Good luck!
祝你好运!