click here to view image
how can i place the as floating button as shown image position. Below is my XML code
<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.v7.widget.RecyclerView
android:id="#+id/rec_summary_recycler_view_inflow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="5dp"
android:scrollbars="vertical"
android:scrollbarThumbVertical="#drawable/scr"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:backgroundTint="#color/colorPrimary"
android:src="#drawable/down_arrow"
android:layout_margin="25dp"
app:fabSize="mini"
android:onClick="Scroll" />
Try to add this lines in fab. change value according to your needs. this lines will sets a fab to right bottom of screen.
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end|bottom"
use this to hide show the fab on recyclerview scroll
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0 && fab.isShown())
{
fab.hide();
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
fab.show();
}
super.onScrollStateChanged(recyclerView, newState);
}
});
Give an id to your content_layout.xml (id:layout_content) and use app:layout_anchor="#id/layout_content" in your fab layout.
<include layout="#layout/content_order_list"
android:id="#+id/layout_content"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/layout_content"
app:layout_anchorGravity="bottom|end"
android:layout_marginBottom="90dp"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
Related
I'm trying to implement hide/show SearchView on scroll feature inside a SwipeRefreshLayout.
I want to show SearchView when user scroll to the bottom and hide - when scroll to top.
Also i want to keep swipe refresh behaviour.
Tried many examples, but haven't seen close results.
My xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent">
<include layout="#layout/loading_bar"/>
<include layout="#layout/network_error"/>
<include layout="#layout/empty_materials_coinlist"/>
<RelativeLayout
android:id="#+id/coinlist_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/coinlist_item_bg">
<RelativeLayout
android:id="#+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="exitUntilCollapsed">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toolbar_title"
android:textColor="#color/toolbar_text_color"
android:textSize="18sp"
android:typeface="sans"
android:layout_gravity="center" />
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/custom_toolbar">
<android.widget.SearchView
android:id="#+id/sv_coin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:iconifiedByDefault="false"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/currenciesRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/coinlist_item_bg"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Try this
myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
//check for scroll down
int itemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
if(itemPosition==list.size()){// your last item your recyclerview
// hide your SearchView here
}
} else {
//check for scroll up
int itemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if(itemPosition==0){
// show your SearchView here
}
}
}
});
I have a Recyclerview inside Scrollview, I want to store item details of Recyclerview where scroll stopped.
I have a xml like,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
android:layout_marginRight="2dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I have added ScrollListener for recyclerview. But As I'm using parent Scrollview for the activity, recyclerview's ScrollListener is not getting called.
I tried changing parent scrollview to NestedScrollView then recyclerview is scrolling as a one item at once but i want scroll to be freely. Like scrolling our contacts.
Issue : When I scroll, Parent ScrollView getting scrolled and I don't have any control on my recyclerview. So It's difficult to figure out which item of recyclerview is visible once scroll stopped.
Basically I want to code without changing my XML.
I want to store item details of Recyclerview where scroll stopped.
When scroll stops, scroll state transitions into idle state. However, idle state is the state before and after scroll. Therefore you are looking for a transition into idle state after settling state.
Do it like this:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
boolean scrolled;
int[] displayedPositions;
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
scrolled = true;
} else if (newState == RecyclerView.SCROLL_STATE_IDLE && scrolled) {
scrolled = false;
int[] into = new int[//number of spans];
displayedPositions = ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPositions(into)
}
}
}
private int overallXScrol = 0;
//...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
Log.i("check","overall->" + overallXScroll);
}
});
try NestedScrollView instead scrollview like below
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
android:layout_marginRight="2dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I found the solution. Just we need to change layout. I'm using CoordinatorLayout as a parent view. And Now I'm able to use onScrollListener of recyclerview.
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginEnd="2dp"
android:visibility="gone"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
The code for initializing the RecyclerView
private void initRecyclerView() {
layoutManager = new GridLayoutManager(this, spanCount);
mRecyclerRV.setLayoutManager(layoutManager);
mRecyclerRV.addItemDecoration(new GridSpacingItemDecoration(spanCount, (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, spaceInDp, getResources().getDisplayMetrics()) + 1), false));
mRecyclerRV.setNestedScrollingEnabled(false);
mRecyclerRV.setAdapter(mAdapter);
mRecyclerRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(-1)
&& RecyclerView.SCROLL_STATE_SETTLING == newState
&& mScrollVerticalDirection
&& mIsAppBarCollapsed) {
mAppBarLayout.setExpanded(true);
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mScrollVerticalDirection = (dy < 0);
}
});
mRecyclerRV.postDelayed(new Runnable() {
#Override
public void run() {
RecyclerView.ViewHolder holder = mRecyclerRV.findViewHolderForAdapterPosition(1);
if (null != holder) {
holder.itemView.performClick();
}
}
}, 1000);
}
The layout file is here, and uses CoordinatorLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_local_album_coordinator"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#color/colorPageBg">
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_local_album_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom_bg"
android:theme="#style/Wemeet.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/activity_local_album_collapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/dp2"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<android.support.v7.widget.Toolbar
android:id="#+id/local_album_toolbar"
style="#style/WemeetTitlebar"
android:layout_marginStart="-10dp"
android:titleTextColor="#color/colorText33"
app:layout_scrollFlags="scroll"
android:theme="#style/Wemeet.Toolbar">
<TextView
android:id="#+id/comm_toolbar_title"
style="#style/WemeetTitlebar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="相机胶卷"
android:gravity="center"
android:singleLine="true" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginTop="#dimen/dp48"
android:layout_height="350dp"
app:layout_scrollFlags="scroll">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/colorDivider"
android:id="#+id/fg_top_border" />
<com.weiguan.wemeet.basecomm.ui.widget.WemeetVideoView
android:visibility="gone"
android:layout_gravity="center"
android:id="#+id/activity_local_album_preview_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.weiguan.wemeet.basecomm.ui.widget.WemeetVideoView>
<ImageView
android:id="#+id/activity_local_album_preview_iv"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/activity_local_album_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
When the AppBarLayout is collapsed the RecyclerView scroll happens correctly.
But when the AppBarLayout is expanded, the height of the RecyclerView does not adjust and the scrolling appears abnormal
No ways to get it. But resize the recycler view when the app bar is expand or gonna collapse.
I have a fragment which renders a recycleview with an floating action button. The problem is that when the list is empty, the fab keeps on the right top, when the list has few items, the fab keeps below the list but not at the bottom, and it only keeps totally at the bottom when the list fill the screen with items. Could somebody help me please? Bellow is 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.juninho.financeapp.activity.FuncionarioActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/funcionario_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_add_func"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/ic_menu_send"
app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior" />
</android.support.design.widget.CoordinatorLayout>
if the list is empty, fab keeps at the top, which is wrong:
if the list has a few items, fab doesn't keep totally at the bottom:
Here is the correct way, fab is at the bottom:
Using RelativeLayout it seems to work, but its too at the right/bottom corner:
The problem is your FAB attribute: app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior".
#. You don't need to add this with your FAB. Just remove this attribute and add attribute app:layout_anchor="#id/funcionario_recycler_view" and app:layout_anchorGravity="bottom|right|end" to FAB to anchor it with RecyclerView.
Update your layout as below:
<?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"
tools:context="com.app.juninho.financeapp.activity.FuncionarioActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/funcionario_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_add_func"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/ic_menu_send"
app:layout_anchor="#id/funcionario_recycler_view"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
#. If you want to hide/show FAB when scrolling RecyclerView, then you can do it pragmatically in your java code as below:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.funcionario_recycler_view);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_func);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0 && fab.isShown())
{
fab.hide();
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
fab.show();
}
super.onScrollStateChanged(recyclerView, newState);
}
});
Hope this will help~
I have two problems. My idea was :
When I click on a marker of a googlemap, a bottomsheet shows the details of the marker.
I have two problems right now.
when a click on a marker, the bottomsheet view slid up and cover half of te screen (as I wanted) covering the map but the 2 FAB buttons remain still over the bottomsheet (I want them below the bottomSheet).
When the bottomSheet is showed, I would like that if I click on the map, the bottomSheet should disappear (it doesn't).
this is my code :
main_layout_activity.xml:
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
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>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ankic.it.nasone.NasoneActivity" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_gravity="bottom|end|right"
app:backgroundTint="#color/white"
android:layout_margin="#dimen/fab_margin"
app:fabSize="normal"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginBottom="85dp"
android:layout_marginEnd="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
app:layout_anchor="#id/floatingButton"
app:layout_anchorGravity="top"
app:backgroundTint="#color/white"
/>
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:clipToPadding="true"
android:background="#android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a test dialog fragment"
android:textColor="#android:color/white"
android:textSize="16sp"
android:padding="16dp"
android:background="#android:color/holo_purple"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/immagine_lista"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
MyActivity.java
here the snippets:
public class MyActivity extends AppCompatActivity
{
protected FloatingActionButton floatingButton;
private FloatingActionButton fabAdd;
private BottomSheetBehavior mBottomSheetBehavior;
............
#Override
protected void onCreate(Bundle savedInstanceState) {
........
this.floatingButton = (FloatingActionButton) findViewById(R.id.floatingButton);
this.fabAdd=(FloatingActionButton)findViewById(R.id.fabAdd);
this.cl=(CoordinatorLayout)findViewById(R.id.coordinatorLayoutNasoni) ;
View bottomSheet = findViewById( R.id.bottom_sheet );
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
return false;
}
});
}
}
What I should do?
you have to go to your mainactivity.java class and add this to your onmarkerclick method. it sets the behaviour of the floating action button for the different states of the bottomsheet and remember to do it for both floating action buttons ..its implemented for one of them here `
// set callback for changes
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
// this part hides the button immediately and waits bottom sheet
// to collapse to show
if (BottomSheetBehavior.STATE_EXPANDED== newState) {
floatingButton.animate().scaleX(0).scaleY(0).setDuration(0).start();
floatingButton.setVisibility(View.GONE);
} else if (BottomSheetBehavior.STATE_COLLAPSED == newState) {
floatingButton.animate().scaleX(1).scaleY(1).setDuration(0).start();
floatingButton.setVisibility(View.VISIBLE);
} else if (BottomSheetBehavior.STATE_HIDDEN == newState) {
floatingButton.animate().scaleX(1).scaleY(1).setDuration(0).start();
floatingButton.setVisibility(View.VISIBLE);
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
// Log.d(this.getClass().getSimpleName(), marker.getTitle());
return true;
}