Android SwipeRefreshLayout stops RecycleView from scrolling - android

I have a group horizontal RecycleViews inside a SwipeRefreshLayout and I am having trouble with swiping the Recycleviews on the start of the page as it keeps stopping it and detecting a refresh swipe.
How to stop this from happening?
Or is there a way to trigger refresh only if not swiping?
Also, is this the best way to implement my desired layout?
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:isScrollContainer="false"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block_border"
android:orientation="vertical">
<TextView
android:id="#+id/trending_episodes_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--This is the first affected recycle -->
<android.support.v7.widget.RecyclerView
android:id="#+id/trendingEpisodesRecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="horizontal" />
</LinearLayout>
<!--This is the Second affected recycle -->
<android.support.v7.widget.RecyclerView
android:id="#+id/allNewsRecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/title_top"
android:clipToPadding="false"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/block_border"
android:orientation="vertical">
<TextView
android:id="#+id/moods_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/title_top"
android:textColor="#color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/moodsRecycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

Ok so I solved this problem by disabling the SwipeRefreshLayout when the RecycleView is being touched, and re-enabling it again once the user let go of the RecycleView.
Bellow are the code for that from my program:
MyRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(final RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView,newState);
if (newState != RecyclerView.SCROLL_STATE_IDLE) { // on scroll stop
mSwipeRefreshLayout.setEnabled(false);
}
else
{
mSwipeRefreshLayout.setEnabled(true);
}
}
}

Zaid answer works fine. But in my case I had horizontal recycle views inside adapter, so for me it would be hard to connect adapter with activity recycler.
Easier approach is to create custom VerticalRefreshLayout and use it instead of SwipeRefreshLayout.
Kotlin solution:
class VerticalRefreshLayout(context: Context?, attrs: AttributeSet?) : SwipeRefreshLayout(context!!, attrs) {
private val mTouchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
private var mPrevX = 0f
#SuppressLint("Recycle")
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> mPrevX = MotionEvent.obtain(event).x
MotionEvent.ACTION_MOVE -> {
val eventX = event.x
val xDiff = abs(eventX - mPrevX)
if (xDiff > mTouchSlop) {
return false
}
}
}
return super.onInterceptTouchEvent(event)
}

Related

Part of the content of a CollapsingToolbarLayout is getting sticky on top, when used with viewpager and recyclerview

