美文网首页
Android 利用SwipeMenuListView实现Lis

Android 利用SwipeMenuListView实现Lis

作者: GODANDDEVIL | 来源:发表于2020-03-25 15:13 被阅读0次

GitHub地址:https://github.com/baoyongzhang/SwipeMenuListView
添加依赖:implementation 'com.baoyz.swipemenulistview:library:1.3.0'

使用例子:
activity_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".testActivity">

    <ImageView
        android:id="@+id/test_actionBar_bg"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/colorModel"/>

    <ImageView
        android:id="@+id/test_back_bt"
        android:layout_width="25dp"
        android:layout_height="25dp"
        app:layout_constraintBottom_toBottomOf="@+id/test_actionBar_bg"
        app:layout_constraintLeft_toLeftOf="@+id/test_actionBar_bg"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="7dp"
        android:padding="3dp"
        android:src="@mipmap/back"
        android:onClick="test_backClick"/>

    <TextView
        android:id="@+id/test_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/test_actionBar_bg"
        app:layout_constraintBottom_toBottomOf="@+id/test_actionBar_bg"
        app:layout_constraintRight_toRightOf="@+id/test_actionBar_bg"
        android:layout_marginBottom="5dp"
        android:text="test demo"
        android:textColor="@color/colorWhite"
        android:textSize="12pt"/>

    <LinearLayout
        android:id="@+id/test_linearLayout"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/test_actionBar_bg"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="20dp"
        android:background="@color/colorSelect">

        <com.baoyz.swipemenulistview.SwipeMenuListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

testActivity.java:

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

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

public class testActivity extends FatherActivity implements MyHandler.InMyHandler {//实现MyHandler的接口

    private SwipeMenuListView swipeMenuListView;//SwipeMenuListView
    private List<String> lists;//数据源
    private ArrayAdapter<String> arrayAdapter;//适配器
    MyHandler myHandler;//自定义Handler

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

        myHandler = MyHandler.getInstance();//初始化MyHandler
        myHandler.setInMyHandler(this);//注册接口

        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {//创建侧滑菜单
//                //侧滑菜单的打开按钮
//                SwipeMenuItem open_item = new SwipeMenuItem(getApplicationContext());
//                open_item.setBackground(new ColorDrawable(Color.RED));
//                open_item.setWidth(GeneralFunction.dpToPx(testActivity.this,90));
//                open_item.setTitle("Open");
//                open_item.setTitleSize(18);
//                open_item.setTitleColor(Color.WHITE);
//                menu.addMenuItem(open_item);
                //侧滑菜单的删除按钮
                SwipeMenuItem delete_item = new SwipeMenuItem(getApplicationContext());
                delete_item.setBackground(new ColorDrawable(Color.RED));
                delete_item.setWidth(GeneralFunction.dpToPx(testActivity.this,90));
//                delete_item.setIcon(R.drawable.delete);
                delete_item.setTitle("Delete");
                delete_item.setTitleSize(16);
                delete_item.setTitleColor(Color.WHITE);
                menu.addMenuItem(delete_item);
            }
        };

        //SwipeMenuListView
        swipeMenuListView = findViewById(R.id.listView);
        //将侧滑菜单添加到SwipeMenuListView里
        swipeMenuListView.setMenuCreator(creator);
        //SwipeMenuListView添加侧滑监听
        swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                //index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值,类似数组的下标。
                //从0开始,依次是:0、1、2、3...
                switch (index) {
//                    case 0:
//                        break;
                    case 0:
                        swipeMenuListView.smoothCloseMenu();//关闭item的侧滑
                        //点击了item的删除按钮,发送message更新UI
                        Message message = new Message();
                        message.what = position;
                        //这里设置延时是因为ListView刷新需要时间
                        myHandler.sendMessageDelayed(message, 500);
                        break;
                    default:
                        break;
                }
                // false : 当用户触发其他地方的屏幕时候,自动收起菜单。
                // true : 不改变已经打开菜单的样式,保持原样不收起。
                return false;
            }
        });

        // 监测用户在ListView的SwipeMenu侧滑事件。
        swipeMenuListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int pos) {
//                Log.d("位置:" + pos, "开始侧滑...");
            }

            @Override
            public void onSwipeEnd(int pos) {
//                Log.d("位置:" + pos, "侧滑结束.");
            }
        });

        //添加数据
        lists = new ArrayList<String>();
        for (int i = 0; i < 30; i++) {
            lists.add("测试数据" + i);
        }
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lists);
        swipeMenuListView.setAdapter(arrayAdapter);
    }

    //实现MyHandler的接口方法
    @Override
    public void InHandleMessage(Message message) {
        int position = message.what;
        lists.remove(position);//删除数据
        arrayAdapter.notifyDataSetChanged();//更新数据源
    }

    //返回按钮点击事件
    public void test_backClick(View view){
        finish();
    }


}

