Scrolling GridView only on Button Click - android

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

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

How to programmatically snap to position on Recycler view with LinearSnapHelper

I have implemented a horizontal recyclerView with LinearSnapHelper, to implement a UI input that selects a particular configuration. Kinda like the old school number picker/selector or spinner. The item in the center is the selected position.
it works fine and all, but here's the problem. On initial start up, I need to programmatically set the position of the recycler view such that the selected item (the index of which was loaded from disk) is position in the center.
.scrollToPosition() wont work becuase it places the selected item in the begining.
now I know I can do all the math and calculate the x coordinate and manually set it, but thats a lot of redundant work because LinearSnapHelper is already doing this, and I feel like there should be a way to just reuse that logic, but with actually initiating a fling.
I need something like LinearSnapHelper.snapToPosition()
More general solution:
First scroll RecyclerView to make target item visible.
Than, take the object of target View and use SnapHelper to determine
distance for the final snap.
Finally scroll to target position.
NOTE: This works only because programmatically you are scrolling at the exact position & covering the missing distance by exact value using scrollBy instead of doing smooth scrolling
Code snippet:
mRecyclerView.scrollToPosition(selectedPosition);
mRecyclerView.post(() -> {
View view = mLayoutManager.findViewByPosition(selectedPosition);
if (view == null) {
Log.e(WingPickerView.class.getSimpleName(), "Cant find target View for initial Snap");
return;
}
int[] snapDistance = mSnapHelper.calculateDistanceToFinalSnap(mLayoutManager, view);
if (snapDistance[0] != 0 || snapDistance[1] != 0) {
mRecyclerView.scrollBy(snapDistance[0], snapDistance[1]);
}
}
});
Try calling smoothScrollToPosition on the RecyclerView object, and passing the position index (int)
mRecyclerView.smoothScrollToPosition(position);
Worked for me with a LinearLayoutManager and LinearSnapHelper. It animates the initial scroll, but at least snaps the item in position.
This is my first post on the stack, hope it helps :)
I have a recyclerView which I have added padding at the left and right with dummy views in the adapter. So that the first "actual" item can be snapped to.
I couldn't get smoothScrollToPosition(0) to work though for the initial snap. I used the following
recycler.scrollBy(snapHelper.calculateDistanceToFinalSnap(binding.recycler.getLayoutManager(), recycler.getChildAt(1))[0], 0);
Isn't the nicest looking way, but seems to work!

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

Android RecyclerView setScrollPosition() delay issue

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

Categories

Resources