带删除功能 listview 实例备忘

界面效果:

说明

假设有一个 DeviceCollection 的类,类中有一个名为 name 的String 类型成员变量。

public class DeviceCollection {
    public String name;
}

接着有一组 DeviceCollection 的实例:

List<DeviceCollection> dataList;

功能上希望把 列表里的每个DeviceCollection 的 name 显示到 listview 的每一行中。

功能实现主要涉及到几部分,

布局文件:

1 列表元素对应的布局文件

2 listview 本身

java 代码:

1 adapter 的实现

2 adapter 的应用

3 option menu 的操作

布局文件

列表元素对应的布局文件

很简单的一个布局,三个子view

textview 显示 name

imageview1 显示 向右的箭头,点击打开该项的内容

imageview2 显示垃圾桶的图标,只在删除模式下显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_centerVertical="true"
        android:textColor="#000"
        android:layout_toLeftOf="@id/iv"
        android:text="设备名称"/>

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_open"/>

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_delete"/>

</RelativeLayout>

listview 本身

listview 嵌在一个界面中,这里省略了不必要的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</RelativeLaout>

java 代码

adapter 的实现

数据展示逻辑

构造函数中传入数据  List<DeviceCollection> collections:

    public DeviceCollectionListAdapter(Context context, List<DeviceCollection> collections){
        this.mContext=context;
        mLayoutInflater =LayoutInflater.from(context);
        this.collections = collections;
    }

getview 中根据 position  参数取出 collections 中相应的项,并调用 textview的 setText方法进行界面渲染。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        DeviceCollection collection = (DeviceCollection) getItem(position);
        holder.name.setText(collection.name);
        return convertView;
    }

删除模式的逻辑

adapter中增加成员变量表示当前是否为删除模式

    private boolean deleteMode = false;

    public void setDeleteMode(boolean deleteMode) {
        this.deleteMode = deleteMode;
    }

getview 中根据该成员变量的值来决定是否显示 垃圾桶(删除)图标:

        if (deleteMode) {
            holder.ivOpen.setVisibility(View.GONE);
            holder.ivDelete.setVisibility(View.VISIBLE);
        } else {
            holder.ivOpen.setVisibility(View.VISIBLE);
            holder.ivDelete.setVisibility(View.GONE);
        }

完整的代码

public class DeviceCollectionListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<DeviceCollection> collections;

    private boolean deleteMode = false;

    public void setDeleteMode(boolean deleteMode) {
        this.deleteMode = deleteMode;
    }

    public DeviceCollectionListAdapter(Context context, List<DeviceCollection> collections){
        this.mContext=context;
        mLayoutInflater =LayoutInflater.from(context);
        this.collections = collections;
    }

    public void setData(List<DeviceCollection> collections) {
        this.collections = collections;
    }

    public List<DeviceCollection> getData() {
        return collections;
    }

    @Override
    public int getCount() {
        return collections.size();
    }

    @Override
    public Object getItem(int position) {
        return collections.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        public TextView name;
        public ImageView ivOpen;
        public ImageView ivDelete;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = mLayoutInflater.inflate(R.layout.item_device_collection_list,null);
            holder = new ViewHolder();
            holder.name= (TextView) convertView.findViewById(R.id.tv_name);
            holder.ivDelete= (ImageView) convertView.findViewById(R.id.iv_delete);
            holder.ivOpen= (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        DeviceCollection collection = (DeviceCollection) getItem(position);

        holder.name.setText(collection.name);

        if (deleteMode) {
            holder.ivOpen.setVisibility(View.GONE);
            holder.ivDelete.setVisibility(View.VISIBLE);
        } else {
            holder.ivOpen.setVisibility(View.VISIBLE);
            holder.ivDelete.setVisibility(View.GONE);
        }

        return convertView;
    }
}

adapter的使用

adapter的构建及绑定到listview

        listView = findViewById(R.id.lv);
        List<DeviceCollection> list = //xxx ; 构建数据部分从略
        adapter = new DeviceCollectionListAdapter(this, list);
        listView.setAdapter(adapter);

listview 设置点击响应

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                final DeviceCollection collection = (DeviceCollection) adapter.getItem(position);
                if (deleteMode) {
                    AlertDialog.Builder ab = new AlertDialog.Builder(context);
                    ab.setTitle("确定删除: " + collection.name + " ?");
                    ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            List<DeviceCollection> list = adapter.getData();
                            list.remove(position);
                            adapter.setData(list);
                            adapter.notifyDataSetChanged();
                        }
                    });
                    ab.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    ab.create().show();
                } else {
                    // 打开下一级界面
                }
            }
        });

activity 的菜单操作部分代码

menu.xml

<menu 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"
    tools:context="com.linptech.matchtool.MainActivity">
    <item
        android:id="@+id/action_delete"
        android:title="删除"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_delete_done"
        android:icon="@drawable/ic_done"
        app:showAsAction="ifRoom"
        android:title="完成删除" />
</menu>

java 代码逻辑

根据当前是否为删除模式,隐藏/显示菜单项:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
            return true;
        }

        if (item.getItemId() == R.id.action_delete) {
            deleteMode = true;
            updateOptionsMenu();

            adapter.setDeleteMode(deleteMode);
            adapter.notifyDataSetChanged();
            return true;
        }
        if (item.getItemId() == R.id.action_delete_done) {
            deleteMode = false;
            updateOptionsMenu();

            adapter.setDeleteMode(deleteMode);
            adapter.notifyDataSetChanged();
            return true;
        }
        return true;
    }

    Menu menu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_delete, menu);
        this.menu = menu;
        updateOptionsMenu();
        return super.onCreateOptionsMenu(menu);
    }

    private void updateOptionsMenu() {
        menu.findItem(R.id.action_delete_done).setVisible(deleteMode);
        menu.findItem(R.id.action_delete).setVisible(!deleteMode);
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zonson9999

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值