Fragment with swipe-refresh Layout not transitioning - android

I have a fragment with a swipe-refresh layout containing a Recycler view and I added custom transitions and associated it with the fragment through frag.setEnterTransition() before the I started the fragment's transaction. But however it doesn't transition at all. It just abruptly appears, just as it would without transitions.
To narrow-down where I could have gone wrong, I redid it with a LinearLayout instead of swipeRefreshLayout and the fragment transitions as expected.
This is my xml for the fragment.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout_inapp_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rv_inapp_messages_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="#dimen/transactions_space_small"/>
<include layout="#layout/widget_empty_screen"/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
This is how the fragment gets transacted.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fragment.setEnterTransition(bottomGravitySlide);
fragment.setExitTransition(fadeOut);
fragment.setAllowEnterTransitionOverlap(false);
fragment.setAllowReturnTransitionOverlap(false);
}
fragmentTransaction.replace(R.id.vg_full_container, fragment, TAG_INTRODUCTION).commitAllowingStateLoss();
And I'm using API23, so the API check shouldn't be the problem.
Where am I going wrong? What should I do if I want to have swipeRefreshLayout and still be able to transition?

I wanted to programmatically move a floating action button to the top center by a transition within a SwipeRefreshLayout and encountered the same problem.
Encapsulating the SwipeRefreshLayout by a FrameLayout solved my issue.
Before:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some other views -->
<android.support.design.widget.FloatingActionButton
android:id="#+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/add"
android:tint="#android:color/white"
android:layout_margin="#dimen/pad16dp" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
After:
<FrameLayout
android:id="#+id/transitions_container"
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.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some other views -->
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/add"
android:tint="#android:color/white"
android:layout_margin="#dimen/pad16dp" />
</FrameLayout>

Related

Kotlin - Keep button sticky in AppBarLayout$ScrollingViewBehavior (in FragmentContainerView)

I have an issue with FragmentContainerView, it looks like this:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/myFragment"
android:name="com.me.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
And in its fragment, I have a recycler view, and a button:
<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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:gravity="bottom|center" />
However, when I am scrolling, button is also moving up, even though I set up alignParentBottom. I believe the issue is in this line of code:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
which is actually:
<string name="appbar_scrolling_view_behavior" translatable="false">com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior</string>
FragmentContainerView is used in CoordinatorLayout. Note that button is out of SwipeRefreshLayout, so it shouldn't be affected by scrolling the recycler view.
I want to keep it sticky, so it doesn't move when I scroll the screen.
Any ideas?

How to hide BottomNavigationView when recylerview scrolls up

Hello there I want to hide bottom nav when recylerview scrolls up but not when the recylerview is scrolled down and when the recylerview stop I want the bottom nav to appear
also like a slide animation like how a bottom sheet has
I have one main activity in which I have given bottom nav and a fragment container as an Include the fragment container contains all of my fragments
Here is my code
Note if anyone wants more reference of the code please tell me i will
update the question
ok i have used app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior" in bootom nav xml but its not working
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/black"
tools:context=".MainActivity">
<include
android:id="#+id/fragment_container"
layout="#layout/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="#+id/bottom_navigation_view"
layout="#layout/bottom_navigation_view" />
</RelativeLayout>
bottom_nav_view.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.bottomnavigation.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_navigation_view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:background="#drawable/bottom_navigation_style"
android:elevation="5dp"
app:itemIconSize="30dp"
app:itemIconTint="#color/white"
app:itemRippleColor="#android:color/transparent"
app:labelVisibilityMode="unlabeled"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation">
</com.google.android.material.bottomnavigation.BottomNavigationView>
You need to use CoordinatorLayout to achieve this "scroll-hide" feature:
<androidx.coordinatorlayout.widget.CoordinatorLayout //root Layout
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">
<androidx.core.widget.NestedScrollView //or RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/main_bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/white"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
app:menu="#menu/main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Demo: https://youtu.be/3Gpeg6M82n4

FAB not positioning inside CoordinatorLayout with RecyclerView

