NestedScrollView with viewPager in CoordinatorLayout - android

Below is the code to handle the correct behaviour of my actionBar/toolbar/tabBar and below them the nestedScrollView with a ViewPager(webView). The ViewPager is basically a normal webView.
I am seeking to copy this exact behavior:
Everything works except my ViewPager can't be scrolled vertically at the same time as the toolBar being scrolled. When the viewPager is being scrolled the toolbar is static (in order to make the toolbar scroll aswell I need to manually scroll the toolbar-area but it does not get affected by the viewPager at all). I am not able to scroll both at the same time like the example above!
My code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.activity.FragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:theme="#style/M.Toolbar"
app:popupTheme="#style/M.Toolbar.PopupTheme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:visibility="gone"
android:background="#color/background"
app:background="#color/background"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#android:color/transparent"
android:fillViewport="true"
>
<com.m.ui.util.EdgeScrollViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:visibility="visible"
>
</com.m.ui.util.EdgeScrollViewPager>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
In order to make my viewPager(webView) scrollable I added a new class which extends webView:
public class TouchyWebView extends WebView {
public TouchyWebView(Context context) {
super(context);
}
public TouchyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
My viewPager.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"
android:background="#android:color/white">
<com.m.ui.base.TouchyWebView
android:id="#+id/web_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:visibility="visible" />
<LinearLayout
android:id="#+id/web_loadingview"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="visible"
android:layout_gravity="center"/>
</LinearLayout>
</FrameLayout>
Regards

Related

Webview not working correctly in nestedscrollview

I have some content in NestedScrollView and under this content i have WebView with height wrap_content. When i scroll NestedScrollView scroll on element inside WebView not work.
activity_main.xml
<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"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_launcher_foreground" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_launcher_foreground" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<WebView
android:id="#+id/widgets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
WebView inside NestedScrollView (not work)
WebView without NestedScrollView
public class CustomView extends WebView {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
<com.abc.customer.android.widgets.CustomView
android:id="#+id/my_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Structure looks like
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.view.MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="#drawable/abc_vector_test" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
app:srcCompat="#drawable/abc_vector_test" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="#drawable/abc_vector_test" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
app:srcCompat="#drawable/abc_vector_test" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
in backend
private lateinit var webView: WebView
webView = findViewById(R.id.webview)
webView.settings.setJavaScriptEnabled(true)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
webView.loadUrl("https://www.google.co.in/")

Recycler inside bottom tablyout not showing last row

I have a coordinate layout inside which there is custom ViewPager and bottom TabLayout. Now issue is RecyclerView of one of fragment of ViewPager not scrolling properly. Its hiding the last row.
tablayout.xml
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<CustomViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#color/colorBottomBar"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
CustomViewPager
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (enabled)
return super.onTouchEvent(event);
else
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return enabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isPagingEnabled() {
return enabled;
}
}
ViewPager Fragmet having recyclerview
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/toolbarColor"
android:id="#+id/tool_bar"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:id="#+id/reload"
android:clickable="true"
android:src="#drawable/icon_refresh"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_below="#+id/tool_bar"
android:orientation="horizontal"
android:id="#+id/sort_layout"
android:background="#android:color/white"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"
android:scrollbars="vertical"
android:background="#color/colorBottomBar"
android:layout_below="#+id/sort_layout"/>
</RelativeLayout>
In above picture , you see that last row of RecyclerView is not completely visible.
Put custom pager and tab layout in relative layout and set custom pager above tab layout like this
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CustomViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/#+id/tab_layout
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#color/colorBottomBar"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
In your ā€¨tablayout.xml use this code instead of yours:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<CustomViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#color/colorBottomBar"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

Android Scrollview doesn't work behind navigation drawer

I have an Activity which have navigation drawer that works perfectly but behind the drawer ScrollView doesn't work.
Here is my xml code,
<RelativeLayout 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:paddingTop="4dp"
tools:context="com.bala.beautytipstamil.Content">
<WebView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/include"
android:layout_below="#+id/txtTitle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgContent"
android:layout_width="match_parent"
android:layout_height="150dp" />
<WebView
android:id="#+id/txtContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
<include
android:id="#+id/include"
layout="#layout/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Please help!
Put your ScrollView inside DrawerLayout like below code. just copy and paste it.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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:paddingTop="4dp"
tools:context="com.bala.beautytipstamil.Content">
<WebView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/include"
android:layout_below="#+id/txtTitle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgContent"
android:layout_width="match_parent"
android:layout_height="150dp" />
<WebView
android:id="#+id/txtContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
<include
android:id="#+id/include"
layout="#layout/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
public class VerticalScrollview extends ScrollView {
public VerticalScrollview(Context context) {
super(context);
}
public VerticalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
return false;
default: break;
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return true;
}
}

CollapsingToolbarLayout set animation threshold?

In my application when I start scrolling the layout, the CollapsingToolbarLayout starts almost immediately changing the background to the scrim color I set.
Is there a way to set the value at which the CollapsingToolbar starts to change the background?
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="eu.ericnisoli.ambrosettiap.activities.MeetingActivity"
android:visibility="visible"
android:id="#+id/container">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:fitsSystemWindows="true"
android:layout_height="#dimen/app_bar_height"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="false">
<RelativeLayout
android:id="#+id/relativeLayout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
app:layout_collapseMode="parallax">
<FrameLayout
android:id="#+id/frameLayout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/frameLayout_info">
<ImageView
android:id="#+id/imageView_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/login_bg"
android:scaleType="centerCrop"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/detail_meeting_gradient" />
<LinearLayout
android:id="#+id/linearLayout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingEnd="80dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_gravity="bottom">
<TextView
android:id="#+id/textView_meeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="#string/meeting"
android:textColor="#color/text_grey_1"
android:textSize="#dimen/text_size_14"
android:textAllCaps="true"
/>
<TextView
android:id="#+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="#dimen/text_size_16"
android:text="New Text"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/grey_2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="32dp"
android:paddingBottom="32dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pin_small_icon"
android:layout_marginBottom="4dp"/>
<TextView
android:id="#+id/textView_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_black_1"/>
<TextView
android:id="#+id/textView_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_black_1"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="32dp"
android:paddingBottom="32dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cal_small_icon"
android:layout_marginBottom="4dp"/>
<TextView
android:id="#+id/textView_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_black_1"/>
<TextView
android:id="#+id/textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_black_1"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/event_caps"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_anchor="#+id/app_bar"
app:layout_anchorGravity="bottom"
app:layout_collapseMode="pin">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:layout_anchor="#+id/app_bar"
app:layout_anchorGravity="bottom"
android:background="#color/grey_2"
app:tabTextColor="#color/text_black_1"
app:tabSelectedTextColor="#color/text_black_1"
style="#style/DetailTab"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#drawable/shadow"
android:layout_gravity="bottom"/>
</FrameLayout>
<android.support.v4.view.ViewPager
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginTop="?attr/actionBarSize"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/frameLayout_title"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="48dp"
android:layout_height="48dp"
android:indeterminate="true"
android:layout_gravity="center"
android:visibility="gone"/>
</FrameLayout>
With version 26.1 or newer, you can use the scrimVisibleHeightTrigger attribute to set the height threshold when to show the scrim overlay.
<android.support.design.widget.CollapsingToolbarLayout
app:scrimVisibleHeightTrigger="80dp"
...>
Set the amount of visible height in pixels used to define when to trigger a scrim visibility change.
If the visible height of this view is less than the given value, the scrims will be made visible, otherwise they are hidden.
Dry
After looking into CollapsingToolbarLayout's implementation, you may see, that content scrim is appearing when CollapsingToolbarLayout height gets lower than its minimum height x 2 + "status bar height":
if (mContentScrim != null || mStatusBarScrim != null) {
setScrimsShown(getHeight() + verticalOffset < getScrimTriggerOffset() + insetTop);
}
and here is getScrimTriggerOffset implementation:
final int getScrimTriggerOffset() {
return 2 * ViewCompat.getMinimumHeight(this);
}
Unfortunately setting minHeight property won't work, because this piece of code will overwrite it during onLayout:
if (mToolbarDirectChild == null || mToolbarDirectChild == this) {
setMinimumHeight(getHeightWithMargins(mToolbar));
} else {
setMinimumHeight(getHeightWithMargins(mToolbarDirectChild));
}
Solution
Extend CollapsingToolbarLayout and after onLayout call setMinimumHeight. You may also use the following implementation with setContentScrimHeight method:
import android.content.Context;
import android.support.design.widget.CollapsingToolbarLayout;
import android.util.AttributeSet;
public class MyCollapsingToolbarLayout extends CollapsingToolbarLayout {
private int contentScrimHeight;
public MyCollapsingToolbarLayout(Context context) {
super(context);
}
public MyCollapsingToolbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
super.setMinimumHeight(contentScrimHeight);
}
public int getContentScrimHeight() {
return contentScrimHeight;
}
public void setContentScrimHeight(int contentScrimHeight) {
this.contentScrimHeight = contentScrimHeight;
requestLayout();
}
}
Use setParallaxMultiplier(float) on view's layout parameters with id relativeLayout_title.
setParallaxMultiplier changes the threshold at which parallax animation starts triggering. By default it triggers at half the parent height, equivalent of setting the value of 0.5f.
Source

SwipeRefreshLayout with fixed header

I have the sequent layout:
<com.example.CustomSwipeRefreshLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</com.example.CustomSwipeRefreshLayout>
with fixed header and a scrollable list. CustmoSwipeRefreshLayout is a class that extends SwipeRefreshLayout:
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
private ListView childListView;
public CustomSwipeRefreshLayout(Context context) {
super(context);
}
public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setChildListView(ListView childListView) {
this.childListView = childListView;
}
#Override
public boolean canChildScrollUp() {
return childListView.getFirstVisiblePosition() != 0;
}
(where childListView is the widget with id lv). This code doesn't work as i want. In fact, progress circle appear in two cases:
- i swipe listview to top
- i make swipe gesture on header and listview has first element visible.
I would that progress circle appear both if i swipe listview to top (as works now), and also if i make swipe gesture on header and listview is not to top element
I had the same problem, my solution was set android:clickable="true" inside my parent layout. try it like this:
<com.example.CustomSwipeRefreshLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include" >
<LinearLayout
android:clickable="true" <!-- this line -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</com.example.CustomSwipeRefreshLayout>

Categories

Resources