Recyclerview notifyItemInserted not updating scrollbar - android

I want to add items to my recyclerview and that my scrollbar is updated accordingly.
Here is my RecyclerView :
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scrollbars="vertical"
android:scrollbarThumbVertical="#color/clear_grey"
android:fadeScrollbars="false"
/>
After adding an element to my dataset, I am calling
recyclerViewAdapter.notifyItemInserted(recyclerViewAdapter.getItemCount() - 1);
The problem is, the scrollbar is not updated : I have to touch the views for it to be updated.
I was using
recyclerViewAdapter.notifyDataSetChanged();
The scrollbar is updated, but I can't use this solution. (I need to be able to select text from the views, which is impossible with notifyDataSetChanged.)
Any ideas on how to update the scrollbar ?

Turns out I had set fixedSize to true :
recyclerView.setHasFixedSize(true);
And thanks to this https://stackoverflow.com/a/40373122/2437227,
I tried without it and it works.

Related

Get Item when scroll in horizontal recyclerview

I have a horizontal Recyclerview with 2 elements
I would like to call a function to load into a data list when I scroll through the elements of the recyclerview.
I would like to load the data of the item located in the center of the screen / Recyclerview
I am trying with onScrolled but I cannot figure out when in the Recyclerview I change the item
the elements could perhaps become even more than 2 (for example 6) so if it were possible I would like a function that is not necessarily only for 2 elements
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_compte"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:divider="#drawable/rectangle_border_bottom"
android:paddingLeft="110dp"
android:paddingRight="110dp"
android:clipToPadding="false"
android:dividerHeight="1dp"
android:isScrollContainer="false"
android:nestedScrollingEnabled="true"
android:orientation="horizontal" />
it's possible?
Thanks
Yes, it is possible. There is a good tutorial here by Nick Rout which perfectly describes your case.

Recyclerview inside NestedScrollView - Scroll to bottom

I have a RecyclerView inside a NestedScrollView:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#dimen/_100sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:background="#color/bright_grey"
></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
My RecyclerView is being filled with items in onCreate()
On a device you would see the first item of the RecyclerView on the very top und would have to scroll down the NestedScrollView in order to see the last item.
Since my items are chat message sorted by the time sent I need the NestedScrollView to be scrolled all the way down so users would see the latest chat message first without having to scroll in the first place.
Any ideas on this?
Given that your RecyclerView is the only child of your NestedScrollView, you would be better off removing the NestedScrollView altogether, and instead applying the fixed height to the RecyclerView. Something like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:background="#color/bright_grey" />
Doing this allows you to have the RecyclerView itself manage scrolling, rather than the parent scroll view. And that allows you to leverage a property of LinearLayoutManager to achieve what you want.
Reverse layout -- setting this will "invert" your list; the first item in your adapter will appear at the bottom of the list, and the default scroll position of the RecyclerView will be to scroll all the way to the bottom.
https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#setReverseLayout(boolean)
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setReverseLayout(true);
If you have the same issue and want to keep the NestedScrollView.
It will work like this.
Handler(Looper.getMainLooper()).postDelayed({
binding.nestedScrollView.smoothScrollTo(
0,
binding.recyclerview.measuredHeight,
500
)
// binding.nestedScrollView.scrollTo(0, binding.recyclerview.measuredHeight)
// binding.nestedScrollView.smoothScrollTo(0, binding.recyclerview.measuredHeight)
}, 50L)
For me, it didn't work without delay.

Reposition scroll in Horizontal Listview using RecyclerView

I have created horizontal ListView using RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recycler_view"
android:scrollbars="horizontal"
android:layout_width="433px"
android:layout_height="76px"
/>
But the problem is I want to reposition the scrollBar, when I try to do that then everything goes wrong. I have tried to add these two lines to RecyclerView:
android:scrollbarStyle="outsideOverlay"
android:paddingBottom="10dp"
If I use those two lines then scrollBar does not go further away from the listview, instead it creates result similar in this image:
It just shrunks the whole listview's height. Does anyone have other ideas on how to reposition scrollBar or maybe I have messed soething up and I do not understand something?

Nested Recycler View Scrolling Issue

I have used Recycler View Inside Nested Scroll View(which is within CoordinatorLayout):
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/sections_recycler_view_linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Within the adapter of above recycler I have added Child Recycler Views programatically using setNestedScrollingEnabled as false.
My Issue is:
When I am scrolling the child recycler, as soon as its scroll is completed the parent recycler scrolls to top, as if I am setting its adapter again. I have not added any code to notify adapter or to scroll to top-most recycler item or to set adapter again. I could not figure out the reason for this strange behavior.
Any help would be highly appreciated.. !!!
Add to your parent Recycler View:
android:descendantFocusability="blocksDescendants"
I was display progress bar in child recycler views until I receive data from server. And once I get data I was populating it. I had used staggered Adapter for child recyclers with row count 2 and hence after data got populated my parent recyler's height was changing.
I used setHasFixedSize(false) for parent recycler view as its height was changing and I have not seen the issue again till now.
Easy,
Add
android:fillViewport="true"
to your NestedScrollview and you will be good to go.

Layout Jerking when RecyclerView inside a ScrollView

Im creating RecyclerView and ListView inside ScrollView and im getting problems with the scroll.. The scroll is Jerking (unable to get smooth scroll), I know its the problem with the RecyclerView inside the ScrollView, because layout is scrolling without any problem when swiping until the ListView exists but once RecyclerView items enter the layout it starts to jerk( only scrolling with the finger, no proper scroll when finger is taken off). Here is the code in the xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lv_home_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorWhite"
android:dividerHeight="0.5dp"
android:visibility="gone"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleView"
android:background="#color/colorWhite">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
Nested views that scroll along the same axis have always been
problematic on Android. Recently Google has added nested scrolling
support. In order to have this on older platform levels, you should
use the views in the support library like NestedScrollView and
RecyclerView.
ListView does not work with wrap_content as its height. You can do
this with RecyclerView if you have the latest version of the
RecyclerView support library. Besides, you are already using RecyclerView in one place, you might as well use them exclusively.
ListView is not really meant to be a "drop down". Perhaps you should consider a Spinner instead.
I have it finally.!!
Just add the following line of code in you class where you are calling the RecyclerView
mRecyclerView = (RecyclerView)tmpView.findViewById(R.id.recycleView);
mRecyclerView.setNestedScrollingEnabled(false);
It works for me!

Categories

Resources