Android Support Library - BottomSheet
来源:互联网 发布:淘宝客网站要多大空间 编辑:程序博客网 时间:2024/06/09 16:17
Android Support Library - BottomSheet
BottomSheet必须是作为CoordinatorLayout
的子布局,并为BottomSheet设置app:layout_behavior
属性
布局文件activity_main.xml
源码地址
<?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=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fragment" android:text="Bottom Sheet Fragment"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/bottom_sheet" android:background="@color/colorAccent" android:paddingLeft="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_horizontal_margin" app:behavior_hideable="false" app:behavior_peekHeight="50dp" app:elevation="4dp" app:layout_behavior="@string/bottom_sheet_behavior"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list_view" android:layout_margin="@dimen/activity_vertical_margin"></android.support.v7.widget.RecyclerView> </RelativeLayout> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fab" android:layout_margin="@dimen/activity_vertical_margin" android:src="@drawable/ic_emoticon" app:layout_anchor="@id/bottom_sheet" app:layout_anchorGravity="top|end"/> </android.support.design.widget.CoordinatorLayout>
在BottomSheet中加入了一个RecyclerView
,实现listview的布局效果
java代码逻辑
mBottomFragment = (Button)findViewById(R.id.fragment); fab = (FloatingActionButton)findViewById(R.id.fab); View bottomSheet = findViewById(R.id.bottom_sheet); BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); //设置回调方法 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { //可以设置FAB的动画 //fab.animate().scaleX(1 - slideOffset).scaleY(1 - slideOffset).setDuration(0).start(); } });
RecyclerView的item的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_text" android:gravity="center_vertical" style="@style/TextAppearance.AppCompat.Body1"/> </LinearLayout>
设置RecyclerView
ArrayList<String> data = new ArrayList<>(); for(int i = 0;i<5;i++){ data.add("Item "+i); } RecyclerView recyclerView = (RecyclerView)findViewById(R.id.list_view); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); ItemAdapter adapter = new ItemAdapter(data); recyclerView.setAdapter(adapter);
BottomSheetDialogFragment
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment { private ArrayList<String> mItems; static MyBottomSheetDialogFragment newInstance(ArrayList<String> items){ MyBottomSheetDialogFragment d = new MyBottomSheetDialogFragment(); Bundle arg = new Bundle(); arg.putStringArrayList("item",items); d.setArguments(arg); return d; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.bottom_dialog,null,false); RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); ItemAdapter adapter = new ItemAdapter(mItems); recyclerView.setAdapter(adapter); return v; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mItems = getArguments().getStringArrayList("item"); } }
在ManiActivity中的逻辑
final MyBottomSheetDialogFragment fragment = MyBottomSheetDialogFragment.newInstance(data); mBottomFragment.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { fragment.show(getSupportFragmentManager(),fragment.getTag()); } });
0 0
- Android Support Library - BottomSheet
- android support Library—-Percent Support Library
- Android Support Library package
- Android Support Library
- Android中的support library
- Android Support Library
- 【翻译】Android Support Library
- Android Design Support Library
- Android Support Library
- android 最新 support library
- Android Support Library 2015
- Android Design Support Library
- Android Support Library 23.1
- android的Support Library
- Android Support Library
- Android Support Library
- Android Design Support Library
- Android Support Library 23.2
- 服务器运维及部署
- 在JSP中引用JS文件的三种方法
- SYS和SYSTEM模式
- ZooKeeper 的一致性算法赏析
- plsql无法识别oracle数据库
- Android Support Library - BottomSheet
- 第十一周项目1——二叉树算法验证(2)二叉树构造算法验证
- strings 命令——ELF文件格式与“链接和装载”
- 2017年大数据从业者又要涨工资了!
- 文字排版中的设计四原则(三)
- 65条最常用正则表达式
- http://blog.csdn.net/huachao1001/article/details/52247268
- Timer的缺陷-不按定时时间执行【已入坑】
- 算法(第四版)——02选择排序