[英]How to highlight a item in recycler view by position android
I am working on an application where I need a spinner if the user chooses some values from the spinner then I need to smooth scroll for that particular position and highlight the item of that position . 我正在开发一个需要微调器的应用程序,如果用户从微调器中选择了一些值,则需要平滑滚动该特定位置并突出显示该位置的项目。 I have done something for that I have a method that will highlight the position if the view is visible if the view is not visible it will not highlight.
我已经做了一些事情,我有一个方法,如果视图不可见,它将突出显示位置,如果视图不可见,则将不突出显示。 So, I am facing some issues like this.
所以,我面临着这样的问题。
This is the place where I get the position from the spinner 这是我从微调器获得职位的地方
versesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
final int possition = (int) versesSpinner.getSelectedItemId();
recyclerView.smoothScrollToPosition(possition);
setBackgroundColor(possition);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And this is the method where I highlight the item 这就是我突出显示项目的方法
public void setBackgroundColor(int position) {
for (int i = 0; i < versesList.size(); i++) {
View view = mLayoutManager.findViewByPosition(i);
try {
if (view != null) {
if (i == position)
view.setBackgroundColor(Color.parseColor("#504cbee7"));
else
view.setBackgroundColor(Color.WHITE);
}
} catch (Exception e) {}
}
}
Here the problem is while calling the setBackground()
method before the smooth scroll method reaches that particular item. 这里的问题是在平滑滚动方法到达特定项目之前调用
setBackground()
方法。
The highlighting will be done only if the view is visible and if the view is not null. 仅当视图可见且视图不为null时,才会进行突出显示。
Please tell me if there is any other way is there to achieve this else please tell me how can I find out if the smooth scroll to position has reached the particular item 请告诉我是否有其他方法可以实现这一目标,否则请告诉我如何确定平滑滚动到位置是否已达到特定项目
I hope it make sense. 我希望这是有道理的。
You are trying to change the background of item before it has completed the scrolling. 您试图在项目完成滚动之前更改其背景。 Use a
handler
to schedule that task after some time. 一段时间后,请使用
handler
安排该任务。
recyclerView.postDelayed(new Runnable(){
public void run(){
setBackgroundColor(possition);
}
},1000);
You might consider having a loom at my answer here to check how to highlight the items in your RecyclerView
on demand. 您可以考虑在这里回答我的问题,以查看如何按需突出显示
RecyclerView
的项目。 Now if you want a smooth scroll to the position highlighted just add smoothScrollToPosition(position)
right after calling the notifyDataSetChanged()
function. 现在,如果要平滑滚动到突出显示的位置,只需在调用
notifyDataSetChanged()
函数之后立即添加smoothScrollToPosition(position)
notifyDataSetChanged()
。
So the highLightTheListItems()
function might look like this which I'm referring from my answer there. 因此
highLightTheListItems()
函数可能看起来像这样,我从那里的答案中引用它。
public void highLightTheListItems() {
// Modify your list items.
// Call notifyDataSetChanged in your adapter
yourAdapter.notifyDataSetChanged();
// Now smooth scroll to the position you want.
}
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.