android 底部抽屉 BottomSheet+CoordinatorLayout 实现 AndroidX版本

android 底部抽屉 BottomSheet+CoordinatorLayout 实现 AndroidX版本

activity_coordinator.xml



        
                
        


CoordinatorActivity.java

package com.wdkh.dw.GPS.activity;

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

import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.wdkh.dw.GPS.model.adapter.CarListAdapter;
import com.wdkh.dw.GPS.model.adapter.CoListAdapter;
import com.wdkh.dw.GPS.utils.DeviceUtils;
import com.wdkh.dw.R;

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

public class CoordinatorActivity extends AppCompatActivity {

    //底部抽屉中的自定义布局
    private RecyclerView recycler_view;
    //抽屉的主view
    private View bottomSheet;
    //抽屉view的id
    private LinearLayout chou_ti_id;

    private BottomSheetBehavior behavior;
    private List carList = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);
        //设置抽屉最大高度
        chou_ti_id = findViewById(R.id.chou_ti_id);
        ViewGroup.LayoutParams para;
        para = chou_ti_id.getLayoutParams();
        int height1 = (int) (getResources().getDisplayMetrics().heightPixels * 0.7);
        para.height=height1;
        chou_ti_id.setLayoutParams(para);
        //底部抽屉栏展示地址
        bottomSheet = findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
        int height = (int) (getResources().getDisplayMetrics().heightPixels * 0.2);
        behavior.setPeekHeight(height);
       // bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 设置展开折叠的默认状态
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) {
                String state = "null";
                switch (newState) {
                    case 1:
                        state = "STATE_DRAGGING";//过渡状态此时用户正在向上或者向下拖动bottom sheet

                        break;
                    case 2:
                        state = "STATE_SETTLING"; // 视图从脱离手指自由滑动到最终停下的这一小段时间
                        break;
                    case 3:
                        state = "STATE_EXPANDED"; //处于完全展开的状态

                        break;
                    case 4:
                        state = "STATE_COLLAPSED"; //默认的折叠状态
                        break;
                    case 5:
                        state = "STATE_HIDDEN"; //下滑动完全隐藏 bottom sheet
                        break;
                }

            }

            //实时返回滚动数据
            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.d("BottomSheetDemo", "slideOffset:" + slideOffset);
            }
        });

        //用于测试的列表数据
        recycler_view = findViewById(R.id.recycler_view);
        List strList = new ArrayList<>();
        for(int i = 0;i<100;i++){
            strList.add("fffff"+i);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recycler_view.setLayoutManager(layoutManager);
        CoListAdapter adapter = new CoListAdapter(strList, this);
        recycler_view.setAdapter(adapter);
        adapter.setOnItemClickListener(postion -> {
            String chepaihao = carList.get(postion)+"";

            Log.e("点击", "onItemLongClick: ");

        });
    }
}

poi_detail_bottom.xml



    
    

CoListAdapter.java

package com.wdkh.dw.GPS.model.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.wdkh.dw.GPS.model.domain.OrderDomain;
import com.wdkh.dw.R;

import java.util.List;

public class CoListAdapter extends RecyclerView.Adapter{
    private List mOrderList;
    private Context mContext;

    //初始化list_item组件
    static class ViewHolder extends RecyclerView.ViewHolder {
        View fruitView;
        LinearLayout rank_item_banck;
        TextView chepaihao;
        ImageView icon;
        public ViewHolder(View view) {
            super(view);
            fruitView = view;
            rank_item_banck = view.findViewById(R.id.rank_item_banck);
            chepaihao = view.findViewById(R.id.chepaihao);//车牌号
         /*   ZhuangTai = view.findViewById(R.id.ZhuangTai);//车辆审核状态*/
            icon = view.findViewById(R.id.icon);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.car_list_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String cph = mOrderList.get(position);
        holder.chepaihao.setText(cph);
        /*holder.ZhuangTai.setText(cph.drivrName);*/


    }
    @Override
    public int getItemCount() {
        return mOrderList.size();
    }
    public CoListAdapter(List OrderReceivedList, Context context) {
        mOrderList=OrderReceivedList;
        this.mContext =context;
    }
    private String getStatus(int state){
        String stateStr = "";
        switch (state){
            case 1:
                stateStr = "待核查";
                break;
            case 2:
                stateStr = "核查通过";
                break;
            default:
                stateStr = "待核查";
                break;
        }
        return stateStr;
    }
    //点击回到多态实现
    OnItemClickListener mClickListener;
    public interface OnItemClickListener {
        void onItemClick(int postion);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }
}

android 底部抽屉 BottomSheet+CoordinatorLayout 实现 AndroidX版本_第1张图片
android 底部抽屉 BottomSheet+CoordinatorLayout 实现 AndroidX版本_第2张图片

你可能感兴趣的:(android,android)