简体   繁体   English

如何通过位置android在回收器视图中突出显示项目

[英]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.

相关问题 如何在Android的回收器视图中滚动到特定位置(不在视图中)的项目? - How to scroll to an item at specific position (that is not in view), in recycler view in android? 如何在“回收者”视图中突出显示选定的项目 - How to Highlight the selected item in Recycler view 如何突出显示所选的Recycler View项目? - how to highlight the selected Item of Recycler View? 如何在回收站视图中更改项目的位置 - How to change position of item in Recycler View Android Espresso如何在Recycler View中单击Recycler View中的项目? - How Android Espresso click an item in Recycler View inside Recycler View? 回收者视图中的Android高亮圆圈 - Android highlight circle in recycler view 如何像Instagram一样在Drag / Touch上突出显示Recycler视图项? - How to Highlight the Recycler view Item on Drag/Touch like Instagram? Recycler view select position border will appear and not selected border not appear - how to make design in Android Studio like highlight position 8817734731788 - Recycler view select position border will appear and not selected border not appear - how to make design in Android Studio like highlight position 如何在android中的位置2的Recycler视图中添加广告? - How to add ads in recycler view at position 2 in android? 如何在Android中找到回收站视图位置? - How to find Recycler view position in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM