I noticed a strange behaviour in my application.
My scrollview contains two elements.
a SearchView
a RecyclerView
Those are wrapped by an LinearLayout.
Now I want to add a SwipeRefreshLayout to refresh the data containing in the RecyclerView.
As soon as I do that the ScrollView ignores the SearchView and only scrolls through the RecyclerView.
Here is the XML:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:layout_marginTop="45dp"
android:queryHint="Search Here" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
It should scroll the SearchView and the RecyclerView at the same time.
Why does it stop working as soon as I add a SwipeRefreshLayout and how can I fix that?
I solve this issue by wrapping the NestedScrollView inside of the SwipeRefreshLayout.
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:iconifiedByDefault="false"
android:queryHint="Search Here" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Related
I am new to android and trying to make a profile section of a social media app. The profile section is a fragment and inside this I wanted to have a CardView to hold user's information and then a RecyclerView to show user's posts. I have tried implementing this using a ScrollView and putting insinde the cardView and RecyclerView. I wanted both cardView and RecyclerView to be scroll as one, but currently the cardView is at its place and only elements in recyclerView are scrolling.
EDIT
NestedScrollView kind of worked for me. But I also wanted overScrollMode to work fine while scrolling to the end.
<ScrollView 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"
tools:context=".ui.profile.ProfileFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
style="?attr/materialCardViewFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<androidx.cardview.widget.CardView
android:layout_width="120dp"
android:layout_height="120dp"
app:cardCornerRadius="100dp">
<ImageView
android:id="#+id/profileDisplayPicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/user_profile_picture"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/profileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
<TextView
android:id="#+id/profileCollegeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/profileLinkedinBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="10dp"
android:contentDescription="#string/linkedin"
android:src="#drawable/ic_linkedin" />
<ImageView
android:id="#+id/profileMailBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="10dp"
android:contentDescription="#string/mail"
android:src="#drawable/ic_gmail" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/profileRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
I was getting started with android app development and my requirement is to add a list with some headers in it. I managed to implement this using a combinations of textView and listView. Following is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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:animateLayoutChanges="true"
android:fillViewport="true"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header 1" />
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sports_array" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header 2" />
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sports_array" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header 3" />
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sports_array" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header 4" />
<ListView
android:id="#+id/list4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/sports_array" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
And sports_array is equal to:
<string-array name="sports_array">
<item>Shuttle Badminton</item>
<item>Tennis</item>
<item>FootBall</item>
<item>Basket Ball</item>
<item>Table Tennis</item>
<item>Chess</item>
<item>Hockey</item>
</string-array>
Now the problem is that it loads all the lists on the UI but it doesn't scroll down to make the elements visible that are not displayed.
Solved this by using custom listView that are not scrollable.
Ref: Non-scrollable ListView inside ScrollView
Why is this not working?
It's a problem with the listview.
I've tried in many ways and ending up with the button not showing
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.notes.NotesFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_baseline_add_24" />
</LinearLayout>
</ScrollView>
</FrameLayout>```
The first problem is that you cannot put a ListView inside a ScrollView, ListView itself is scrollable and the second problem it's with the margin if you use a bottom menu, I also encountered this problem. Try this one
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.notes.NotesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:src="#drawable/ic_baseline_add_24" />
</RelativeLayout>
</FrameLayout>```
So I have recyclerView with height wrap_content as I understand this means that recyclerView should expand and collapse whenever I add items to it or remove, but for some reason in NestedScrollView it doesn't behave like that when I add new Objects to it, it stays same size as it was any ideas what could be wrong?
Tried adding
android:fillViewport="true"
But no reactions as far as I understand it could be because RecyclerView not getting updated when children of it changes, because when I restart app it collapses or expands to proper size.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/icons"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested"
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/empty_group_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_text_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/no_group_first"
android:textColor="#color/secondary_text"
android:textSize="17sp" />
<ImageView
android:id="#+id/add_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/empty_text_first"
android:src="#drawable/ic_add_group_gry" />
<TextView
android:id="#+id/empty_text_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/add_icon"
android:text="#string/no_group_second"
android:textColor="#color/secondary_text"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
<TextView
android:textSize="14sp"
android:layout_marginStart="6dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favorite groups"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/favorite_group_list"
android:nestedScrollingEnabled="true"
android:layout_width="match_parent"
android:layout_height="156dp"
android:layout_gravity="center"
android:background="#color/icons"
android:padding="6dp">
</android.support.v7.widget.RecyclerView>
<TextView
android:textSize="14sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginStart="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Groups"/>
<android.support.v7.widget.RecyclerView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/group_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:nestedScrollingEnabled="false"
android:padding="6dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_new_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="#drawable/ic_playlist_add_48px"
app:behavior_autoHide="true"
app:layout_anchor="#id/main_holder"
app:layout_anchorGravity="bottom|right|end" />
Hi kosas give wrap _content for recycler view and while setting layout
manager follow this
linearLayoutManager = new LinearLayoutManager(getActivity()) {
#Override
public boolean canScrollVertically() {
return false;
}
};
Probably the problem is related with the adapter's layout. Be sure to put some fixed value or wrap_content on the adapter's layout height. If it's match_parent it won't work as expected because each row will fill all the RecyclerView
I have implemented an activity with two slides, the slides content is a fragment with RecyclerView that loads data from my server and creates a GridLayout with images (like in image gallery).
I have added the behavior of scrolling so when you scroll you don't see the toolbar.
My problem is that when the activity starts with the fragment the toolbar is half hidden and when i continue to scroll down and the toolbar is completely hidden the CoordinatorLayout from under is exposed so i get this thing:
as you can see the light green is the CoordinatorLayout and the red is the ViewPager and what i want to achieve is that when you scroll more you will only see the red background and never the green one.
My activity_main.xml
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/backgroundGray"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/appGreen"
app:layout_scrollFlags="scroll|enterAlways">
<com.apps.haver.getout.libs.view.CustomTextView
android:id="#+id/actionBarTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Get Out"
android:textColor="#color/white"
android:textSize="50sp"
app:fontName="Peterbuilt.ttf" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#565654"
android:orientation="vertical"
android:scrollbars="none">
<com.apps.haver.getout.libs.slider.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/normal_row"
android:background="#color/appGreen"
android:elevation="2dp" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#e90f0f" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
And my fragment.xml
<FrameLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.apps.haver.getout.FriendsFragment">
<com.apps.haver.getout.libs.view.AutofitRecyclerView
android:id="#+id/rvPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:columnWidth="102dp"
android:fitsSystemWindows="true"
android:scrollbars="vertical" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pressToRetryPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone">
<TextView
android:id="#+id/textPressToRetry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/press_to_retry"
android:textColor="#color/black"
android:textSize="16sp" />
</RelativeLayout>
<include
android:id="#+id/loadingPanel"
layout="#layout/loading_template"
android:layout_width="match_parent"
android:layout_height="match_parent" />
How can I make the toolbar to hide when I am scrolling but still see the RecyclerView content on my entire screen?
Thanks...