1. 程式人生 > >懸浮按鈕(實現程式碼)

懸浮按鈕(實現程式碼)

一、倒依賴

//懸浮按鈕
compile 'com.android.support:design:26.1.0'

二、在Xml中

<android.support.design.widget.FloatingActionButton
android:id="@+id/mXuanFu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="60dp"

app:backgroundTint="#6eed5b"
app:rippleColor="#e7aa59"
app:elevation="10dp"
app:pressedTranslationZ="5dp"
app:fabSize="mini"
android:src="@drawable/ic_arrow_upward"/>

三、在Activity中

(1)初始化、設定全域性靜態
public static FloatingActionButton mXuanFu;
(2)呼叫介面卡的監聽事件:
mRecya.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState > 0) {
((MainActivity) getActivity()).findViewById(R.id.tabbar).setVisibility(View.GONE);
} else {
((MainActivity) getActivity()).findViewById(R.id.tabbar).setVisibility(View.VISIBLE);

}

}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0){
((MainActivity)getActivity()).mXuanFu.setVisibility(View.GONE);
((MainActivity)getActivity()).tabbar.setVisibility(View.GONE);
}else {
((MainActivity)getActivity()).mXuanFu.setVisibility(View.VISIBLE);
((MainActivity)getActivity()).tabbar.setVisibility(View.VISIBLE);
((MainActivity)getActivity()).mXuanFu.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
((MainActivity)getActivity()).mXuanFu.setFocusable(true);
mRecya.scrollToPosition(0);
}
});
}
}
});