Recycler View loading very slow for large data when inside NestedScrollView - java

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.

Related

Android RecyclerView item dynamic layout

Inside my recycler view item layout I want to add something like horizontal recyclerview layout, because I don't know how many ImageViews I need, that's why I can't create them as a static layout. The number of icons depends on server logic. How can achieve something like the picutre below ?
Thank you in advance!
You Need Add Horizontal Recyclerview Inside The Main Recyclerview.
Your First RecyclerView is Verticle list and inside every index create another RecyclerView It's a Horizontal. so you can display n number of Imageview.
You have to create Recyclerview inside the child layout of your main Recyclerview
Something like
Suppose this is the Main Recyclerview
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:listitem="#layout/item_demo" />
In your child layout, Let's say item_demo you have to add another Recyclerview for the imageView that you want to show in horizontal
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:listitem="#layout/item_child" />
item_child layout contains the Imageview that will display horizontally

RecyclerView inside fragment not scrolling in Android API 22

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.

Access other view with recycler view android studio

As described in below image. I have one layout above the recycler view and one below. both tags are the linear layout. I face two problems.
because the recycler view has inner scroll therefore if items in recycler view increase, the first linear layout stays at the same place but I am expecting it to scroll up while scrolling items.
When items increase in recycler view the second layout get disappear. how to make the layout scroll when recycler view reaches its last item.
Tried android:nestedScrollingEnabled="false" it disables the scroll effect but shows only a few items in the recycler view.
Require solutions: if somehow I can make the recycler views items static and display all the available contents. so the parent layout decides whether scroll or not. if size increase it will scroll otherwise not.
You can add both LinearLayouts to the RecyclerView as header and footer views. The top answer in this question should give you an idea how to make this: RecyclerView header and footer
If your two LinearLayout have fixed size, you can fix them inside a RelativeLayout and then insert your RecyclerView "in between".
<RelativeLayout>
<LinearLayout alignParentTop="true" height="100dp" id="#+id/topLayout"/>
<LinearLayout alignParentBottom="true" height="100dp" id="#+id/bottomLayout"/>
<RecyclerView height="match_parent" above="#id/bottomLayout" below="#id/topLayout"/>
</RelativeLayout>
Alternatively, if your top container is a LinearLayout, you can play with weights (1 for top LinearLayout, 1 for bottom LinearLayout, 3 for RecyclerView)
<LinearLayout orientation="vertical">
<LinearLayout height="0dp" weight="1"/>
<RecyclerView height="0dp" weight="3"/>
<LinearLayout height="0dp" weight="1"/>
</LinearLayout>

Android: Fixed size ScrollView not scrolling/scrolling weird

I have a problem with a fixed size scrollview that i'm trying to make. I've read many questions that are similar (but not equal) to mine here in StackOverflow and other sites and none of the answers have helped me, so I decided to ask my own question.
Basically, i want a fixed size scrollview with different controls inside. The basic one would be a textview of a dynamically changing size inside it. When I change the text of the textview and it is bigger than the scrollview, the scrollbars flash quickly, as they should, indicating that I can scroll but no matter how many times I swipe my finger, it doesn't scroll. Then I tried swiping with two or three fingers and sometimes (only a counted number of times) it scrolls.
I have tried many different approaches to this, like changing the textview to an edittext with focusable = false so it doesn't give the user chance to edit the text; or putting the textview alone in the scrollview, or wrapping it in linearlayouts, relative layouts etc. and it still doesnt scroll.
Below is the code as it is today. This scrollview is inside a vertical linearlayout along with other controls that I'm not putting 'cause of the length, but if someone needs it, I'll put it. I would appreciate very much if someone can point to my problem or help me solving this.
XML:
<ScrollView
android:id="#+id/scrollFifthHorizontalLineDetails"
android:layout_width="304dp"
android:layout_height="133dp"
android:layout_gravity="center"
android:layout_marginTop="5dp" >
<LinearLayout
android:id="#+id/rrrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtlblPlaceDetails"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:background="#drawable/placedescriptionbg"
android:text="#string/null_text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
the line in which I populate the TextView:
((TextView) findViewById(R.id.txtlblPlaceDetails)).setText(mJsonOb.getString("placeDescription"));
I had a similar problem with an Android app I was developing a few months back. I got around the scrolling problem by increasing the layout width for the scroll view. In the editor you can actually drag the RHS of the scroll view to exceed the visible area. I think I set the android:layout_width to about 1024.

Scrolling in a view containing a ListView (Android)

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.

Categories

Resources