<android.support.design.widget.FloatingActionButtonandroid:src="@drawable/ic_done"app:fabSize="normal"android:layout_width="wrap_content"android:layout_height="wrap_content" />
<android.support.design.widget.CoordinatorLayoutandroid:id="@+id/main_content"xmlns:android=""xmlns:app=""android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/lvToDoList"android:layout_width="match_parent"android:layout_height="match_parent"></ListView><android.support.design.widget.FloatingActionButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|right"android:layout_margin="16dp"android:src="@drawable/ic_done"app:layout_anchor="@id/lvToDoList"app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
<android.support.v7.widget.RecyclerViewandroid:id="@+id/lvToDoList"android:layout_width="match_parent"android:layout_height="match_parent"
</android.support.v7.widget.RecyclerView>
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {@Overridepublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || StartNestedScroll(coordinatorLayout, child, directTargetChild, target,nestedScrollAxes);}}
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {// ...@Overridepublic void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {NestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,dyUnconsumed);if (dyConsumed > 0 && !this.mIsAnimatingOut && Visibility() == View.VISIBLE) {animateOut(child);} else if (dyConsumed < 0 && Visibility() != View.VISIBLE) {animateIn(child);}}// ...}
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {private static final android.view.animation.Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();private boolean mIsAnimatingOut = false;// Same animation that FloatingActionButton.Behavior uses to // hide the FAB when the AppBarLayout exitsprivate void animateOut(final FloatingActionButton button) {if (Build.VERSION.SDK_INT >= 14) {ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {public void onAnimationStart(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = true;}public void onAnimationCancel(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = false;}public void onAnimationEnd(View view) {ScrollAwareFABBehavior.this.mIsAnimatingOut = false;view.setVisibility(View.GONE);}}).start();} else {Animation anim = AnimationUtils.Context(), R.anim.fab_out);anim.setInterpolator(INTERPOLATOR);anim.setDuration(200L);anim.setAnimationListener(new Animation.AnimationListener() {public void onAnimationStart(Animation animation) {ScrollAwareFABBehavior.this.mIsAnimatingOut = true;}public void onAnimationEnd(Animation animation) {ScrollAwareFABBehavior.this.mIsAnimatingOut = false;button.setVisibility(View.GONE);}@Overridepublic void onAnimationRepeat(final Animation animation) {}});button.startAnimation(anim);}}// Same animation that FloatingActionButton.Behavior // uses to show the FAB when the AppBarLayout entersprivate void animateIn(FloatingActionButton button) {button.setVisibility(View.VISIBLE);if (Build.VERSION.SDK_INT >= 14) {ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();} else {Animation anim = AnimationUtils.Context(), R.anim.fab_in);anim.setDuration(200L);anim.setInterpolator(INTERPOLATOR);button.startAnimation(anim);}}
}
<android.support.design.widget.FloatingActionButton app:layout_behavior=depath.floatingactionbuttontest.ScrollAwareFABBehavior" />
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {// ...public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {super();}// ...}
<FrameLayout xmlns:android=""xmlns:fab=""android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@android:id/list"android:layout_width="match_parent"android:layout_height="match_parent" />&lnykov.fab.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|right"android:layout_margin="16dp"android:src="@drawable/ic_action_content_new"fab:fab_type="normal"fab:fab_shadow="true"fab:fab_colorNormal="@color/primary"fab:fab_colorPressed="@color/primary_pressed"fab:fab_colorRipple="@color/ripple" />
</FrameLayout>
ListView listView = (ListView) findViewById(android.R.id.list);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.attachToListView(listView); // or attachToRecyclerView
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.fab);
fab.attachToListView(list, new ScrollDirectionListener() {@Overridepublic void onScrollDown() {Log.d("ListViewFragment", "onScrollDown()");}@Overridepublic void onScrollUp() {Log.d("ListViewFragment", "onScrollUp()");}
}, new AbsListView.OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {Log.d("ListViewFragment", "onScrollStateChanged()");}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {Log.d("ListViewFragment", "onScroll()");}
});
本文发布于:2024-02-01 11:16:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170675738636224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |