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
Related
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.
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 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.
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!
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.