My app uses a RecyclerView with a LinearLayoutManager and a LinearSnapHelper.
At the start of my app I want my RecyclerView to immediately scroll to a snap position without any animation.
I know of RecyclerView.smoothScrollToPosition() but that animates the scroll. Any ideas how to achieve this? Thanks! :)
Code
Set up the RecyclerView:
mRecyclerRooms = (RecyclerView) mView.findViewById(R.id.recyclerRooms);
mRoomsLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerRooms.setLayoutManager(mRoomsLayoutManager);
mSnapHelperRooms = new LinearSnapHelper();
mSnapHelperRooms.attachToRecyclerView(mRecyclerRooms);
mRecyclerRooms.addOnScrollListener(mRoomScrollListener);
XML for RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
mRoomScrollListener() is used to calculate the distance to snap via float distanceFromSnap = abs(mSnapHelperRooms.calculateDistanceToFinalSnap(mRecyclerRooms.getLayoutManager(), view)[0]); and then scale the item accordingly. The idea is to make items get bigger and have more alpha when they are near the snap position which should be the center of the RecyclerView.
Use this
recyclerViewList.scrollToPosition(position);
http://www.devexchanges.info/2016/09/android-tip-recyclerview-snapping-with.html?m=1
This might help. But I am worried that Linear snap helper is enough for snapping at center.
Related
When scrolling from left half screen of the mobile scrolling is happening but when i scroll from right half screen scroling is not happening.
This is my xml clode.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/postRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:nestedScrollingEnabled="false">
</androidx.recyclerview.widget.RecyclerView>
My Java code is
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.postRecyclerView.layoutManager = layoutManager
binding.postRecyclerView.setHasFixedSize(false)
enter code here
Finally after so much search i found the solution. Actually i have used Floating button in that page due to that hieght from right side scrolling was not happend in Recyclerview.
I have an Android app using a recyclerView and a LinearLayoutManager. I was under the impression that changing the LinearLayoutManager to GridLayoutManager (see the commented line below and the following line) will simply give me a grid. But nothing changes and I still see the linear list, one item per row, and one column.
recyclerView = view.findViewById(R.id.item_setup_recycler_view);
recyclerView.setHasFixedSize(true);
//recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
adapter = new ItemSetupRecyclerViewAdapter(getActivity(), kids);
recyclerView.setAdapter(adapter);
Can someone please let me know if there are other areas I need to change before I see the grid?
Here is the xml for the recyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/feed_recycle_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:clipToPadding="false"
android:paddingBottom="74dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView8"
app:layout_constraintVertical_bias="0.0">
</androidx.recyclerview.widget.RecyclerView>
Thanks in advance
Use this like this.
In your .xml
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your code.
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
then give this to your recyclerView.
Or you can do with one line:
recyclerView.setLayoutManager( new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);)
you need to add orientation and direction of the list
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2, RecyclerView.VERTICAL, false));
Thanks to the comment from #Công Hải I realized the change I was doing was the only change needed. The issue was I made the change on the wrong recycleView in my app. Once I modified the right recycleView everything worked as expected.
I am using a Recycler view inside a Horizontal Scroll View to display threaded comments like below:
Comment1
Comment1Child1
Comment1Child2
Comment1Child2Child1
Comment1Child3 Comment2
etc.
I have done this with the following XML:
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbarSize="2dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="47dp"
android:minWidth="200dp" />
</HorizontalScrollView>
You'll see I set a min width of 200dp so that the comments never get too small and the deeper down the thread they go the further off screen it will be pushed.
It is displaying perfectly but I am unable to scroll horizontally. I can see the scroll bars but I cannot scroll horizontally.
I have fiddled with disabling Touch and nestedScrollingEnabled in the RV but not sure what the real solution is. Nothing seems to work?
Any help would be much appreciated! Thank you
Remove HorizontalScrollView you can give LinearLayoutManager with horizontal orientation like this
LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
reclyclerView.setLayoutManager(layoutManager);
//recyclerView.setMinimumWidth(200); optional for width if u need
I'm using findLastCompletelyVisibleItemPosition() to determine the last visible item in my RecyclerView.
Here is a code snippet of how I'm setting up my layout:
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
Layout XML:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="#dimen/footer_progress_bar"
android:paddingTop="16dp"
android:scrollbars="vertical" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone" />
<ProgressBar
android:id="#+id/footer_progress_bar"
android:layout_width="#dimen/footer_progress_bar"
android:layout_height="#dimen/footer_progress_bar"
android:layout_gravity="center|bottom"
android:visibility="gone" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
In portrait mode, this works fine and always returns the right position.
However in landscape mode, the position returned is always -1.
My question is:
Does anyone know why this happens?
How I can override this to return the right position?
Or can anyone recommend another solution to get the right position of the last item in landscape?
The "-1" that you are seeing is the RecyclerView.NO_POSITION return code indicating that there
is no completely visible position in your RecyclerView in landscape mode. "Completely visible" means that the entire
view including any decorations and its margins (top/bottom margins for vertical orientation and left/right
for horizontal orientations) is visible. You may see all of your data, but a single pixel could be sneaking off out
of view. Take a look at findLastCompletelyVisibleItemPosition() and its
invocation of findOneVisibleChild() here.
Double check that you have a 100% visible position in your RecyclerView as outlined above. Show margins in developer
mode, etc. Something else more mysterious is going on if you verify that there is at least one completely visible position.
This is happening because there is no item completely visible, this is caused because either there are no items shown at all, or your items are bigger than your screen and therefore are not completely visible.
You should consider using the findLastVisibleItemPosition() method to get the last visible item on the screen, altho this will return the position of the last item that is visible on the screen, even if it's only one pixel of that item. I don't know if this will forfill your requirements, if not, you might wanna create a new question with more context/details about your requirements.
Have you tried initializing it in if (savedInstanceState == null)
if (savedInstanceState == null){
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
} else {
// Do nothing
}
Before setAdapter (Listview/Recyclerview) you use below line its work.
recyclerviewList.setHasFixedSize(true);
recyclerviewList.setNestedScrollingEnabled(false);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerviewList.setLayoutManager(llm);
recyclerviewList.setAdapter( adapter );
I'm developping a Android app, and I got a vertical NestedScrollView, who take all my screen, and inside multiple hoizontal RecyclerView. It's work, but the horizontal scroll is really hard to achieve.
I mean, when I scroll horizontally the gesture is catch by the NestedScrollView, and the view move up/down. I need to focus and do a real horizontal movement to scroll the RecyclerView, it's killing UX...
Here my NestedScrollView :
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/scrollLayout"
android:orientation="vertical" />
</android.support.v4.widget.NestedScrollView>
I'm inflating RecyclerView programmatically, because the number is define in network data, here the inflated layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:textSize="#dimen/secondary_text_size"
android:textAllCaps="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="#dimen/recycler_view_size" />
</LinearLayout>
And how I set my RecyclerView in Java :
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.recycler_view_layout, null, false);
RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(this);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(false);
I tried to change the "velocity" NestedScrollView, but I can't find good advice/post, maybe I don't find the good one. Someone can help ?
Hope you find it useful, as I think you need to implement one of these libraries (on GitHub you would find more):
View Flow for Android: https://github.com/pakerfeldt/android-viewflow
HorizontalListView: https://github.com/MeetMe/Android-HorizontalListView
TwoWayGridView https://github.com/jess-anders/two-way-gridview
If you want to do it with standard Android libraries, RecyclerView might be useful if you want to scroll horizontally
To prove that it has horizontally scrolling support, take a look at:
public boolean canScrollHorizontally ()
Query if horizontal scrolling is currently supported. The default
implementation returns false.
Returns True if this LayoutManager can scroll the current contents horizontally
or
public int computeHorizontalScrollExtent (RecyclerView.State state)
Override this method if you want to support scroll bars.
Read computeHorizontalScrollExtent() for details.
Default implementation returns 0.
From: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html
Check also subclass of RecyclerView called HorizontalGridView
Hope it help.