Android第三方FloatingActionButton:伴随ListView、RecyclerView、ScrollView滚动滑入滑出
转自https://blog.csdn.net/zhangphil/article/details/50135707
Android在5.0及以上提供了原生的FloatingActionButton【参考文章1】,然而,github上有一个开源的第三方FloatingActionButton实现,这个第三方的FloatingActionButton有一个特点:伴随Android常见的ListView、RecyclerView、ScrollView这类滚动View上下滚动时候滑入滑出,如图(图来自该项目主页):
Android第三方FloatingActionButton在github上的项目主页是:https://github.com/makovkastar/FloatingActionButton
Android第三方FloatingActionButto使用还是简单,就是导入作为库的时候有些复杂。
现在给出一个例子说明。
首先要写布局文件定义Android第三方FloatingActionButto:
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
xmlns:fab="http://schemas.android.com/apk/res-auto"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
tools:context="zhangphil.fab.MainActivity" >
-
<ListView
-
android:id="@+id/listView"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" />
-
<com.melnykov.fab.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_margin="16dip"
-
android:src="@drawable/ic_add_new"
-
fab:fab_shadow="true"
-
fab:fab_type="normal" />
-
</RelativeLayout>
然后写Java代码:
-
package zhangphil.fab;
-
import com.melnykov.fab.FloatingActionButton;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.ArrayAdapter;
-
import android.widget.ListView;
-
public class MainActivity extends Activity {
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
String[] data=new String[100];
-
for(int i=0;i<data.length;i++){
-
data[i]="Zhang Phil @CSDN "+i;
-
}
-
ListView listView=(ListView) findViewById(R.id.listView);
-
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
-
listView.setAdapter(adapter);
-
//附着在ListView,跟随ListView滚动滑入滑出
-
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
-
fab.attachToListView(listView);
-
fab.setColorPressed(0xffb71c1c);
-
}
-
}
相关文章:
【文章1】《Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout》链接地址:http://blog.csdn.net/zhangphil/article/details/48861371
--------------------- 本文来自 zhangphil 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zhangphil/article/details/50135707?utm_source=copy