I have a viewpager hosted in a scrollview. The viewpager hosts fragments with variable height linearlayouts. If I set the layout_width to wrap_content,I find that on some pages the viewpager holds are skewered. Ideally I would like the viewpager to resize dynamically
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<Scrollview
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/container_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/details_playWindow"
layout="#layout/details_play_window" />
<include
android:id="#+id/details_topPanel"
layout="#layout/details_header"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="#+id/details_body"
layout="#layout/details_header_body"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="#+id/relatedMovies"
layout="#layout/layout_stiff"
android:layout_width="match_parent"
android:layout_height="250dp" />
<LinearLayout
android:id="#+id/tv_show_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/view_series_header" />
</LinearLayout>
<!--Setting the height 2000dp is silly but for the mean time -->
<android.support.v4.view.ViewPager
android:id="#+id/download_pager"
android:layout_width="match_parent"
android:layout_height="1800dp" />
</LinearLayout>
</LinearLayout>
</Scrollview>
</RelativeLayout>
I am not sure you can do that using xml layouts only, but here is what I used to create flexibile layouts.
There is a LinearLayout atribute namely android:layout_weight. It is similar as percentage in CSS. What I would do can be roughly put in these steps:
Set view pagers parent Linear layout to be of vertical orientation and height to wrap content.
Set view pager android:layout_height=0dp and android:layout_weight="1" match parent.
More about layout weight here
Related
I have a layout similar to the one shown in image below.
Layout Explained
<ScrollView>
<LinearLayout>
...
</LinearLayout>
<ViewPager>
</ScrollView>
ViewPager has a height equal to the height of screen below ActionBar.
While scrolling, when half of the viewPager is visible.. Vertical scroll of RecylerView of fragment in ViewPager interferes with the main scroll of ScrollView.
I want a solution that makes sure the scroll of RecyclerView inside viewpager is disabled untill the viewpager is fully visible.
Main.xml
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginLeft="#dimen/secondary_margin"
android:layout_marginRight="#dimen/secondary_margin"
android:id="#+id/scroll_view">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/home_deals_slider" />
<include layout="#layout/top_selling_slider" />
...
<TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/fragments_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/fragments_view_pager_tab" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Fragment.xml
<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" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/staggered_grid_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
...
I am trying to divide my fragment UI into two sections, the left half will be a static Image View and the right half will be a scrolling view to show a dynamic list. I have the following layout
`
<android.support.design.widget.AppBarLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/appBarLayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/primaryText">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/primary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:id="#+id/image_preview"
android:layout_below="#id/toolbar"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#50000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar"
android:layout_toEndOf="#id/image_preview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
`
The problem is that I cant get the ScrollView to scroll, even setting the height programmatically has no effect and the scrollview only shows list items that fit the screen
Looks like you misunderstood what ListView is.
What I see is you are trying to use ListView as a list item while ListView is like a LinearLayout with a Scroll.
Take a look at these two links:
Android documentation
Example
I have an activity layout that looks like the following:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The viewpager has two fragments, one with a RecyclerView and one with a LinearLayout.
The fragment with RecyclerView works as expected and the action bar scrolls off screen when the RecyclerView is scrolled.
The other fragment with the LinearLayout isn't displaying as I would like it to however. The LinearLayout is drawn below the TabLayout and extends offscreen. I would like it to be resize to fill the space available below the TabLayout without extending offscreen. The LinearLayout looks like this:
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:id="#+id/station_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/station_detail_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<include
layout="#layout/station_detail_body"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
<TextView
android:id="#+id/text_view_station_detail_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="#string/text_view_station_detail_empty"
android:visibility="gone" />
</LinearLayout>
Thanks for any help, much appreciated!
I think you need to add nested scroll view to your linearlayout.
Alex I thing you have to use two CoordinatorLayout in two fragment and you can include your linear layout inside it .
I´m attempting to use the new Android Design Library´s CoordinatorLayout inside of another layout. The CoordinatorLayout contains a RecyclerView. What I´m attempting to do is to place a simple LinearLayout below the CoordinatorLayout. This layout should be placed below the CoordinatorLayout. This is XML I´m using:
<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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/conversation_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_send_white_36dp" />
</LinearLayout>
</LinearLayout>
As you can see, I´m attempting to wrap the CoordinatorLayout inside another LinearLayout. However, on-screen the second part of the layout doesn´t even render.
That is because the CoordinatorLayout has layout_height="match_parent". That means it takes the whole size of the screen and your LinearLayout is rendered but it is below the CoordinatorLayout and off-screen.
An easy way to fix this would be setting the weight of the CoordinatorLayout to fill the parent layout but leave space necesseary to display the footer LinearLayout. This would be
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
But this is not ideal way so I would suggest leaving the CoordinatorLayout as the root element and place your LinearLayout just bellow the RecyclerView where it belongs. Since your footer LinearLayout has fixed height this can be done easily
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:paddingBottom="150dp" />
<LinearLayout
android:layout_height="150dp"
android:layout_gravity="bottom">
<EditText />
<ImageButton />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
My Layout looks sth like this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
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" >
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:background="#color/black" />
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/newsTabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/newsHeaderKenburn"
android:background="#drawable/background_news_tab"
app:pstsDividerColor="#color/transparent"
app:pstsIndicatorColor="#android:color/white"
app:pstsTextAllCaps="true" />
<android.support.v4.view.ViewPager
android:id="#+id/newsPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/newsTabs" />
</RelativeLayout>
When the Layout gets inflated it does not draw the Fragments of the Viewpager. I think its because ScrollView does not know when its layouted which size to expect, because the ViewPager has a flexible size depending on the fragments.
Is there a way to resize the Scrollview after the fragments are layouted ?