国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中FloatingActionButton的顯示與隱藏示例

2019-12-12 01:52:56
字體:
來源:轉載
供稿:網友

FloatingActionButton簡介

FloatingActionButton(FAB) 是Android 5.0 新特性――Material Design 中的一個控件,是一種懸浮的按鈕,并且是 ImageView 的子類,因此它具備ImageView的全部屬性。一般FloatingActionButton 結合 CoordinatorLayout 使用,即可實現懸浮在任意控件的任意位置。

FloatingActionButton使用

本文主要實現的效果:Toolbar和FloatingActionButton根據頁面列表的上下滑動來隱藏和顯示。

效果圖:


當我上滑列表時:隱藏Toolbar和FloatingActionButton


當我下滑列表的時:顯示Toolbar和FloatingActionButton

實現方法(一)

監聽頁面列表(RecyclerView)的滑動回調事件,通過回調來決定Toolbar和FAB的顯示和隱藏。

1)封裝RecyclerView.OnScrollListener,封裝的原因是為了讓Activity顯得沒那么臃腫。

public class MyScrollListener extends RecyclerView.OnScrollListener {  private HideAndShowListener mHideAndShowListener;  private static final int THRESHOLD = 20;  private int distance = 0;  private boolean visible = true;  public MyScrollListener(HideAndShowListener hideAndShowListener) {    mHideAndShowListener = hideAndShowListener;  }  @Override  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {    super.onScrolled(recyclerView, dx, dy);    /**     * dy:Y軸方向的增量     * 有正和負     * 當正在執行動畫的時候,就不要再執行了     */    Log.i("tag","dy--->"+dy);    if (distance > THRESHOLD && visible) {      //隱藏動畫      visible = false;      mHideAndShowListener.hide();      distance = 0;    } else if (distance < -20 && !visible) {      //顯示動畫      visible = true;      mHideAndShowListener.show();      distance = 0;    }    if (visible && dy > 0 || (!visible && dy < 0)) {      distance += dy;    }  }  public interface HideAndShowListener {    void hide();    void show();  }}

主要在onScrolled方法計算判斷FAB的顯示和隱藏,然后設置HideAndShowListener回調,調用相應的顯示和隱藏的方法即可。

2)RecyclerView添加OnScrollListener監聽并且設置HideAndShowListener回調,通過HideAndShowListener的hide()和show()來設置FAB的隱藏和顯示。

public class FABActivity extends AppCompatActivity {  private RecyclerView mRecyclerView;  private FloatingActionButton fab;  private Toolbar toolbar;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_fab);    toolbar = (Toolbar) findViewById(R.id.toolbar);    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);    setSupportActionBar(toolbar);    setTitle("FAB");    fab = (FloatingActionButton) findViewById(R.id.fab);    fab.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)            .setAction("Action", null).show();      }    });    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));    List<String> list = new ArrayList<>();    for (int i = 0; i < 60; i++) {      list.add("Item"+i);    }    FabRecyclerAdapter adapter = new FabRecyclerAdapter(list);    mRecyclerView.setAdapter(adapter);    setListener();  }  /**   * 添加ScrollListener監聽   * 以及HideAndShowListener回調   */  private void setListener() {    mRecyclerView.addOnScrollListener(new MyScrollListener(new MyScrollListener.HideAndShowListener() {      @Override      public void hide() {        // 隱藏動畫--屬性動畫        toolbar.animate().translationY(-toolbar.getHeight()).setInterpolator(new AccelerateInterpolator(3));        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) fab.getLayoutParams();        fab.animate().translationY(fab.getHeight() + layoutParams.bottomMargin).setInterpolator(new AccelerateInterpolator(3));      }      @Override      public void show() {        // 顯示動畫--屬性動畫        toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) fab.getLayoutParams();        fab.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));      }    }));  }}

在hide()和show()方法中,設置了FAB的隱藏和顯示的動畫。

接下來給出RecyclerView的Adapter

public class FabRecyclerAdapter extends RecyclerView.Adapter {  private List<String> list;  public FabRecyclerAdapter(List<String> list) {    this.list = list;  }  @Override  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);    return new MyViewHolder(view);  }  @Override  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {    String str = list.get(position);    MyViewHolder hd = (MyViewHolder) holder;    hd.mButton.setText(str);  }  @Override  public int getItemCount() {    if (list != null) {      return list.size();    }    return 0;  }  class MyViewHolder extends RecyclerView.ViewHolder {    private Button mButton;    public MyViewHolder(View itemView) {      super(itemView);      mButton = (Button) itemView.findViewById(R.id.btn);    }  }}

MyViewHolder的xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="vertical" >  <Button    android:id="@+id/btn"    android:layout_margin="5dp"    android:layout_width="match_parent"    android:layout_height="wrap_content"    /></LinearLayout>

Activity的布局文件

<?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.main.fab.FABActivity">  <android.support.v7.widget.RecyclerView    android:id="@+id/recyclerview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clipChildren="false"    android:clipToPadding="false"    android:paddingTop="?attr/actionBarSize"    />  <android.support.v7.widget.Toolbar    android:id="@+id/toolbar"    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="?attr/colorPrimary"/>  <android.support.design.widget.FloatingActionButton    android:id="@+id/fab"    android:layout_width="58dp"    android:layout_height="58dp"    android:layout_alignParentBottom="true"    android:layout_alignParentRight="true"    android:layout_margin="16dp"    /></RelativeLayout>

以上就是實現Toolbar和FloatingActionButton根據頁面列表的上下滑動來隱藏和顯示方法一的這個過程。

實現方法(二)

通過封裝CoordinatorLayout.Behavior,通過它的onNestedScroll方法計算判斷顯示和隱藏,同時給Toolbar和FAB設置app:layout_behavior,該屬性指定使用封裝的CoordinatorLayout.Behavior即可。

1)封裝CoordinatorLayout.Behavior

public class FabBehavior extends CoordinatorLayout.Behavior {  public FabBehavior() {  }  public FabBehavior(Context context, AttributeSet attrs) {    super(context, attrs);  }  private boolean visible = true;//是否可見  @Override  public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {    //被觀察者(RecyclerView)發生滑動的開始的時候回調的    //nestedScrollAxes:滑動關聯軸,現在只關心垂直的滑動。    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild,        target, nestedScrollAxes);  }  @Override  public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);    //被觀察者滑動的時候回調的    if (dyConsumed > 0 && visible) {      //show      visible = false;      onHide(child);    } else if (dyConsumed < 0) {      //hide      visible = true;      onShow(child);    }  }  public void onHide(View view) {    // 隱藏動畫--屬性動畫    if (view instanceof Toolbar){      ViewCompat.animate(view).translationY(-(view.getHeight() * 2)).setInterpolator(new AccelerateInterpolator(3));    }else if (view instanceof FloatingActionButton){      ViewCompat.animate(view).translationY(view.getHeight() * 2).setInterpolator(new AccelerateInterpolator(3));    }else{    }  }  public void onShow(View view) {    // 顯示動畫--屬性動畫    ViewCompat.animate(view).translationY(0).setInterpolator(new DecelerateInterpolator(3));  }}

onStartNestedScroll:列表(RecyclerView)剛開始滑動時候會回調該方法,需要在方法內設置滑動關聯軸。這里只需要垂直方向上的滑動即可。

onNestedScroll:滑動的時候不斷的回調該方法,通過dyConsumed來判斷是上滑還是下滑。

2)Toolbar和FAB設置app:layout_behavior

<?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.main.behavior.BehaviorActivity">  <android.support.v7.widget.RecyclerView    android:id="@+id/recyclerview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clipChildren="false"    android:clipToPadding="false"    android:paddingTop="?attr/actionBarSize"    />  <android.support.v7.widget.Toolbar    android:id="@+id/toolbar"    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="?attr/colorPrimary"    app:layout_behavior="com.main.behavior.FabBehavior"/>  <android.support.design.widget.FloatingActionButton    android:id="@+id/fab"    android:layout_width="58dp"    android:layout_height="58dp"    android:layout_gravity="bottom|end"    android:layout_margin="16dp"    android:src="@mipmap/ic_launcher"    app:layout_behavior="com.main.behavior.FabBehavior"/></android.support.design.widget.CoordinatorLayout>

在布局文件中給Toolbar和FAB直接設置app:layout_behavior即可。

BehaviorActivity.java

/** * FloatActionButton * 滑動顯示與隱藏 */public class BehaviorActivity extends AppCompatActivity {  private RecyclerView mRecyclerView;  private Toolbar toolbar;  private FloatingActionButton fab;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_behavior);    mRecyclerView = (RecyclerView)findViewById(R.id.recyclerview);    fab = (FloatingActionButton)findViewById(R.id.fab);    toolbar = (Toolbar)findViewById(R.id.toolbar);    setSupportActionBar(toolbar);    setTitle("FAB");    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));    List<String> list = new ArrayList<>();    for (int i = 0; i < 60; i++) {      list.add("Item"+i);    }    FabRecyclerAdapter adapter = new FabRecyclerAdapter(list);    mRecyclerView.setAdapter(adapter);  }}

這樣就可以使用該方案實現Toolbar和FloatingActionButton根據頁面列表的上下滑動來隱藏和顯示。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜城县| 维西| 秦皇岛市| 桃源县| 深水埗区| 罗定市| 沂源县| 麦盖提县| 宝兴县| 永吉县| 常德市| 电白县| 禹州市| 南澳县| 莱芜市| 淮南市| 香港 | 金昌市| 满城县| 汨罗市| 武宁县| 深水埗区| 凤山县| 米脂县| 禹州市| 蕉岭县| 崇文区| 吴江市| 民县| 安远县| 项城市| 平果县| 贵溪市| 苗栗县| 措勤县| 天峨县| 广元市| 马公市| 美姑县| 乌什县| 湘潭市|