Android Support Library - BottomSheet

Android Support Library - BottomSheet

效果图

BottomSheet必须是作为CoordinatorLayout的子布局,并为BottomSheet设置app:layout_behavior属性

布局文件activity_main.xml
源码地址




  

      

在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的布局



  

  

设置RecyclerView

ArrayList 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 mItems;

  static MyBottomSheetDialogFragment newInstance(ArrayList 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());
    }
});

你可能感兴趣的:(Android Support Library - BottomSheet)