I am trying to implement a layout like this:
The issue is that when scrolling up the card touches the toolbar and it does not go up anymore like this:
I want it to scroll up till the viewpager to fills the screen ie, at least the rectangle card, should scroll up beyond the toolbar(Even the tabLayout can also scroll up). But I don't want it to stay sticky at the top.
The main layout is here:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/activity_main">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbarCollapse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="190dp"
android:minHeight="190dp"
android:src="#drawable/ic_launcher_foreground"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:behavior_overlapTop="90dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/lin"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/debit_card_item" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_marginTop="40dp"
android:background="?attr/colorPrimary" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The fragments of the pager are inside a NestedScrollView like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
Things that I tried:
I have tried to remove the NestedScrollView above the linear layout and added the app:behavior_overlapTop="90dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior" to the linear layout. but it's still giving me the same output.
Set the height of the view pager statically(gave the same result).
-->>>EDIT<<<
3. I thought of a workaround and I am trying to add custom behavior to my LinearLayout in my mainActivity. so as to reduce the margin-bottom and height of the rectangular card. It is giving good results when I fling and close the collapsing toolbar. But when I hold down and scroll, the screen is flickering as shown below:
My Custom behavior is
class CustomHeaderBehavior : AppBarLayout.ScrollingViewBehavior {
private var mContext: Context
var height = 0
var width = 0
var marginBottom = 0
var firstTime = true
constructor(
context: Context,
attrs: AttributeSet?
) : super(context, attrs) {
mContext = context
}
constructor(
context: Context?,
attrs: AttributeSet?,
mContext: Context
) : super(context, attrs) {
this.mContext = mContext
}
override fun layoutDependsOn(
parent: CoordinatorLayout,
child: View,
dependency: View
): Boolean {
return super.layoutDependsOn(parent, child, dependency)
}
override fun onDependentViewChanged(
parent: CoordinatorLayout,
childP: View,
dependency: View
): Boolean {
val child = childP.findViewById<RelativeLayout>(R.id.cardParent)
val maxScroll: Int = (dependency as AppBarLayout).totalScrollRange
val percentage =
abs(dependency.y) / maxScroll.toFloat()
Log.d("Behavx","D.y "+abs(dependency.y)+ " m s "+maxScroll+ " p "+percentage)
val lp: LinearLayout.LayoutParams =
child.layoutParams as LinearLayout.LayoutParams
if(firstTime){
height = child.height
width = child.width
marginBottom = lp.bottomMargin
firstTime = false
}
Log.d("Behaviour", "P "+ ((1-(percentage))*100).toString() +" H " +height+ " U H " +(height*(((1-(percentage))*100))/100) )
// lp.bottomMargin = ((marginBottom*(((1-(percentage))*100))/100) - ((height*((((percentage))*100))/100))).toInt()
lp.bottomMargin = ((marginBottom*(((1-(percentage))*100))/100)).toInt() //updating margin bottom
lp.height = ((height*(((1-(percentage))*100))/100)).toInt() //updating height
child.layoutParams = lp
child.alpha = 1 - (percentage * 4)
return super.onDependentViewChanged(parent, childP, dependency)
}
companion object {
fun getToolbarHeight(context: Context): Int {
var result = 0
val tv = TypedValue()
if (context.theme.resolveAttribute(R.attr.actionBarSize, tv, true)) {
result = TypedValue.complexToDimensionPixelSize(
tv.data,
context.resources.displayMetrics
)
}
return result
}
}
}
The flicker seems to be happening because the dependency.y is becoming wrong randomly, while I hold down and scroll and update the height of the card(if I only update the margin flicker is not occurring). For some reason controlling the width is happening smoothly. But unfortunately, that's not helping me here.
I also tried to do something similar by adding an offsetChangedListener to the app bar like this:
appBar.addOnOffsetChangedListener(OnOffsetChangedListener { appBarLayout, verticalOffset ->
if (abs(verticalOffset) == appBarLayout.totalScrollRange) {
// Collapsed
cardParent.visibility = View.GONE
} else {
//Expanded
cardParent.visibility = View.VISIBLE
}
})
But I am still getting the flicker
I have added the sample project here:
github link
The custom behavior is pushed to a new branch custom_behaviour
Finally Solved my issue. I added the card inside the collapsing layout like Kalyans answer and added a dummy view and a -ve margin to the card to have the overlap behavior effect like
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbarCollapse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin" />
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000" />
<include
layout="#layout/debit_card_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-90dp"
app:layout_collapseMode="parallax" />
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
I have also pushed my code to the github.
In order to achieve that effect, the card layout should be inside the collapsing toolbar.
Try replacing this:
<ImageView
android:layout_width="match_parent"
android:layout_height="190dp"
android:minHeight="190dp"
android:src="#drawable/ic_launcher_foreground"
app:layout_collapseMode="parallax" />
with:
<include layout="#layout/debit_card_item"
android:layout_width="match_parent"
android:layout_height="190dp"
android:minHeight="190dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="72dp"
app:layout_collapseMode="parallax"/>
and remove <include layout="#layout/debit_card_item" /> in the NestedScrollView.
Hope this helps.

Stop scroll of parent (NestedScrollview) When scrolling Child(RecyclerView)

