android layout scrolling with gridview and RecyclerView - android

I have layout with RecyclerView scrolling horizontally and gridview vertically.
but recyclerview dosen't scroll up in activity, leaving gridview scrolling vertically below horizontal recyclerview list.
i want recyclerview to be scrolled horizontally while whole layout vertically scrolling.
content_main.xml
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.test.MainActivity"
tools:showIn="#layout/app_bar_dash_board"
android:orientation="vertical"
android:scrollbars="vertical">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:scrollbars="none" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:scrollbars="none"
android:numColumns="2"
/>
</LinearLayout>
grid_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textAlignment="center"
android:textColor="#android:color/white"
android:background="#55000000"/>
</FrameLayout>

why you are using gridview in recycler view. it will not behave correctly. if you want a grid view you can use recyclerview as a grid view.
set you recycler view layout manger as StaggeredGridLayoutManager. use it like.
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
take a refreance from https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

Your main layout should be scroll view and your recycle view height should not match parent .
you need to set height of grid view dynamically based on number of children.

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>

NestedScrollView does not scroll

I have two list views in nested-scroll view and I want both the list views to scroll as one. This is what I implemented but I do not see any scroll, can someone tell me what is wrong
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gcm_list_item_header_bg"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
>
</ListView>
<ListView
android:id="#+id/lv_disabled_activities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
>
</ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Remove this line from nestedScrollView android:fillViewport="true"
Edit
If you want to see full height of each listView follow this link... and use this method.. to set height of listview based on children listview and recyclerview doesnot wrap content with count

How can i add double ScrollView in XML?

I am currently trying to add a double ScrollView inside my XML.
I am using two different views in my layout - a list and categories.
When a user clicks on a category - the list of videos will become visible and the categories will become invisible.
Currently, the category buttons are scroll-able and when I click a category button, the list of videos does become visible. However, the issue is that the list of videos do not scroll.
Here is my layout code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--this is parent layout where I call ListView-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent01"
android:descendantFocusability="beforeDescendants"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"></ListView>
<ProgressBar
android:id="#+id/nextProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:visibility="gone"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"/>
</RelativeLayout>
</LinearLayout>
<!--This is the categories layout-->
<ScrollView
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_buttons"
android:layout_marginTop="60dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button4"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
for nested scrolling better to use NestedScrollView
NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view.
ScrollView vs NestedScrollView
another thing use RecyclerView insted of Listview
RecyclerView vs. ListView
sample layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
// add here all your controlls
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and dont forgot to set setNestedScrollingEnabled() property in RecyclerView like below code
If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll.
mRecyclerView.setNestedScrollingEnabled(false);
use nested scroll views
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.widget.NestedScrollView>

Bottom Sheet vertical scroll not working

I have two recycler view inside bottomsheet, one is horizontal and other one is vertical.
I am able to scroll horizontally but not able to scroll vertically in second recycler view.
Is there any way to do vertical scroll?
This is work for me use NestedScrollview in xml
Like that
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerViewFeaturedLoc"
android:layout_below="#+id/recyclerViewHorizontal"
android:paddingTop="#dimen/activity_vertical_margin"
/>
<ProgressBar
android:layout_centerInParent="true"
android:id="#+id/home_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then set vertical scroll view in java file
like that
recyclerViewFeaturedLoc.setNestedScrollingEnabled(false);

Render a scroll view and list view on the same screen

I'm having issue with rendering a scroll view, below which I want a list view.
I want a scroll view, inside which I will add image views and text views or may be other UI elements.
Below this scroll view, I want a list view.
But when I do this, either the list view and scroll view overlap, or only the scroll view is rendered.
XML Code: Gist
<?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:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.dell.finalstartup.MainActivity"
tools:showIn="#layout/app_bar_main">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<!-- For pic of the day -->
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layout_PicOfTheDay"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pic of the day example layout"
android:textSize="22sp" />
<ImageView
android:id="#+id/picOfTheDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- Grey Line -->
<View
android:layout_width="match_parent"
android:layout_height="#dimen/gray_line_width"
android:background="#c0c0c0" />
</LinearLayout>
</ScrollView>
<!-- For products -->
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView" />
</RelativeLayout>
Its always an issue to have multiple scroll based views in the same layout. You can use a nested scrollview which is part of the V4 library.
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
For more on layout_behaviour and nested scroll.
Change the Relative layout to Linear layout will solve this issue..!!

Categories

Resources