RecyclerView height wrap_content calculation is incorrect - android

I want my RecyclerView to wrap_content. I don't want any scrolling inside RecyclerView, it should adjust to the height of inner children. I wan't my parent ScrollView to scroll the content of my activity.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- scrolls a little bit as RecyclerView goes slightly down beyond the screen -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- still scrolls inside -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Populate RecyclerView:
myAdapter = new MyAdapter();
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(myAdapter);
I use RecyclerView library where wrap_content issues should be fixed:
dependencies {
compile 'com.android.support:recyclerview-v7:25.0.0'
}
Basically RecyclerView height calculation doesn't work well for me here. RecyclerView still has it's own scroll and ScrollView scrolls a little as well. If I try to set some rediculous RecyclerView height to 1000dp so that it's bigger than height of items in total, scrolls work as needed e.g. RecyclerView doesn't scroll and ScrollView scrolls the activity with all RecyclerView items.
So what did I do wrong? :)

All I needed is to use android.support.v4.widget.NestedScrollView instead of a ScrollView.

In that case you need to upgrade your recyclerview gradle version, if you are using 23.0.1 make it 23.1.1 or higher version. In the latest gradle update google has provided the wrap_content property to recyclerview.

Related

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.

RecyclerView inside HorizontalScrollView Android

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

Recyclerview inside Nested Scrollview scroll but does not fast scroll like normal Recyclerview or Nested Scrollview

I am using RecyclerView inside NestedScrollView and it works. But when I use RecyclerView inside LinearLayout or something, it scroll in various speed depending on gesture. The scroll listen to gesture and if I slide up only a bit, then it scroll a little bit while if I slide up really fast, then it scroll really fast. Now my problem is that RecyclerView inside NestedScrollView certainly scroll but fast scroll does not work. However I slide up fast or slow, RecyclerView or NestedScrollView only scroll a little bit.
How can I make my NestedScrollView or RecyclerView inside that scroll view scroll in various speed?
try
recyclerView.setNestedScrollingEnabled(false);
By default setNestedScrollingEnabled works only after API-21.
You can use ViewCompat.setNestedScrollingEnabled(recyclerView, false); to disable nested scrolling for before and after API-21(Lollipop). Link to documentation.
I was working on android 16 where this was not possible to use setNestedSCrollEnabled method,
What I end up doing to stop RecyclerView from handling Scrolls.
Like in LinerLayoutManager i made canScrollHorizontally, canScrollVertically to return false by default.
myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false){
#Override
public boolean canScrollHorizontally() {
return false;
}
#Override
public boolean canScrollVertically() {
return false;
}
});
After several iterations, I came up with a solution.
If you are using RecyclerView, then:
recyclerView.setNestedScrollingEnabled(false);
If you are using LinearLayout inside NestedScrollingView, take the LinearLayout inside a normal ScrollView and then set its scrolling to
scrollView.setNestedScrollingEnabled(false);
android:overScrollMode="never
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
You can use ScrollView with ExtendRecyclerView class that overrides the onMeasure method. That works for me!
#Override
protected void onMeasure(int widthSpec, int heightSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthSpec, expandSpec);
}
recyclerView.setNestedScrollingEnabled(false);
Will be useful sometimes.But it is not advisable for all the times.because it disables view recycling feature in recylcer view.
Alternatives:
Try CollapsiveToolbarLayout with Recycler view.
put other views in collapsiveTollbar layout.
I also met this problem.
And upgrade to 26.1.0 fix it.
In My Case i placed all images in drawable folder insted of drawable-xxxhdpi folder thats why my screen UI is lagging.
This is WAI. The NestedScrollView measures its children with the Spec "Unspecified". The child can grow as much as it wants too.
This essentially equates the height of NSV and RV. So as far as the RV is concerned, it believes that it is completely displayed.
Wrap your RV with an LL and give your RV a height. The LL would not set the measure spec to be UNSPECIFIED so the RV would correctly scroll within its set height of whatever DPs you provide.
The only downside of this method is that you will not be able to do a match parent on your RV.
You should wrap recycler view in any layout like LinearLayout and set RecyclerView size to constant, like 800dp. This will enable smooth scroll and recycler view will still recycler views during scroll.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="800dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>

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!

Android SwipeRefreshLayout on RecyclerView with reverse layout

I'm using a RecyclerView inside a SwipeRefreshLayout in Android.
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mySwipeRefresh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
In this RecyclerView i'm using a LinearLayoutManager with reverse layout and stack from end:
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
myRecyclerView.setLayoutManager(layoutManager);
So, the scrolling content starts from bottom and items are added by adapter from bottom to top.
With this configuration, i'd like to show SwipeRefreshLayout when RecyclerView is at the bottom and user try scrolling more down. Instead, the results is that SwipeRefreshLayout has the standard behavior and is shown from top to bottom animation when user scrolls to top and RecyclerView is at the top.
I didn't find apis to do this, how can i solve this issue?
Thanks
I resolved using this library since SwipeRefreshLayout doesn't support reverse layout
Refresh layout bottom

Categories

Resources