Android第三方FloatingActionButton:伴随ListView、RecyclerView、ScrollView滚动滑入滑出

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:

 

 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. xmlns:fab="http://schemas.android.com/apk/res-auto"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent"

  6. tools:context="zhangphil.fab.MainActivity" >

  7.  
  8. <ListView

  9. android:id="@+id/listView"

  10. android:layout_width="match_parent"

  11. android:layout_height="match_parent" />

  12.  
  13. <com.melnykov.fab.FloatingActionButton

  14. android:id="@+id/fab"

  15. android:layout_width="wrap_content"

  16. android:layout_height="wrap_content"

  17. android:layout_alignParentBottom="true"

  18. android:layout_alignParentRight="true"

  19. android:layout_margin="16dip"

  20. android:src="@drawable/ic_add_new"

  21. fab:fab_shadow="true"

  22. fab:fab_type="normal" />

  23.  
  24. </RelativeLayout>

 

然后写Java代码:

 

 
  1. package zhangphil.fab;

  2.  
  3. import com.melnykov.fab.FloatingActionButton;

  4.  
  5. import android.app.Activity;

  6. import android.os.Bundle;

  7. import android.widget.ArrayAdapter;

  8. import android.widget.ListView;

  9.  
  10.  
  11. public class MainActivity extends Activity {

  12.  
  13. @Override

  14. protected void onCreate(Bundle savedInstanceState) {

  15. super.onCreate(savedInstanceState);

  16.  
  17. setContentView(R.layout.activity_main);

  18.  
  19. String[] data=new String[100];

  20. for(int i=0;i<data.length;i++){

  21. data[i]="Zhang Phil @CSDN "+i;

  22. }

  23.  
  24. ListView listView=(ListView) findViewById(R.id.listView);

  25.  
  26. ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);

  27. listView.setAdapter(adapter);

  28.  
  29. //附着在ListView,跟随ListView滚动滑入滑出

  30. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

  31. fab.attachToListView(listView);

  32. fab.setColorPressed(0xffb71c1c);

  33. }

  34. }

 

 

相关文章:
【文章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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值