Android RecyclerView setScrollPosition() delay issue - android

I have a RecyclerView with 30 items totally, 10 items are visible on the screen. I want to use setScrollPosition() to scroll the RecyclerView
But when I pass a value,
For Example: setScrollPosition(5), the RecyclerView is not getting scrolled by 5 items. But the scrolling initiates when I pass value 10 or above.
What is the reason for that delay?
Is that because the RecyclerView offset is at the lastVisiblePosition?
How to solve this issue?

Let say you have 30 items in your RecyclerView and if you want to scroll to 15thposition you can simply use scrollToPosition() function to scroll to that particular position.
And if you want to scroll by 5 positions and you have used LinearLayoutManager, then you can do this :
LinearLayoutManager llm = (LinearLayoutManager)mRecyclerView.getLayoutManager();
int lastPos = llm.findLastVisibleItemPosition();
mRecyclerView.scrollToPosition(lastPos + 5);

Related

Is there any to let RecycleView's first visible item get focus

There is some code snippet in my project which used RecycleView.
Is there any way to let the first visible item to get the focus rather than the last visible item?
// The picture marked with RED
parentRecycleView.setAdapter(ParentAdapter)
// The picture marked with GREEN
parentRecycleViewViewHolder.populate(xxxx){
childRecycleView.setAdapter(ChildAdapter)
}
Then I scroll to the first item.
parentRecycle.scrollToPosition(0)
As you can see in the capture below: the last visible one got the focus(EditText) auto.
My Question is:
Is there any to let the first visible item to get focus(EditText)?
The image capture
To get the first visible item
First visible child depends on the LayoutManager. If you are using LinearLayoutManager or GridLayoutManager, you can use
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
For example:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may prefer to layout more items than visible for caching.
Now once you have the visible item you can send the focus using requestfocus() function.
And that's it.
Hope it helps.
Thank you!

RecyclerView Scroll to one position

I have one quiz layout in which I am using recyclerview to show questions. I am using LinearLayoutManager for the recyclerview adapter. What I need is that whenever the user selects an option from the given questions, the next question should scroll to one position up (to the top of the layout). I tried to use different ways to get this but the problem is that scrolling occurs but it's not reaching to the top of the layout. It's scrolling in such way that the question and options are visible in the layout screen completely but not at the top it's showing in the down section. Code snippet in short
recyclerView = view.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(context);
recyclerView.scrollToPosition(Qno+1);
// recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, new RecyclerView.State(), (Qno+1));
I tried both scrolltoposition and smoothscrolltoposition, but i am getting the same result.
Attaching the screenshot for the better explanation.
Here what I am getting after clicking any option on Q.5
This is what I am expecting, to load Q.6 in the top of the layout.
I suppose that you have a list of questions (java or Kotlin objects) and you want to scroll to Item with position = 20 (Qno = 20). Then you need to use code snippet I added below:
recyclerView.scrollToPosition(questions.indexOf(question))

Scrolling GridView only on Button Click

I want to achieve the following layout -
In this layout, I want to show all the available items to the user. Pressing right arrow will show the next 6 available items. This should happen via horizontal scrolling type motion.
Since items can be many, I want to use GridView for this because of its recycling optimisation. Optimisation is necessary because every item is a Linear Layout in itself.
Now, I can't use GridView because I want to show next 6 items using the arrows only. Also, it will be good if this happens in smooth scrolling type motion.
Can I make any tweak to Gridview so that I can use arrows to scroll GridView to show next 6 items or Is there any other ViewGroup available to achieve this along with the recycling optimisation.
This example is for listview you can refer for gridview also.
For a SmoothScroll with Scroll duration:
getListView().smoothScrollToPositionFromTop(position,offset,duration);
Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll
Note: From API 11.
listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)
To position the current selection:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);
Or
You can use RecyclerView :
You have to make use of LayoutManager for that. Follow the below steps.
1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager
private LinearLayoutManager mLinearLayoutManager;
2). Initialise the LinearLayoutManager and set that to your RecyclerView
mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);
3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.
mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.
Hope this will help..!!
You could try giving corresponding list input to adapter at each click. And then call the notifydatasetchanged to reflect the changes on the View

Android top pagination

I have a RecyclerView with linear layout manager. There 3 items loaded, current position after first position. I add custom RecyclerView.OnScrollListener and when i scroll down, it loads more item
adapter.updateDays(weeksList.subList(currentItem, items.size()));
adapter.notifyItemRangeChanged(currentItem, currentItem + (items.size() - currentItem));
and i updating in my adapter class list, i don't have problems with down scroll
but, if I scroll up, I use
adapter.updateTopDays(list.subList(0, 5));
adapter.notifyItemRangeChanged(0, 5);
and in my adapter class, i update
this.items.addAll(0,list);
notifyDataSetChanged();
that method cause strange behavior, like some items dissapeared, lagging, how can i fix this?

Scroll to particular span in listview

I want to create view Quran Majeed ie multiple selectable parts of listview item.
I have done this using Clickable span and I have managed to add Clickable functionality but I have a problem. I am implementing reciting on user click and listview should have to scroll during recitation.
I have used
listview.scrollTo(0, onclickView.getY())
But gety() returns 0 every time
Use a recycler view instead and do the following:
LinearLayoutManager layoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager());
int lastVisiblePosition =layoutManager.findLastVisibleItemPosition();
Next do the following:
mRecyclerView.smoothScrollTo(lastVisiblePosition);

Categories

Resources