My app and the RecyclerView was working fine until I add the second RecyclerView on to the fragment. Since I added the second fragment, I added a ScrollView to the fragment. Then I faced some issue with the scrolling within the recyclerview, it didn't scroll smoothly. After going through a lot of articles online including StackOverflow, I changed the ScrollView with androidx.core.widget.NestedScrollView. This fixed the issue with scrolling but some major issue occured. The loading of items in the RecyclerView took more time, when I hit the searchbutton, that's there on the menu bar at the top, it took long time to expand the search field and some times app show 'not responding' message. Some articles say add .setNestedScrollingEnabled=false and I added it like the following in onCreateView() of the fragment. But I am still facing the issue.
I am using kotlin
binding.rv_home_items.isNestedScrollingEnabled=false
binding.rv_home_categories.isNestedScrollingEnabled=false
Following is the xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorOffWhite"
android:orientation="vertical"
tools:context=".ui.fragments.HomeFragment">
<FrameLayout
android:id="#+id/framelayout_category"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_home_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:paddingBottom="15dp" />
<ImageButton
android:id="#+id/ibutton_show_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="5dp"
android:background="#drawable/ic_show_category" />
</FrameLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/framelayout_category">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.denzcoskun.imageslider.ImageSlider
android:id="#+id/image_slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
app:iss_auto_cycle="true"
app:iss_corner_radius="5"
app:iss_delay="0"
app:iss_error_image="#color/colorDarkGrey"
app:iss_period="2500"
app:iss_placeholder="#color/colorDarkGrey"
app:iss_selected_dot="#drawable/default_selected_dot"
app:iss_unselected_dot="#drawable/default_unselected_dot"
tools:visibility="visible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_home_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/image_slider"
android:layout_marginBottom="50dp" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
I am completely stuck on my project, any help with this is highly appreciated
Edit:
When I launch the app, in a few seconds it's shows a message that the app is not responding.
If you have more than one RecyclerView and even all of them have same scroll direction, seems ConcatAdapter can be preferred solution.
ConcatAdapter enables sequentially combine multiple adapters to a single RecyclerView.
Related
I have viewpager2 with 4 fragments. 3 of them have SwipeRefreshLayout to refresh async task data in particular fragments.
When using SwipeRefreshLayout and viewpager2 the gestures are somehow conflicting. ie. swype down to refresh makes screen so sensitive, that a little move to left or right also makes page screen change and refresh icon is freezing or the processis unfinished.
my goal is to make gestures independent, so for example when i start to swype down SwipeRefreshLayout, then vp2 is disabled so it it is not interfere with SRL.
This was not happening when using standard viewpager with SwipeRefreshLayout, gestures were not conflicting, but I need to use "setUserInputEnabled" in VP2. any idea how to mitigate this behaviour and should i mitigate it at SwipeRefreshLayout level or within viepager2 code?
It looks problem is resolved when I added to my scrollview:
android:nestedScrollingEnabled="true"
Final code of the fragment layout then looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/some_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/activity_background"
tools:context="some_fragment_name">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:nestedScrollingEnabled="true" <<<<------ HERE the change
android:id="#+id/some_id">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sensors_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="some_package_name">
<TextView
android:id="#+id/some_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:color/white"
android:gravity="center"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginTop="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:textSize="20sp" />
...
EDIT: Update as 1.1.0 has been released on July 22, 2020
The problem was due to a bug in SwipeRefreshLayout, which has been resolved by Version 1.1.0. To use it, just upgrade to that version by adding the following line in the dependencies of your Gradle file:
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
See issueTracker for the bug history: [ViewPager2] SwipeRefreshLayout should not ignore requestDisallowInterceptTouchEvent. Note that there's also a workaround described there (extending SwipeRefreshLayout and overriding "requestDisallowInterceptTouchEvent").
If you have RecyclerView in SwipeRefreshLayout you need to wrap RecyclerView in FrameLayout aur any other layout and set nestedScrollingEnabled="true" on Layout not set on RecyclerView.
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/media_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
I've using the Fragment inside the Navigation Drawer.
In Drawer there are 7 more option menus are presents and all other working fine.
Only this fragment tasking 3sec time to show the views.
I've find using debug mode, below this only line taking that much time to execute.
final View view=inflater.inflate(R.layout.fragment_vehicle_inspection,container,false);
Moreover I've using 4 include layout in XML part.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lat_inspection_vendor_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_05dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_inspection_vendor_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableEnd="#drawable/ic_action_edit"
android:gravity="center"
android:maxLines="1"
android:textColor="#color/colorAccent"
android:textSize="#dimen/margin_15sp"
android:textStyle="bold" />
</LinearLayout>
<include
android:id="#+id/include_inspection_function_layout"
layout="#layout/fragment_inspection_function_layout_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lat_inspection_vendor_name_layout"
android:layout_marginTop="#dimen/margin_05dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/scroll_inspection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include_inspection_function_layout"
android:layout_marginTop="#dimen/margin_10dp"
android:focusableInTouchMode="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/fragment_inspection_vendor_details_card" />
<LinearLayout
android:id="#+id/lat_inspection_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/fragment_inspection_vehicle_form_row_card" />
<include layout="#layout/fragment_inspection_vehicle_features_row_card" />
<include layout="#layout/fragment_inspection_vehicle_form_penalty_row_card" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I hope include part is not affect inflate view.
I've using 26- EditText, 26-checkbox and 30-TextViews inside the include layout.
My concern is Inflating too much number of views only taking time.
Please assist me to encounter this problem.
Thanks in advance.
It's because too much view's in your layout file and yes include part affect your inflation process. Try to reduce number of view.
Share you layout design image so i can give you a suggestion.
It's because you are showing some high resolution image or any other background resource so it may happen to take some time to draw view in your fragment
I have this piece of layout or Android which is problematic:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="#+id/influencerMainPhotoImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/photos_list"
android:name="com.company.android.InfluencerFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
app:layoutManager="LinearLayoutManager"
tools:context="com.company.android.InfluencerFragment"
tools:listitem="#layout/fragment_photo"/>
</LinearLayout>
The problem is that the embedded RecyclerView is doing nested scrolling (ie. it scrolls within itself) - I don't want this.
I'd like to the entire screen to scroll as a single piece.
I tried to set "view.setNestedScrollingEnabled(false);" (on the LinearLayout view) but it says it works only for API 21 and above. My project is set to min API 15.
What can I do to make the entire screen scroll as a single piece?
You can replace View with ViewCompat(v4 compat lib):
https://developer.android.com/reference/android/support/v4/view/ViewCompat.html
where you have the setNestedScrollingEnabled(boolean); method.
I'm developing a chat application. Recently I changed the Chat, Activity (no lag) to Fragment (so many lag when resizing) because I wanted to build a flexible UI but now there is my problem : When the keyboard show up or disappear the UI resizing is very laggy.
I tried to changed adjustpan and other inside the manifest -> not working
When I remove the recyclerview from de chat -> it works, no lag when the keyboard show up.
So I guess the problem is here (I'm not sure),
I noticed this error RecyclerView: No adapter attached; skipping layout
I tried to fix it with other post on stackoverflow but no success. The error also appears when I scroll the recyclerview or when the keyboard appears.
When I have lot of messages inside my adapter it's very laggy when the view is resizing.
About the architecture
The MainActivity have one container.
the container can contain fragments : FamilyFragment, SettingFragment, RootChatFragment
RootChatFragment -> building flexible UI (Contact Fragment and ChatFragment) or just Contact Fragment if it's mobile view
Can someone help me ? (sorry for my bad english)
Here a screenshot when I close the keyboard.
UPDATED, FIXED
Using android:layout_weight="value" was the problem but I don't know why.
Old one
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:fillViewport="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.myapp.fragments.SpaceContact"
android:id="#+id/contact_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.myapp.fragments.SpaceChat"
android:id="#+id/chat_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
New one without lag
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:fillViewport="true">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.myapp.fragments.SpaceContact"
android:id="#+id/contact_fragment"
android:layout_width="350dp"
android:layout_height="match_parent" />
<fragment android:name="com.myapp.fragments.SpaceChat"
android:id="#+id/chat_fragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/contact_fragment"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
My RecyclerView contains a list of CardView
xml for MainActivity:
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView>
<android.support.v7.widget.RecyclerView
android:id="#+id/view_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
<FloatingActionButton/>
</android.support.design.widget.CoordinatorLayout>
I use an adapter for the RecyclerView above to contain the Cards.
xml used to inflate ViewHolder inside the adapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout"
android:paddingTop="2dp"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="#android:color/holo_red_light"
card_view:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="3dp"
card_view:contentPadding="7dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/relat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:padding="10dp">
<TextView/>
//...
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
To make the Cards clickable, I tried all solutions in these - two popular posts , but I always have this weird bug:
The list of Cards won't scroll when I start the app for the first time, unless I click on the RecyclerView once. It's as if the RecyclerView is not in focus initially.
Also, if I get rid of all click listeners or similar ways to make the CardView's clickable, and only keep the focusable code in xml:
android:focusable="true"
android:focusableInTouchMode="false"
, then it does scroll right away, but as soon as I add any click (listener) mechanism, or even include "android:clickable="true"" for the ViewHolder, that bug re-emerges.
Please advise. Thank you
You should never nest a RecyclerView inside a ScrollView. Just remove the NestedScrollView and the RecyclerView should take care of its scrolling behavoiur.
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/view_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Turns out the scrolling issue is not related to the RecyclerView. It was due to an open source widget I used which anchored to the RV and somehow interfered with the focusing/scrolling/touch interception. Finally got rid of this bug after days of looking elsewhere..
thank you all the same