I need behavior similar to this implementation But NestedScrollView would be the parent and RecyclerView would be the child of would NestedScrollView .
Eg: https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a
I am not sure whether it can be achieved.Tried disabling the of parent(NSV) while child(RV)is scrolling But scroll on child scrolls the entire view including parent.
I'v implemented Marc Knaup solution and all works correct event if NestedScrollView would be the parent and the result was shown below
btw, i put what i'v done
CustomRecycleView
package com.example.nested_scroll_test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.core.view.NestedScrollingParent;
import androidx.recyclerview.widget.RecyclerView;
public class CustomRecycleView extends RecyclerView implements NestedScrollingParent {
private View nestedScrollTarget = null;
private boolean nestedScrollTargetIsBeingDragged = false;
private boolean nestedScrollTargetWasUnableToScroll = false;
private boolean skipsTouchInterception = false;
public CustomRecycleView(Context context) {
super(context);
}
public CustomRecycleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRecycleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean temporarilySkipsInterception = nestedScrollTarget != null;
if (temporarilySkipsInterception) {
// If a descendent view is scrolling we set a flag to temporarily skip our onInterceptTouchEvent implementation
skipsTouchInterception = true;
}
// First dispatch, potentially skipping our onInterceptTouchEvent
boolean handled = super.dispatchTouchEvent(ev);
if (temporarilySkipsInterception) {
skipsTouchInterception = false;
// If the first dispatch yielded no result or we noticed that the descendent view is unable to scroll in the
// direction the user is scrolling, we dispatch once more but without skipping our onInterceptTouchEvent.
// Note that RecyclerView automatically cancels active touches of all its descendents once it starts scrolling
// so we don't have to do that.
if (!handled || nestedScrollTargetWasUnableToScroll) {
handled = super.dispatchTouchEvent(ev);
}
}
return handled;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
return !skipsTouchInterception && super.onInterceptTouchEvent(e);
}
#Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyConsumed != 0) {
// The descendent was actually scrolled, so we won't bother it any longer.
// It will receive all future events until it finished scrolling.
nestedScrollTargetIsBeingDragged = true;
nestedScrollTargetWasUnableToScroll = false;
} else if (dyConsumed == 0 && dyUnconsumed != 0) {
// The descendent tried scrolling in response to touch movements but was not able to do so.
// We remember that in order to allow RecyclerView to take over scrolling.
nestedScrollTargetWasUnableToScroll = true;
if (target.getParent() != null)
target.getParent().requestDisallowInterceptTouchEvent(false);
}
}
#Override
public void onNestedScrollAccepted(View child, View target, int axes) {
if (axes != 0 && View.SCROLL_AXIS_VERTICAL != 0) {
// A descendent started scrolling, so we'll observe it.
nestedScrollTarget = target;
nestedScrollTargetIsBeingDragged = false;
nestedScrollTargetWasUnableToScroll = false;
}
super.onNestedScrollAccepted(child, target, axes);
}
#Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return nestedScrollAxes != 0 && View.SCROLL_AXIS_VERTICAL != 0;
}
#Override
public void onStopNestedScroll(View child) {
nestedScrollTarget = null;
nestedScrollTargetIsBeingDragged = false;
nestedScrollTargetWasUnableToScroll = false;
}
}
content_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Top Section"/>
</LinearLayout>
<com.example.nested_scroll_test.CustomRecycleView
android:id="#+id/rw"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#color/colorPrimary"
android:nestedScrollingEnabled="true"
android:orientation="vertical">
</com.example.nested_scroll_test.CustomRecycleView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="Bottom Section"/>
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
RecycleViewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nsw"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#CCCC"
android:gravity="center"
android:nestedScrollingEnabled="false"
android:orientation="vertical"
android:padding="2dp"
android:textColor="#FFFFFF" />
</androidx.core.widget.NestedScrollView>
#Rockin use overscrollmode bro:)
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:overScrollMode="always"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_white"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/activity_insight_recyclerview_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/_20sdp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/raw_insight" />
post entire thing what you have tried so far.
your xml should be ,
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View > <!-- upper content -->
<!-- set recycler view with with wrap_content -->
<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>
set scrolling behaviour inside onCreateView() / onCreate() method.
requires Api 21+ .
RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);
or
android:nestedScrollingEnabled="false" // inside recycler view in xml file
This is demo for what you want look, In MainActivity.xml file :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="MainActivity">
<android.support.v4.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/top_seller"
android:layout_width="match_parent"
android:layout_height="200sp"
android:background="#color/colorAccent"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
android:src="#drawable/background"
android:scaleType="fitXY"/>
<ImageView
android:id="#+id/top_seller1"
android:layout_width="match_parent"
android:layout_height="200sp"
android:background="#color/colorAccent"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
android:src="#drawable/background"
android:scaleType="fitXY"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="none" />
<ImageView
android:id="#+id/top_seller2"
android:layout_width="match_parent"
android:layout_height="200sp"
android:background="#color/colorAccent"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
android:src="#drawable/background"
android:scaleType="fitXY"/>
<ImageView
android:id="#+id/top_seller3"
android:layout_width="match_parent"
android:layout_height="200sp"
android:background="#color/colorAccent"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
android:src="#drawable/background"
android:scaleType="fitXY"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
In your MainActivity.java class onCreate method:
RecyclerView bestRecyclerView = findViewById(R.id.product_list);
GridLayoutManager mGrid = new GridLayoutManager(this, 2);
bestRecyclerView.setLayoutManager(mGrid);
bestRecyclerView.setHasFixedSize(true);
bestRecyclerView.setNestedScrollingEnabled(false);
// Create ProductAdapter for RecyclerView data
ProductAdapter mAdapter = new ProductAdapter(MainActivity4.this,getProductTestData());
bestRecyclerView.setAdapter(mAdapter);
I hope It'll help you...!
In general simple trick works for me. When attaching OnTouchListener to child View, in onTouch() just disable touch interrupts. Something like this:
RecyclerView rv = findViewById(...);
rv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
v.getParent().requestDisallowInterceptTouchEvent(true);
Have you try subclass the RV and override onInterceptTouchEvent?
override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
when (event!!.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_DOWN -> {
// WE INTERACT WITH THIS RV. PREVENT PARENT TO INTERCEPT
parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
// THIS SEEMS LIKE IT WILL HAVE "DEFAULT" BEHAVIOUR BUT SINCE WE CURRENTLY DRAGGING THE RV THEN IT WONT SCROLL THE PARENT
parent.requestDisallowInterceptTouchEvent(false)
}
}
return false
}

