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);
Related
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!
I have a list of days and every day has a list of events. How do I show the list of events within the day item? same as google calendar
example
https://lh3.googleusercontent.com/bWNeO_FeYhGvuJ2UGNTrYVXWk4G3Re21-sOgdjM5V7ZaxehFUJDq7qaevvASs5tnbLNL=w1536-h674-rw
In you day cell, create another recycler view that hold ( vertical item ). Assume that each position have different list of data so collect data from each parent position and set to childAdapter of each position
Example:
Assume that you have a calendar using recycler view which holding data by ParentViewHolder.
In your ParentViewHolder Create Another Recycler View Like ChildRecyclerView.
Create ChildAdapter for ChildRecyclerView that holding vertical item using ChildViewHolder.
Add your necessary action on ChildViewHolder.
1.The day item view in a RecyclerView with a GridLayoutManger
2.The event item view in a RecyclerView with a LinearLayoutManger
3.Set the event RecyclerView's height to wrap_content
4.To make good performance you should make all RecyclerView use a same RecyclerViewPool:
In Activity/Fragment:
// field
var globalViewPool = RecycledViewPool()
// ....
// init view
dayRecyclerView.setRecycledViewPool(globalViewPool)
In EventAdapter:
// onCreateViewHolder
eventRecyclerView.setRecycledViewPool(globalViewPool)
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
If the recyclerview has a list of items and I want to go to a specific row immediately when user enters the UI, so I don't want the user to see it scroll to reach that row. Is it achievable?
Thanks.
You can use method scrollToPositionWithOffset (int position, int offset)
Scroll to the specified adapter position with the given offset from
resolved layout start.
See documentation
Example:
//Scroll to item position 2 with offset 0
RECYCLERVIEW_LAYOUT_MANAGER.scrollToPositionWithOffset(2, 0);
Hope this will help~
I would say it is impossible.
RecyclerView#scrollToPosition causes sort of animation because it does not know the height of all rows initially. Not all items are loaded immediatly but one by one as you scroll down the list.
I use ListView and I solved the issue by calling setSelection(position)
What I want to do is to scroll view after listening a button.
Simply user taps button and view is scrolled to specific id below.
I tried to use scrollto and scrollby passing as an argument reference to object to which I want to scroll with no effect.
Anyone solved this problem yet?
I have spent many hours on this.
Try something like this. It makes sure that the item selected it the one in view:
ListView listView;
int positionSelected = listView.getSelectedItemPosition();
listView.setSelection(positionSelected);