欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

BottomSheetDialog的简易使用

程序员文章站 2022-04-26 08:09:36
...

工作日志记录,最近工作上有一个设计效果类似于BottomSheetDialog的效果,这里做了一个demo,记录下实现的效果和代码,方便以后可以参照一下:

注意:使用这个BottomSheetDialog时候要映入对应得MaterialDesign包,我这里是androidx版本所有使用  

compile 'com.google.android.material:material:1.0.0',如果是appconpact包编译,可以使用as上的refactor->Migrate to Appcompact来转成appcompact兼容。

实现效果如下:

BottomSheetDialog的简易使用

MainActivity.java代码如下(具体实现):

package com.xuganwen.testdrag;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recycleview;
    //    private MyAdapter myAdapter;
    private MyAdapter myAdapter;
    private SimpleCycleProgressView progressview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recycleview = findViewById(R.id.recycleview);
        final List<String> list = new ArrayList<>();

        for (int i = 0; i < 60; i++) {

            list.add(i + "hehe");
        }

        //第一种方式使用  com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30库提供的adapter的扩展方法来实现,实际上也是在第二种方式的基础上再做简易封装
        myAdapter = new MyAdapter(R.layout.item,list);
        recycleview.setLayoutManager(new GridLayoutManager(this, 4));
        recycleview.setAdapter(myAdapter);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(myAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(recycleview);

        myAdapter.enableDragItem(itemTouchHelper, R.id.tv, true);
        OnItemDragListener onItemDragListener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos){}
            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {}
            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.i("xuganwenlog", list.toString());
            }
        };

        myAdapter.setOnItemDragListener(onItemDragListener);


        final BottomSheetDialog dialog = new BottomSheetDialog(this);

        View view = View.inflate(this, R.layout.bottom_sheet_layout, null);
        dialog.setContentView(view);
        RecyclerView recycleview = view.findViewById(R.id.recycleview);
        recycleview.setLayoutManager(new GridLayoutManager(this, 4));
        recycleview.setAdapter(myAdapter);

        final BottomSheetBehavior<View> mDialogBehavior = BottomSheetBehavior.from((View) view.getParent());

        mDialogBehavior.setPeekHeight(getResources().getDisplayMetrics().heightPixels/3);
        mDialogBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dialog.dismiss();
                    mDialogBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        });

        dialog.show();


        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
            }
        });
    }

}

 

简单实例,这里不再赘述。

相关标签: 移动开发