Anchor top content to BottomSheet

I have a FrameLayout (teaserContainer) and BottomSheet (invitesContainer). The FrameLayout is outside (and above) the BottomSheet. I want the FrameLayout shrink and follow the BottomSheet, so the FremeLayout collapses as the BottomSheet expands.
What happens now is that the FrameLayout occupies the whole page because of its android:layout_height="match_parent" but if I set it to android:layout_height="wrap_content" it shows behind the BottomSheet and vertically centred like a FAB would.
I want that when the BottomSheet (invitesContainer) is fully expanded the FrameLayout (teaserContainer) should occupy the rest of the screen up to the toolbar.
All examples of anchoring views to the BottomSheet involve a FAB, so not any help to me here.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".social.friends.FriendsListFragment">
<FrameLayout
android:id="#+id/teaserContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fitsSystemWindows="true"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
app:layout_anchor="#+id/invitesContainer"
app:layout_anchorGravity="top">
<com.myapp.android.common.social.friends.views.FriendsTeaser
android:id="#+id/teaser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
app:friendsTeaserState="EMPTY" />
</FrameLayout>
<LinearLayout
android:id="#+id/invitesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.1"
android:background="#color/body" />
<com.myapp.android.common.social.friends.views.FriendsConnectItem
android:id="#+id/connectFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:friendsConnectType="facebook" />
<com.myapp.android.common.social.friends.views.FriendsConnectItem
android:id="#+id/connectContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:friendsConnectType="contacts" />
<com.myapp.android.common.social.friends.views.FriendsConnectItem
android:id="#+id/share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:friendsConnectType="invite" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
--------------------------------------------------------------------------------------------------------------
Solution:
This is a Kotlin version of Vadim's suggestion
BottomSheetBehavior.from(invitesContainer).setBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
val currentHeight = teaserContainer.height - bottomSheet.height
val bottomSheetShiftDown = currentHeight - bottomSheet.top
teaserContainer.setPadding(
0,
0,
0,
(bottomSheet.height + bottomSheetShiftDown)
)
}
})
My example is like:
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
int currentHeight = teaserContainer.getHeight() - bottomSheet.getHeight();
int bottomSheetShiftDown = currentHeight - bottomSheet.getTop();
rootContainer.setPadding(
0,
0,
0,
(mBottomSheet.getPeekHeight() + bottomSheetShiftDown));
}
and the mBottomSheet - BottomSheetBehavior mBottomSheet = BottomSheetBehavior.from(invitesContainer);
So this will add/remove padding of yours teaserContainer when you pull it down.

How to retain state of views of orientation changes in Android Conductor?