自定义的Handler,MyHandler.java:

import android.os.Handler;
import android.os.Message;

/**
 * Handler单例的封装
 */
public class MyHandler extends Handler {
    private MyHandler(){}
    private static class MyHandlerHolder{
        private static final MyHandler INSTANCE = new MyHandler();
    }
    static MyHandler getInstance(){
        return MyHandlerHolder.INSTANCE;
    }//返回线程安全的单例

    @Override
    public void handleMessage(Message message){//重写handleMessage方法
        super.handleMessage(message);
        if (inMyHandler!=null){
            inMyHandler.InHandleMessage(message);//调用接口方法
        }
    }

    private static InMyHandler inMyHandler;//定义接口变量
    public interface InMyHandler{//定义接口
        void InHandleMessage(Message message);//定义接口方法
    }
    void setInMyHandler(InMyHandler inMyHandler){//注册接口的方法
        MyHandler.inMyHandler = inMyHandler;//接口的引用指向它的实例化对象,传入的参数inMyHandler为实现该接口的类的实例化对象
    }
    void removeInMyHandler(){//取消注册接口的方法
        MyHandler.inMyHandler = null;//inMyHandler置为null,inMyHandler将不再持有外部类引用
    }

}

GeneralFunction.java:

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

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

public class GeneralFunction {
    private GeneralFunction(){}
    private static class GeneralFunctionHolder{
        private static final GeneralFunction INSTANCE = new GeneralFunction();
    }
    public static GeneralFunction getInstance(){
        return GeneralFunctionHolder.INSTANCE;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dpToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 获取应用程序显示区域宽高(不包括状态栏)
     */
    static List getScreenInformation(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        List<Integer> list = new ArrayList<>();
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(outMetrics);
            int widthPixels = outMetrics.widthPixels;//宽度(以像素为单位)。
            int heightPixels = outMetrics.heightPixels;//高度(以像素为单位)。
//            int densityDpi = outMetrics.densityDpi;//屏幕密度表示为每英寸点数。
//            float density = outMetrics.density;//显示器的逻辑密度。
//            float scaledDensity = outMetrics.scaledDensity;//显示屏上显示的字体缩放系数。
            list.add(widthPixels);
            list.add(heightPixels);
            return list;
        }
        return null;
    }
    /**
     * 实际屏幕显示区域宽高
     */
    static List getRealScreenInformation(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        List<Integer> list = new ArrayList<>();
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
            int widthPixels = outMetrics.widthPixels;//宽度(以像素为单位)。
            int heightPixels = outMetrics.heightPixels;//高度(以像素为单位)。
            list.add(widthPixels);
            list.add(heightPixels);
            return list;
        }
        return null;
    }

    /**
     * 获取屏幕高度
     */
    static int getRealScreenHeight(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height = 0;
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
            height = outMetrics.heightPixels;//高度(以像素为单位)。
        }
        return height;
    }

    /**
     * 获取屏幕宽度
     */
    static int getRealScreenWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = 0;
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
            width = outMetrics.widthPixels;//高度(以像素为单位)。
        }
        return width;
    }


}

相关文章

网友评论

      本文标题:Android 利用SwipeMenuListView实现Lis

      本文链接:https://www.haomeiwen.com/subject/xvpyyhtx.html