RecyclerView items overlap the rest of my layout - android

I try to create a RecyclerView which has to be in the bottom of my View and I want to be able to scroll the RecyclerView so I added a NestedScrollView.
But the problem is : when the RecyclerView has too many items, I'm not able to scroll and even worse because the items go the top of the screen and overlap with the rest of my layout.
<ConstraintLayout
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="wrap_content">
<unrelated data>...</unrealated data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="#+id/XXX"
tools:layout_editor_absoluteX="0dp">
<LinearLayout
android:id="#+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</ConstraintLayout>

Related

How to stick Linear Layout(with child as recyclerview) in ScrollView at top of screen

How to stick linear layout at top and allow nested scrolling in Recycler view after it get stick to top of screen. Linear layout should stick as header in ScrollView then allow its children nested scrolling in recyclerview
Below is image of how initial layout will look like
After Scroll stick linear layout to top of screen and allow nested scrolling in recyclerview like below image
Below is layout xml
<?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:fillViewport="true"
android:scrollbars="none"
tools:context=".ui.fragment.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/background_gradient_primary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/layout_divider_16dp"
android:orientation="vertical"
android:visibility="gone"
android:showDividers="middle|end">
<include layout="#layout/layout_toolbar_dashboard" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoriesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginVertical="#dimen/margin_16dp"
android:orientation="horizontal"
android:paddingHorizontal="#dimen/margin_16dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="6"
tools:listitem="#layout/adapter_categories_item" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background_light_grey_top_corners">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:gravity="center"
android:layout_marginVertical="#dimen/margin_8dp"
android:orientation="vertical">
<ImageView
android:id="#+id/right_action"
android:layout_width="40dp"
android:layout_height="6dp"
android:src="#drawable/background_line_divider_icon" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/dashboardRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="3"
android:overScrollMode="never"
tools:listitem="#layout/shimmer_category_item"
android:padding="#dimen/margin_8dp"/>
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

How to fix swiperefresh when one view is visible/invisible

I have recyclerview list.
If list is empty recyclerview goes to invisible or gone and another view appears to visible
If list is not empty recyclerview goes to visible.
It works fine, but when we have empty list another view appears I can't swipe to refresh in the right place (look at the screen).
I tried https://github.com/airbnb/epoxy/issues/74 but it hadn't help me.
my xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/vSwipeToRefreshNotification"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</FrameLayout>
Try to change your xml layout to this
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Make RecycleView show items in the middle of the screen, if the items are few

I have programmed a simple linear RecycleView for my new alarm application. Since I am very new to Android programming I have run into such a question: now, all the items I have are shown from top to bottom, even if there are only, say 3 items at all. It doesn't look too good and not convenient also. Is there some way, if there are only few items in the list, to show them in the middle, rather than from top to bottom?
Thank you!!
My current main XML code is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#drawable/background"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="true"
android:scrollbars="vertical"
android:layout_marginTop="27dp"
android:layout_marginBottom="36dp"
android:requiresFadingEdge="vertical|horizontal"
tools:listitem="#layout/alarms_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
You should change the recycleView android:layout_height="match_parent" to
android:layout_height="wrap_content"
then add these lines in your RecycleView.
app:layout_constraintCircleRadius="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
You can center all the recyclerView in the middle
for that change your recyclerView like the xml below
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fadeScrollbars="true"
android:scrollbars="vertical"
android:layout_marginTop="27dp"
android:layout_marginBottom="36dp"
android:requiresFadingEdge="vertical|horizontal"
tools:listitem="#layout/alarms_view" />
and when you add new item your recyclerview expand up to the height of screen
Add layout gravity to parent layout and change recyclerview height to wrap content like this:
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_gravity="center"
android:background="#drawable/background"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:scrollbars="vertical"
android:layout_marginTop="27dp"
android:layout_marginBottom="36dp"
android:requiresFadingEdge="vertical|horizontal"
tools:listitem="#layout/alarms_view" />

(android) How to place a view below other views in constraint layout

This picture is state of layout
I want to place the RecyclerView below the Button
However, as you can see, even with layout_constraintTop_toBottomOf, the RecyclerView is not placed
under the Button
There is a condition, the size of the RecyclerView must be math_parent.
This is because you are using a nested recycler view and you are dynamically adding items, otherwise
the last nested recycler view's item will be cut off and not visible when adding dynamically..
If I set the height of a view to match_parent in the constraint layout, is there any way to place it
below other views?
This is XML file.
activity_write_routine.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".data.DailyRecordDetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppBarOverlay"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/body_part_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P A R T"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<Button
android:id="#+id/add_routine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A D D"
android:backgroundTint="#color/light_green"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/routine_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="#+id/add_routine"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
change the height to 0dp, and constraint the bottom of recyclerview to parent
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/routine_recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_routine"/>

How to make the text above recylcerview scroll when when you scroll recylerview

I have a layout which is have title textview and recylerview below it.
It all works good. But I am trying to make the whole thing scrollable. Meaning that if I scroll down the rcylerview when new items are added, I want the title textview to scroll up (disappears) as if it is part of the recylerview
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:text="Near me"
android:textColor="#color/White"
android:textSize="21sp"
android:textStyle="bold" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvVideos"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
I know that I can fake it so item 1 of the recylerview has a different layout which is the title text but I am hoping there is a better option so I don't have to deal with edge cases of number of items and manipulating positions
you need to wrap both the textview and recycler view in a NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Textview"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
I hope this helps...Cheers

Categories

Resources