I created a RecyclerView in a ScrollView inside a Fragment to display multiple notifications. On Android API 28 it's working perfectly, but on API 22 it's not scrolling, unless I close the fragment and open it again, then it shows the RecyclerView in the new position (as if I scrolled, it doesn't scroll while the fragment is shown).
I tried:
1- Changing the ScrollView to NestedScrollView
2- Removing the ScrollView completely and working with the RecyclerView only
3- Removing the onClickListeners of the RecyclerView elements
Here is the code for the fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/common_google_signin_btn_text_dark_default"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:padding="20dp"
android:id="#+id/my_recycler_view_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Edit: I removed the scroll layout, but the problem still persists. I also tried it and it works perfectly on android API 23 and later. The scrolling only stops working on android API 22 and earlier.
I just tried leaving the recycler view on its own, and it's still not scrolling.
Turns out the problem was in another file where I used the fragment, there was an AppBarLayout there which prevented the scrolling in older APIs for some reason.
Removed the AppBarLayout and it worked fine.
Edit: I found a better solution that didn't require removing AppBarLayout
Instead of hiding and unhiding the fragment normally, I used setVisibility(GONE/VISIVILE) and it also somehow worked.
Related
I have a collapsing toolbar within an appbar layout. It contains a view pager as it's collapsing content and a pinned action toolbar. The activity also has a nested scroll view below the collapsing toolbar. The nested scroll view contains a google map fragment.
On start of the activity, the collapsing toolbar collapses automatically in some of the cases without programmatically collapsing it.
I have tried a combination of different scroll flags for my view with scroll|exitUntilCollapsed|enterAlways.
I have also tried not showing the google map fragment but I can still reproduce it.
How can I stop it from collapsing automatically? Any help would be appreciated!
I know its late but still..
The reason collapsing toolbar automatically collapses on actvity start is because there may a view in <include layout="#layout/content_scrolling" /> like edittext which gain its focus when activity is started..
To overcome this issue you can add the tag
android:focusableInTouchMode="true"
to or
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
xmlns:android="http://schemas.android.com/apk/res/android">
Hope it helps..:)
Happy Coding..:)
I have added RecyclerView inside my NestedScrollView. Basically I want RecyclerView to scroll with other Views. The problem that I am facing is that for a small set of data, it is working fine, but for a large set of data(200 entries) whenever I launch the activity, it freezes for about about 3-5 seconds and then loads. I removed the NestedScrollView and it is working flawlessly, but it doesn't provide me the behaviour I want.
(For extra info, I am loading the data from SQLite database. There is no problem in scrolling, as it is smooth. The only problem is the activity is freezing for a while)
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<... Some other Views ...>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This case of RecyclerView inside NestedScrollView.
RecyclerView is calling onCreateViewHolder() times equal to your data size.
If data has 200 items, it freezes for onCreateViewHolder() to be called 200 times.
The problem as said above is because RecyclerView as a child or subChild in NestedScrollView measures its height as infinitive when you use WRAP_CONTENT or MATCH_PARENT for height of RecyclerView.
one solution that solved this problem for me was setting the RecyclerView Height to a fixed size. you could set height to a dp value, or you could set it to a pixel value matching devices height if your requirements needs a vertical infinitive RecyclerView.
here is a snippet for setting the recyclerView size in kotlin
val params = recyclerView.layoutParams
params.apply {
width = context.resources.displayMetrics.widthPixels
height = context.resources.displayMetrics.heightPixels
}
recyclerView.layoutParams = params
I faced the same problem!
The solution was to change NestedScrollView to SwipeRefreshLayout.
add this for enable/disable ToolBar Scrolling:
ViewCompat.setNestedScrollingEnabled(recyclerView, true);
As said by Nancy , recyclerview.setNestedScrollingEnabled(false); will solve scroll stuck issue. i too faced this type of issue and solved by false the NestedScroll.
I have a ListView and a SwipeRefreshLayout. Everything is working fine, I can scroll up/down and on the top of the list the refreshing is working too.
But if I select an item from the listview my application show me another fragment. After that I would like to go back to the listview without scroll to the top.
I solved this with save/restore listview state, but if I dont wait 1-2 seconds after I go back to listview and I would like to scroll up the swipe refresh is showing but not srolling up.
If I wait a little bit before I scroll up, everything is ok.
On Android 4.4.2 its not problem. On Android 5.0.2 and 6.0.0 its problem.
Use this xml code for ListView with Swipe Refresh
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Your description isn't enough.I think it's your listview and swipelayout are reset,such as setAdapter, when return from fragment.
I have some TextViews on my app, I don't know why but the android:gravity attribute is not centering the text content where it should be on devices running the API 18+ (4.3+).
There is the code I use on my custom TextView, this is a child of RelativeLayout:
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double"
android:layout_toLeftOf="#+id/seekbar"
android:gravity="center_vertical|left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
This code should take the edges of this TextView and align it to the top and bottom and put it to the Left of the SeekBar, this is working, but the TextView gets big, so with android:gravity I center the text to the center and left from it self. It works, but I don't know why, the text is not centered at center|left on devices running android 4.3 and 4.4. This issue can be reproduced on real devices and as well on the layout preview (Graphic layout) of Eclipse.
There is any changes made on API 18+ or on android:gravity that I'm missing?
PS: I'm targetting the API 19 on the project and on AndroidManifest.xml
PS2: My TextView is custom just to set an external font.tff
This is how it looks like on API 17-
This is how it looks like on API 18+
Thanks in advance!
= UPTADATE =
On my Manifest, I changed the android:targetSdkVersion to 17 instead of 19 and the problem disappeared, but this is a "trick", not a solution, what can I do since it could be an issue from the API ? And yes, I have the latest version of the API 18 and 19 (today, 01/30/2014).
This appears to be a known issue in API 18+:
https://code.google.com/p/android/issues/detail?id=59368
https://code.google.com/p/android/issues/detail?id=59700
The problem seems to occur when a TextView is part of a scrollable container (e.g. ListView), making the view ignore the vertical gravity for some reason (some sources suggest this has to do with the TextView being the child of a RelativeLayout, though it's been my experience that this can happen even when no such layout is involved).
A possible workaround (albeit not a particularly elegant one), would be to wrap the TextView in a LinearLayout. You can then use "layout_gravity" on the TextView to center it inside the LinearLayout, instead of relying on "gravity" (just make sure to wrap_content so the text itself is properly centered).
E.g., in your example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_toLeftOf="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double" >
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
</LinearLayout>
This method does have the disadvantage of adding an otherwise-unnecessary level to your view hierarchy, but it currently seems to be the only way around this (other than reverting to an earlier API level).
Also see similar question at:
Android sdk 18 TextView gravity doesn't work vertically
Your textview height is "wrap_content", which means the height of the textview will be the same as the height of the text. If you change the background of the textview to black, it might be easier to see the bounds of the view. I'm guessing you'll find that the textview doesn't have as much height as you expect.
Try setting the height of the textview to match_parent. You can wrap the textview inside another view if needed and modify its height as appropriate.
In one of my activities I am showing certain information and at the end I have a ListView.
So my layout looks a bit like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
>
...
<ListView android:id="#android:id/android:list" android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
When I run the application in the emulator I see that the information before the list view is always shown in the screen and there is a vertical scroll for the ListView only.
Is there a way to change this scrolling behaviour ?. What I would like to have is a vertical scroll for all the information in the screen, not only at the level of the ListView.
I tried wrapping the LinearLayout with a ScrollView, with different combinations of the android:layout_height attribute for all the views involved but I did not get the effect that I was looking for. Besides, some people say that it is a pretty bad idea to wrap a ListView with a ScrollView :
Android ScrollView layout problem
Scrolling with Multiple ListViews for Android
Thanks for any other ideas.
I've not tried this, but you might want to look at you tube video:
Google I/O 2010 - The world of ListView
http://www.youtube.com/watch?v=wDBM6wVEO70
Starting at time 27:11, it talks about how to make info above and/or below a ListView scroll with the ListView. It refers to scrolling headers and footers, but it does say you can put anything you want in them.