Fragment in Viewpager is not scrolling upwards - android

I have a viewpager consisting of 3 fragments. The first two fragments are simply recyclerViews and they scroll upwards perfectly. However in my third fragment, it's a linear layout and it doesn't scroll upwards:
The first two fragment layouts are just recyclers:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
The third fragment that is not scrolling:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundColor"
android:orientation="vertical">
</LinearLayout>
My viewpager layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/fragprofile_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
//Collapsing toolbar, etc
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/fragprofile_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Why is this happening?

You should replace the root LinearLayout with ScrollView or NestedScrollView.
Note 1: Android layouts are not scrollable by default.
Note 2: ScrollView and NestedScrollView must have only one direct child (should be a layout of some sort such as a LinearLayout or ConstraintLayout)
Hope this helps.

Related

How to make view on top of another view in CoordinatorLayout?

this is my view. I want all all views in mainContainer be on top of the screen. And if views from secondContainer are at the same position they need to be covered by views from mainContainer.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/mainContainer"
layout="#layout/view_home_content"
app:layout_anchorGravity="bottom"/>
<include
android:id="#+id/secondContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Now everything from secondContainer appears on top of views from mainContainer
Oh the solution was easy. I just changed the order of views
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/secondContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>
<include
android:id="#+id/mainContainer"
layout="#layout/view_home_content"
app:layout_anchorGravity="bottom"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

How do I make a Toolbar so that it stays on top of all view and layouts under it?

I currently have a Toolbar and a FrameLayout in the activity. The problem is that during runtime, the FrameLayout expands and covers the toolbar, which I do not want. How should I modify the toolbar so that it stays on top of all views under it?
The layout file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
mapbox:layout_constraintBottom_toTopOf="#id/frameLayout"
mapbox:layout_constraintLeft_toLeftOf="parent"
mapbox:layout_constraintRight_toRightOf="parent"
mapbox:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="600dp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toBottomOf="#id/toolbar"
android:paddingTop="2dp">
// Map here.
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View Pager contents not showing

On my Home Fragment it contains
ScrollView
LinearLayout
RecycleView
LinearLayout
TabsLayout
ViewPager
TextView (Just to test if the height of ViewPager was updated)
When opening the home screen it will show the Recycle View, Tabs Layout and Text View does not include the View Pager (I did confirm that the adapter was executed properly but the content does not show)
I did try to remove the visibility of the RecycleView and after that
the ViewPager Contents shows up.
fragment_home.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"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/item_listing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:clipChildren="false"
tools:listitem="#layout/item_categories_content"/>
<include
layout="#layout/fragment_home_news_events"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
fragment_home_news_events.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/activity_margin"
app:tabSelectedTextColor="#color/colorPrimary"
android:id="#+id/fragment_home_inner_tabs">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/fragment_home_inner_tabs_content">
</androidx.viewpager.widget.ViewPager>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text_label_status"/>
</LinearLayout>
When the RecycleView is visible
When the RecycleView is not visible
You have RecyclerView with include of another layout (fragment_home_news_events) the two had match_parent in height :(
try to fix height to fragment_home_news_events not match_parent.
or
put the height 0dp for both RecyclerView and fragment_home_news_events but with weight 1.

How to make view container above Viewpager "scrollable" (Toolbar is inside Activity, not Fragment)?

I have fragment, which doesn´t cointains toolbar. Toolbar must stay in activity. This fragment must show main view and bellow viewpager with two fragments. Each fragment is specific. First use custom layout, second use adapter. Each has different height. I tried a lot of libs and ideas, but none of them works as it should (it made scroll longer/shorter than it should or it made crazy UI overlays).
I am trying to do UI where it looks that this all is one layout, meaning, it is scrollable from up to bottom. But I can´t figure it out. I was thinking to use CoordinatorLayout, but I have no idea how.
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABCDEFGH" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GHIKJ" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
How to make this as scrollable layout in fragment without using toolbar references?
Update: Yes, here are some pictures, I hope it is more clear now.
Wrap your LinearLayout inside an AppBarLayout. Actually AppBarLayout extends LinearLayout so you could just replace your LinearLayout with it.
Even if your app doesn't have a toolbar or an app bar, the AppBarLayout is the view that CoordinatorLayout coordinates with.
Adding the AppBarLayout should provide the behavior you're looking for.
This root fragment layout seems to working. I am not sure that this is best solution, but it works.
Note: I replaced listview by standard LinearLayout as root container and inflate child layouts (items) there.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
Child fragment (for viewpager):
<?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:id="#+id/childFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>

Adding fragment below toolbar

I have an activity.xml.
As you can see, it's a relative layout. I am hoping that the Frame layout which will contain the fragment, will be below the toolbar.
The fragment is added, for sure. But it's added at the top, covering the toolbar. Just wondering what I am doing wrong.
The website wouldn't let me add code for some reason. It was telling me it's not properly formatted. here is the activity.xml layout.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
>
<!--scroll|snap-->
<android.support.v7.widget.Toolbar
android:id="#+id/appToolBar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolBarHeight"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/appToolBar"/>
</RelativeLayout>
And this is simply what I am doing in the Activity.java file.
getFragmentManager().beginTransaction().add(android.R.id.content, SetupFrag.createInstance(false), SETUP_FRAGMENT).commit();
You're using an existing id, #android:id/content
You should use + to add your own ID, #+id/content, otherwise the fragment is being added to the content view (the level above your activity layout).
You should use a different root Layout.
RelativeLayout will layout its children relatively to each other.
What you want instead is a LinearLayout with a vertical orientation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/appToolBar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolBarHeight"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Categories

Resources