How to focus RecyclerView top position inside NestedScrollView? - android

Not able t focus the recyclerview top position inside NestedScrollView.
I have the following structure of the xml file.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_alignParentTop="true"
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/cv_comment_post">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardCornerRadius="#dimen/_2dp">
<!-- Some Views of -->
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
When i am notify the recyclerview adapter then it's not focus to the top of RecyclerView using below code
mRecyclerView.scrollToPosition(0);

Related

Android recycler view inside on scrollview [duplicate]

This question already has answers here:
RecyclerView inside ScrollView is not working
(26 answers)
Closed 2 years ago.
i have
MAIN VIEW
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
/>
</ScrollView>
On my template adapter, i have a another recycler adapter for child views
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/title_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="#color/GRAY"
android:text="TextView" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_for_specification_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:nestedScrollingEnabled="false"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
The code work, but when user scroll on android device, the parent scrollView does not allow me to scroll over the recycler view (1)
Use NestedScrollview instead of scrollview and use android:nestedScrollingEnabled="false" for recycler_products recyclerview not in both
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
/>
</androidx.core.widget.NestedScrollView>
If you are not able to scroll it is because of this line
android:nestedScrollingEnabled="false".
Change it to True, so you can enable the scrolling:
android:nestedScrollingEnabled="true".
In you code you have recylerview inside scroll view, this is wrong.
Option 1: If scroll view has not any other child view than remove it and just use recyclerview and also remove android:nestedScrollingEnabled="false
And your Main View code looks like this;
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Option 2: If scroll view has also other child including recyclerview than you should use nestedscrollview like below code:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ImageView
android:id="#+id/sellerProduct"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:src="#drawable/iphone"
android:scaleType="fitXY"
android:contentDescription="#string/app_name" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false
android:id="#+id/productList"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

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

scrollToPosition method not working when recyclerview is placed inside scrollview/nestedScrollView

In my layout, ScrollView is the parent and it has Imageview (160dp height) and a Recyclerview as children. Scenario: there's the image and 10 String item list, visible listitems are 0,1,2,3,4. So Whenever there's a new listitem ,I will update the list and call scrollToPosition method(itemcount -1), but nothing happens. I already have setNestedScrollEnabled(false).
I tried scrollView.fullScroll(DOWN) method, scrollView.scrollTo(0, adapter.itemcount), using this it scrolls to last item of the old list, but not scrolled completely to show added new item. This is my layout
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null">
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="140dp" />
<View
android:id="#+id/grey_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/light_grey"
android:visibility="gone" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/comments_list"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Please help, Thank you!
May be you are using
android:descendantFocusability="blocksDescendants"
inside your Layout.
If not then try to use
android:focusable="true"
android:focudeableInTouchMode="true"
inside your Recyclerview

how to scroll RecyclerView in scrollview

how to scroll all above RecyclerView in scrollview
I have to implement RecyclerView in scrollview show as below code, but not scroll RecyclerView.
please give answer
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/horizontalScrollView"
android:layout_marginTop="10dp">
<RelativeLayout...
<android.support.v7.widget.RecyclerView
android:id="#+id/rvpouch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/textView3">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</ScrollView>
Don't use RecyclerView inside ScrollView. Use NestedScrollView instead of ScrollView.
NestedScrollView is just like ScrollView, but it supports acting
as both a nested scrolling parent and child on both new and old
versions of Android. Nested scrolling is enabled by default.
For Example:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Use attribute android:nestedScrollingEnabled="false" for smooth scrolling.
Use NestedScrollView instead of scroll view and set
recyclerView.setNestedScrollingEnabled(false);
Following code snippet will help you to implement scrolling using ScrollView of RecyclerView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="3dp"
android:scrollbarThumbVertical="#drawable/scrollbar_black">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rlFiltersSearchEvent"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#drawable/action_bar_gradient">
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvListOfEventsMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/fab_margin"
android:layout_below="#+id/rlFiltersSearchEvent"
android:nestedScrollingEnabled="false"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>
Hope it helps

cardview is not wrapping the content inside swipeview

I Want to display an listview inside an Cardview.
This is the Code I used :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_layout_with_fab_list_view_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<ListView
android:id="#+id/list_layout_with_fab_list_view"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:divider="#null"
android:fastScrollEnabled="true"
android:listSelector="#android:color/transparent"
android:outlineProvider="none" />
</android.support.v7.widget.CardView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
If i remove the SwipeView the CardView is wrapping the content. but i need both swipeview as well as CardView.
Is there anything i have done wrong??
You want to wrap a content inside cardview make sure that your card view height should set to height which is able to handle the content inside just give height in numbers and check weather it handles your content or not.Use this code in your layout and check if it works or not and in #dimes/fab_margin you can set the height for your cardview.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="#dimen/fab_margin"
app:cardCornerRadius="2dp"
app:cardElevation="6dp"
android:layout_alignParentBottom="true"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView">
</ListView>
</android.support.v7.widget.CardView>
</RelativeLayout>

Categories

Resources