I am trying to display a list of items with a FAB to add a new item. The current screen is as below.
I have a CoordinatorLayout which has only a RecyclerView and the FAB. My problem is the FAB is not getting anchored to the bottom of the page, but to the bottom of the RecyclerView whose height seems to behave as if its wrap_content, but I've declared as match_parent. The RecylerView has a silver background for illustrative purposes.
<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"
tools:context="com.ae.apps.tripmeter.fragments.expenses.TripsListFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/silver"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layoutManager="LinearLayoutManager"
tools:listitem="#layout/fragment_trip"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
app:elevation="8dp"/>
I can't figure out why I am not able to position the FAB at the bottom of the page.
Try putting the RecyclerView in a 'content' layout file and add it to the main layout via
<include layout="#layout/content_recyclerview" />
EDIT:
The FAB should not be placed in the Fragment if it is to reside at the bottom of the Activity. Remove the FAB from the fragment and add it to the main activity. 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="com.your_company.your_project">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<-- ref. your recycler here -->
<include layout="#layout/content_with_your_list"/>
<-- Put the FAB here -->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
/>
</android.support.design.widget.CoordinatorLayout>
Now in your fragment remove the FAB! It should reside in your main activity with the CoordinateLayout. Use a simple LinearLayout in your fragment.
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_your_main_activity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/silver"
android:layout_margin="#dimen/activity_horizontal_margin"
app:layoutManager="LinearLayoutManager"/>
</LinearLayout>
You will need to make changes were appropriate in order to fit you your project.
Toggle FAB visibility
In order to toggle the visibility of the FAB add the following property to the FloatingActionButton in your Parent Activity.
android:visibility="gone"
and make it visible in code with:
mFab.setVisibility(View.VISIBLE);
or invisible, again with
mFab.setVisibility(View.GONE);
Whenever you need, eg. when a fragment is displayed or removed.
You probably want something like this?
Example
<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.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:src="#android:drawable/ic_input_add"
android:layout_margin="8dp"
app:elevation="8dp"/>
If you want to have some layout under fab, you should use relative layout as root layout. And put my code in this relative layout.
I use the following to anchor the fab at bottom of the recyclerView and it gives me my desired output. Try with this..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Android: How to display two fragments with RecyclerView in one screen

I have two fragments, each with its own respective RecyclerView list.
The RecyclerView from each of the fragments may contain many items. So, scrolling will mostly be necessary.
My problem is, how do I combine and display these two fragments into one screen using ScrollView, but at the same time, making the scrolling of the RecyclerViews behave as normally (non-sticky, follows the entire screen's scroll).
Thanks.
EDIT:
My layout file
<?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="com.easyuni.courserecommender.ResultsActivity">
<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
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/toolbar">
<FrameLayout
android:id="#+id/fragment_container_results_career"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<View
android:id="#+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/fragment_container_results_career"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#color/divider" />
<FrameLayout
android:id="#+id/fragment_container_results_courses"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#333"
app:itemTextColor="#333" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Screenshot:
(Notice the RecyclerView at the top and bottom scrolls individually)
https://drive.google.com/file/d/0B-glHmJbVcwiVU41WVRHU3g2bkE/view
You can do like this one -
In your activity's layout file, you should have two framelayout for containing your two fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/first_fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<FrameLayout
android:id="#+id/second_fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
Then in your Activity's onCreate() method, you can use FragmentManager class to add Fragment to the FrameLayout.
getSupportFragmentManager().beginTransaction().add(R.id.first_fragment_container, new YourFirstFragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.second_fragment_container, new YourSecondFragment()).commit();

Snackbar makes RecyclerView within CoordinatorLayout slide up when it shows

I'm testing the use of Snackbar for the first time and I'm finding a behaviour which I don't understand. My Activity is composed of a Fragment, which is composed of a FrameLayout with a RecyclerView. I'm using Toolbar and Appbar.
When the Snackbar appears it moves the recyclerView up, I don't want it to behave like this, I want the Snackbar to show OVER the RecyclerView without moving it. I can however remove this from the Layout that contains the fragment:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
and then the Snackbar displays as I intend it to but of course my fragment overlaps with the Toolbar. I'm new to CoordinatorLayout so I fail to see how to get the behaviour I want. My full code is below.
My activity:
<? xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/master_coordinator_layout"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:animateLayoutChanges="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/toolbar_home"
layout="#layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/randomizerFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<FrameLayout
android:id="#+id/randomizerScreenFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/randomizeFAB"
android:src="#drawable/ic_play_arrow_white_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
app:backgroundTint="#color/myYellow"
app:fabSize="normal"
app:layout_anchor="#id/randomizerFragment"
android:scaleType="center"
app:layout_anchorGravity="top|right"
android:visibility="invisible" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/resetFAB"
android:src="#drawable/ic_replay_white_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
app:backgroundTint="#color/myOrange"
app:fabSize="mini"
app:layout_anchor="#id/randomizerScreenFragment"
android:scaleType="center"
app:layout_anchorGravity="top|right"
android:visibility="invisible" />
</android.support.design.widget.CoordinatorLayout>
My fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".RandomizerActivity"
android:orientation="vertical"
android:animateLayoutChanges="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/factionsRecyclerView"
android:paddingTop="#dimen/margin_small"
android:layout_width="match_parent"
android:clipToPadding="false"
android:layout_height="wrap_content"/>
</LinearLayout>
I'm using the Snack like thus, fL being the FrameLayout #+id/randomizerFragment.
Snackbar.make(fL, "hey!", Snackbar.LENGTH_SHORT).show();
Any help is welcome.

Categories

Resources