RecyclerView Scroll to one position - android

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))

Related

Scroll down when adding item to RecyclerView while keyboard is open

When I add a new list item, with the keyboard still open, the list item appears behind the keyboard and is thus invisible. How do I scroll it automatically down to it becomes visible?
Keep in mind that I have already solved the following:
When opening keyboard scroll the RecyclerView to make the last item visible: Solved
The last remaining issue is when the keyboard is already open. It works fine when I open the keyboard for the first time. It's when I hit "send" that the item is added behind the keyboard and when I want it to scroll up
I hit "send message"
This is what happens. It's hidden behind the keyboard
Set your LayoutManager to reverse layout to have the RecyclerView retain its positioning against its bottom edge instead of the top one:
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
myRecycler.setLayoutManager(layoutManager);
There should be no need to automatically scroll to the last position of your Adapter anymore so, if you previously had code to do so, you should remove it.
You will also have to reverse the order of the data set in your Adapter to match the ordering you originally had (If your data was originally sorted Ascending, then you should change it to Descending).
For your case, you would want to sort by Descending time to have the items arranged by most recent at the bottom and oldest at the top of the RecyclerView.
And, if you're directly adding to the Adapter's data set, you should add to the beginning of the Adapter's list:
myDataList.add(0, myNewItem); // Add to the beginning of the list shifting previous items over
notifyDataSetChanged(); // Notify the adapter to refresh

StaggeredGridLayoutManager is loading one item on left and the rest on the right

I'm trying to use the StaggeredGridLayoutManager but facing a weird issue.
Once I'm opening the fragment with the Recyclerview, item load but isn't placed at the right place. Like I have two columns (A and B) and 4 items.
Item 1 is in A
Item 2-3-4 are in B
But if I refresh the fragment then 3 switch to A and everything is fine.
I don't know what I'm missing.
Here is the code where I define the layoutManager and the rest of the code is obliviously open source.
https://github.com/Martichou/Lionwayt/blob/develop/app/src/main/java/martichou/me/lionwayt/fragments/HomeProfileFragment.kt
Fixed by using this layoutManager
val lm = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
lm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE

Dynamically sized RecyclerView items

I'm attempting to create a RecyclerView which displays a TV Guide. Something like this:
[12:00PM] [12:30PM] [1:00PM] [1:30PM ] [2:00PM] [2:30PM]
[Chan 1] [-show1-] [------show2-----] [----------show3----------]
[Chan 2] [------show1------] [-----show2------] [-----show3-----]
The provided LayoutManagers don't seem to support this type of layout. I've started to write my own LayoutManager, but I've hit a roadblock.
When the LayoutManager adds a view for an item, it only knows the item's position in the dataset. It doesn't have access to the item itself.
This is a problem since the width of each "show" is dependant on the start/end time of the show. Therefore, it seems the LayoutManager would need to be able to see the values of the "show" itself in order to layout the view for it correctly.
How can this type of layout be achieved with RecyclerView?

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

Get RecyclerView to stop scrolling when I add items to the Adapter

How can I stop RecyclerView from scrolling to the bottom of the list whenever I add items?
Even when I insert something at the top of the list and call notifyDataSetChanged or notifyItemInserted the list scrolls to the bottom.
The one feature that works as expected is notifyItemRemoved(index)
I think I was calling notifyRangeInserted() with the entire list and that was scrolling to the end of that range.
If you're having similar issues, just cut the Adapter functionality back to its bare essentials and build up from there.
The problem was I had an item in the list, a ProgressBar to indicate that data was loading, but when the new items loaded I added them to the list, this moved the "focused item," the ProgressBar down and if there were enough items to move it off the screen then the RecyclerView would scroll down to keep it on screen. The problem being 1. I didn't want it to maintain "focus" and 2. The very next thing I did was remove the ProgressBar from the `Adapter.
So the solution is to remove the ProgressBar or other items before adding items earlier positions in the Adapter.

Categories

Resources