I am currently learning about the Conductor framework for Android and have a bit of a problem or misunderstanding of how it works.
I was under the impression that the method
setRetainViewMode(RetainViewMode.RETAIN_DETACH);
would save the states of the views in the controller. To test the behaviour, I added EditText views, entered a value in it and rotated the screen. I also added 2 views with onclick listeners attached, changing the background color onclick
The result of the test was that the EditText views maintained the state and preserved the entered values. But the 2 views, changed back to their original background color (none).
This is the behaviour of the views regardless on which RetainViewMode is set
I have this simple MainActivity (note: I'm writing in Kotlin):
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var router: Router = Conductor.attachRouter(this, controller_container, savedInstanceState)
if (!router.hasRootController()) {
var t : TestController = TestController()
t.retainViewMode = Controller.RetainViewMode.RETAIN_DETACH
router.setRoot(RouterTransaction.with(t))
}
}
companion object doTask {
fun start(activity : Activity) {
val intent = Intent(activity, MainActivity::class.java)
activity.startActivity(intent)
}
}
}
And here is the the TestController:
class TestController : BaseController() {
var i : Int = 0
var h : Int = 0
override fun onViewBound(view: View) {
view.a.setOnClickListener {
i++
if (i % 2 == 0) {
view.a.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.white))
} else {
view.a.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.turtle_green))
}
}
view.b.setOnClickListener {
h++
if (h % 2 == 0) {
view.b.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.white))
} else {
view.b.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.blue_light))
}
}
}
override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
return inflater.inflate(R.layout.controller_layout_test, container, false)
}
}
And xml layout file controller_layout_test:
<?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">
<requestFocus></requestFocus>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/logo_simple"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="30dp"
android:layout_gravity="center"
android:background="#color/transparent50p"
android:padding="20dp">
<EditText
android:id="#+id/gt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:padding="6dp"
android:background="#color/white_transparent50p"/>
/>
<EditText
android:id="#+id/erergeargf"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:padding="6dp"
android:background="#color/white_transparent50p"/>
<View
android:id="#+id/a"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_gravity="center"></View>
<View
android:id="#+id/b"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:layout_gravity="center"></View>
</LinearLayout>
</ScrollView>
</FrameLayout>
activity_main xml layout looks like this:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="dk.minreklame.minetilbud_v2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
</android.support.design.widget.AppBarLayout>
<com.bluelinelabs.conductor.ChangeHandlerFrameLayout
android:id="#+id/controller_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
As your views have references to the host Activity, they will never be retained across orientation changes. That would cause a memory leak. The docs on RETAIN_DETACH state:
The Controller will retain its reference to its view when detached, but will still release the reference when a config change occurs.

Endless load more recyclerview inside nested scroll android

I have Recyclerview inside the nestedscrollview and I want to scroll the nestedscrollview to the buttom display loading and load more list to recyclerview.
Here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kh.com.iknow.endless.MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Filter"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sort"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
you can do it in two ways :
1- adding header to recyclerview
2- using setOnScrollChangeListener method of NestedScrolview
I prefer the second way :
NestedScrollView nestedSV = (NestedScrollView) findViewById(R.id.nested_sync_scrollview);
if (nestedSV != null) {
nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
String TAG = "nested_sync";
if (scrollY > oldScrollY) {
Log.i(TAG, "Scroll DOWN");
}
if (scrollY < oldScrollY) {
Log.i(TAG, "Scroll UP");
}
if (scrollY == 0) {
Log.i(TAG, "TOP SCROLL");
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
if (!isRecyclerViewWaitingtoLaadData) //check for scroll down
{
if (!loadedAllItems) {
showUnSentData();
}
}
}
}
});
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/off_white_bg"
android:orientation="vertical">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollViewId"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:id="#+id/permiumLAyout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/divider"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible">
<!-- List view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/btn_br_color"
android:padding="3dp">
<TextView
android:id="#+id/primiumGameNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="15dp"
android:gravity="center"
android:maxLines="4"
android:text="Primium Games"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/row_text_color"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_primiumGameList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="false"
android:nestedScrollingEnabled="false"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="3dp" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
</LinearLayout>
I Noted one issue putting my RecyclerView inside the NestedScrollView. I realized that scrolling the content of my RecyclerView slacked.Getting issue in scrolling. And one main issue load more data getting issue for load data inside NestedScrollView.
Solution: use load more event like this
mRecAdapter = new RecyclerAdapter(recylerView,
primiumgameDetailsArrayList, getActivity(), cost);
recylerView.setAdapter(mRecAdapter);
//set load more listener for the RecyclerView adapter
mRecAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore () {
**mRecyclerView.setNestedScrollingEnabled(false);**
if (reqCountStatus.equalsIgnoreCase("true")) {
primiumgameDetailsArrayList.add(null);
mRecAdapter.notifyItemInserted(primiumgameDetailsArrayList.size() - 1);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
primiumgameDetailsArrayList.remove(primiumgameDetailsArrayList.size() - 1);
mRecAdapter.notifyItemRemoved(primiumgameDetailsArrayList.size());
int index = primiumgameDetailsArrayList.size();
primiumGameloadData(mRecyclerView, index);
}
}, 5000);
} else {
mRecyclerView.setNestedScrollingEnabled(false);
Toast.makeText(getActivity().getApplicationContext(),
"Loading data completed", Toast.LENGTH_SHORT).show();
}
}
});
Note:I had to disable the scroll functionality of my RecyclerView with this method mRecyclerView.setNestedScrollingEnabled(false); and enable when load more request for data mRecyclerView.setNestedScrollingEnabled(true)

Categories

Resources