首先先写布局文件界面样式
android中listview的长按删除多选功能实现_第1张图片
下面是布局代码

<?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:orientation="vertical"    android:background="@color/white">    <RelativeLayout        android:id="@+id/main_title"        style="@style/mailtitle_rl_style">        <ImageView            android:id="@+id/img_inboxmenu"            style="@style/mailtitle_leftiv_style"            android:background="@drawable/inbox_menu"/>        <TextView            android:id="@+id/tv_choseall"            style="@style/right_tv_style"            android:text="@string/long_all"            android:layout_alignTop="@+id/img_inboxedit"            android:visibility="gone"            />        <TextView            android:id="@+id/title_name"            style="@style/mailtitle_txt_style"            android:text="@string/mail_inbox" />        <ImageButton            android:id="@+id/img_inboxedit"            style="@style/mailtitle_tightiv_style"            android:background="@drawable/inbox_edit01"            />        <TextView            android:id="@+id/tv_cencal"            style="@style/writeemail_righttv_style"            android:text="@string/long_cencal"            android:visibility="gone"/>    RelativeLayout>        <RelativeLayout        android:id="@+id/rl_search_edittext"        android:layout_width="match_parent"        android:layout_height="@dimen/view_40dp"        android:layout_below="@+id/main_title"        android:layout_marginTop="@dimen/view_10dp"        >        <TextView            android:id="@+id/edit_search"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:width="@dimen/view_1dp"            android:layout_marginLeft="14dp"            android:layout_marginRight="14dp"            android:hint="搜索"            android:paddingLeft="@dimen/view_20dp"            android:drawableLeft="@drawable/inbox_search"            android:background="@drawable/mail_search_deittext_shape"            android:textColor="#898989"            android:paddingRight="@dimen/view_5dp"            />    RelativeLayout>    <com.xjr2.view.PullToRefreshView        android:id="@+id/pull2refresh"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="@android:color/white"        android:layout_below="@+id/rl_search_edittext"        android:layout_alignLeft="@+id/rl_search_edittext"        android:layout_alignStart="@+id/rl_search_edittext"        android:orientation="vertical">        <ListView            android:id="@+id/mailinbox_lv"            android:layout_width="fill_parent"            android:layout_height="fill_parent">        ListView>    com.xjr2.view.PullToRefreshView>    <RelativeLayout        android:id="@+id/rl_longchose"        android:layout_width="match_parent"        android:layout_height="58dp"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:layout_centerHorizontal="true">/>        <RadioGroup            android:id="@+id/rg_longchose"            android:layout_width="match_parent"            android:layout_height="58dp"            android:gravity="center_vertical"            android:orientation="horizontal"            android:layout_alignParentRight="true"            android:layout_alignParentEnd="true"            android:background="@color/zs_zsmytask_grey_bg">            <RadioButton                android:id="@+id/rb_restor"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:button="@null"                android:text="恢复"                android:drawableTop="@drawable/restore_icon"                android:gravity="center"                android:layout_weight="1"                android:checked="true"                android:textColor="#2b85d0"                android:textSize="@dimen/text_15sp"                android:visibility="gone">            RadioButton>            <RadioButton                android:id="@+id/rb_againedit"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:button="@null"                android:text="@string/againedit"                android:drawableTop="@drawable/edit_icon"                android:gravity="center"                android:layout_weight="1"                android:checked="true"                android:textColor="#2b85d0"                android:textSize="@dimen/text_15sp"                android:visibility="gone">            RadioButton>            <RadioButton                android:id="@+id/rb_longdelete"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:button="@null"                android:text="删除"                android:drawableTop="@drawable/long_delete"                android:gravity="center"                android:layout_weight="1"                android:checked="true"                android:textColor="#2b85d0"                android:textSize="@dimen/text_15sp">            RadioButton>            <RadioButton                android:id="@+id/rb_mack"                style="@style/RadioButtonStyles"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:gravity="center"                android:layout_weight="1"                android:button="@null"                android:checked="true"                android:text="标记"                android:drawableTop="@drawable/mack_icon"                android:textColor="#2b85d0"                android:textSize="@dimen/text_15sp"                />        RadioGroup>    RelativeLayout>RelativeLayout>

下面是activity代码

/** * Created by qln on 2016/7/20. * 收件箱界面 */@ContentView(R.layout.mailinbox_layout)public class MailInboxActivity extends Activity implements View.OnClickListener,OnHeaderRefreshListener, OnFooterRefreshListener {    private ListView mailinbox_lv;    private TextView tv_choseall,tv_cencal,title_name;    private RadioButton rb_delete,rb_mack;    private RelativeLayout rl_blow,rl_search;    //是否可删除    private boolean canDel = false;    private ImageView menu;    private ImageButton edit;    private Intent intent;    private PullToRefreshView pullToRefreshView;    private QTPageBean datas;    private MailListService mailListService;    private MailListAdapter adapter;    private HashMap params = new HashMap<>();    //选中item坐标    public static List selectPosition = new ArrayList();    //菜单点击事件    private PopupWindow mPopWindow;    private RelativeLayout mailmanger_writeemail_ll;    private RelativeLayout mailmanger_inbox_ll;    private RelativeLayout mailmanger_drafts_rl;    private RelativeLayout mailmanger_hassent_rl;    private RelativeLayout mailmanger_delete_rl;    @ViewInject(R.id.main_title)    private RelativeLayout main_title;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        LogUtils.d(this.getClass().getName() + " create().");        ViewUtils.inject(this);        mailListService = new MailListService(this);        params.put("search_type", MailSearchType.ALL);        datas = new QTPageBean(GlobalContext.HD_REQUEST_COUNT, new IQTPageBeanHelper() {            @Override            public int getPosition(List list, MailList t) {                int position = -1;                String id = null;                for (int i = 0; i < list.size(); i++) {                    id = t.getEmailId();                    if (id != null && id.equals(list.get(i).getEmailId())) {                        position = i;                    }                }                return position;            }        });        initView();    }    private void initView() {        pullToRefreshView = (PullToRefreshView) findViewById(R.id.pull2refresh);        menu = (ImageView) findViewById(R.id.img_inboxmenu);        edit = (ImageButton) findViewById(R.id.img_inboxedit);        tv_choseall = (TextView) findViewById(R.id.tv_choseall);        tv_cencal = (TextView) findViewById(R.id.tv_cencal);        title_name = (TextView) findViewById(R.id.title_name);        rl_blow = (RelativeLayout) findViewById(R.id.rl_longchose);        rl_blow.setVisibility(View.GONE);        rl_search = (RelativeLayout) findViewById(R.id.rl_search_edittext);        rb_delete = (RadioButton) findViewById(R.id.rb_longdelete);        rb_mack = (RadioButton) findViewById(R.id.rb_mack);        mailinbox_lv = (ListView) findViewById(R.id.mailinbox_lv);        mailinbox_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                if (canDel) {                    selectItem(view, position);                    title_name.setText("已选择" + selectPosition.size() + "封");                } else {                    MailList maillist = datas.get(position);                    String mailId = maillist.getEmailId();                    LogUtils.d("mailId:" + mailId);                    intent = new Intent(MailInboxActivity.this, ShowMailActivity.class);                    intent.putExtra(Ids.MAILID, mailId);                    intent.putExtra("position", position);                    intent.putExtra("boxType", MailType.INBOX);                    LogUtils.d("MailType.INBOX:"+MailType.INBOX);                    startActivityForResult(intent, 0x123);                }            }        });        pullToRefreshView.setOnHeaderRefreshListener(this);        pullToRefreshView.setOnFooterRefreshListener(this);        rl_search.setOnClickListener(this);        menu.setOnClickListener(this);        edit.setOnClickListener(this);        firstPageDatas();        clickener();    }    /**     * 加载第一页数据     */    private void firstPageDatas() {        DialogManager.showProgressDialog(this, ProgressDialog.STYLE_SPINNER, "数据加载中。。。", true,                false, true);        mailListService.firstPage(datas, GlobalContext.getUrl(R.string.url_mail_index), params, new BaseServiceCallBack>() {            @Override            public void onFailed(int type, String showMessage, String detailMessage) {                switch (type) {                    case ServiceCallBack.ERROR_TYPE_SESSION_TIMEOUT:                        DialogManager.showToast(MailInboxActivity.this, "session超时,请重新登陆");                        ActivityUtil.reLogin(MailInboxActivity.this);                        break;                    case ServiceCallBack.ERROR_TYPE_NODATA:                        DialogManager.showToast(MailInboxActivity.this, "不存在该信息,请刷新列表");                        MailInboxActivity.this.finish();                        break;                    default:                        DialogManager.showToast(MailInboxActivity.this, showMessage);                        break;                }            }            @Override            public void onSuccess(List newsList) {                if (newsList.size() != 0) {                    datas.clear();                    datas.addAll(newsList);                    refreshAdapter();                } else {                    DialogManager.showToast(MailInboxActivity.this, "没有数据");                }                DialogManager.closeProgressDialog();            }        });    }    private void showPopupWindow() {        //设置contentView        View contentView = LayoutInflater.from(MailInboxActivity.this).inflate(R.layout.mail_menupopu, null);        LogUtils.d("contentView:"+contentView);        mPopWindow = new PopupWindow(contentView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);        mPopWindow.setContentView(contentView);        //设置这个参数点击外边可消失        mPopWindow.setBackgroundDrawable(new BitmapDrawable());        //点击外边窗口消失        mPopWindow.setOutsideTouchable(true);        //获得焦点,否则无法点击        mPopWindow.setFocusable(true);        //设置各个控件的点击响应        mailmanger_writeemail_ll = (RelativeLayout)contentView.findViewById(R.id.mailmanger_writeemail_ll);        mailmanger_inbox_ll = (RelativeLayout)contentView.findViewById(R.id.mailmanger_inbox_ll);        mailmanger_drafts_rl = (RelativeLayout)contentView.findViewById(R.id.mailmanger_drafts_rl);        mailmanger_hassent_rl = (RelativeLayout)contentView.findViewById(R.id.mailmanger_hassent_rl);        mailmanger_delete_rl = (RelativeLayout)contentView.findViewById(R.id.mailmanger_delete_rl);        mailmanger_writeemail_ll.setOnClickListener(this);        mailmanger_inbox_ll.setOnClickListener(this);        mailmanger_drafts_rl.setOnClickListener(this);        mailmanger_hassent_rl.setOnClickListener(this);        mailmanger_delete_rl.setOnClickListener(this);        mPopWindow.showAsDropDown(main_title, 0, 0);        setBgAlpha(10f);    }    public void setBgAlpha(float alpa) {        WindowManager.LayoutParams lp = getWindow().getAttributes();        lp.alpha = alpa; // 0.0-1.0        getWindow().setAttributes(lp);    }    private void refreshAdapter() {        adapter = new MailListAdapter(datas,selectPosition,this, MailType.INBOX);        adapter.setCanDel(canDel);        mailinbox_lv.setAdapter(adapter);        adapter.notifyDataSetChanged();    }    @Override    public void onFooterRefresh(PullToRefreshView view) {        mailListService.nextPage(datas, GlobalContext.getUrl(R.string.url_mail_index), params, new BaseServiceCallBack>() {            @Override            public void onFailed(int type, String showMessage, String detailMessage) {                switch (type) {                    case ServiceCallBack.ERROR_TYPE_SESSION_TIMEOUT:                        DialogManager.showToast(MailInboxActivity.this, "session超时,请重新登录");                        break;                    case ServiceCallBack.ERROR_TYPE_NODATA:                        DialogManager.showToast(MailInboxActivity.this, "不存在该信息,请刷新列表");                        MailInboxActivity.this.finish();                        break;                    default:                        DialogManager.showToast(MailInboxActivity.this, showMessage);                        break;                }                pullToRefreshView.onFooterRefreshComplete();            }            @Override            public void onSuccess(List newsList) {                pullToRefreshView.onFooterRefreshComplete();                if (newsList.size() == 0) {                    DialogManager.showToast(MailInboxActivity.this, R.string.msg_nodata);                }                for (int i = 0; i < newsList.size(); i++) {                    datas.addTAt(newsList.get(i), datas.size());                }                mailinbox_lv.setSelection(datas.size());            }            @Override            public void onStart() {            }            @Override            public void onCancel() {            }        });    }    private void clickener() {        mailinbox_lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {                menu.setVisibility(View.GONE);                edit.setVisibility(View.GONE);                tv_choseall.setVisibility(View.VISIBLE);                tv_cencal.setVisibility(View.VISIBLE);                rl_blow.setVisibility(View.VISIBLE);                //显示删除选项                canDel = true;                //锁定数据刷新功能                pullToRefreshView.lock();                adapter.setCanDel(canDel);                adapter.notifyDataSetChanged();                clicks();                return true;            }        });    }    /**     * 选择item     * @param view 按下的视图     * @param position 按下的坐标     */    public void selectItem(View view,Integer position){        if (selectPosition.contains(position)) {            selectPosition.remove(position);            adapter.refreshItemSelected(position,mailinbox_lv,false);        }else{            selectPosition.add(position);            adapter.refreshItemSelected(position,mailinbox_lv,true);        }    }    private void clicks() {        //删除        rb_delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                final AlertDialogToast dialog = new AlertDialogToast(MailInboxActivity.this);                dialog.setTitle(GlobalContext.getStr(R.string.deletetext));                dialog.setPositiveButton("删除", new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        if (selectPosition.size() == 0) {                            DialogManager.showToast(MailInboxActivity.this, "没有选择删除项!");                            return;                        }                        StringBuffer mailIds = new StringBuffer();                        for (int position : selectPosition) {                            MailList mailList = datas.get(position);                            mailIds.append(mailList.getEmailId()).append(";");                        }                        mailIds.deleteCharAt(mailIds.length() - 1);                        mailListService.updateMailStatus(GlobalContext.getUrl(R.string.url),                                mailIds.toString(), MailStatus.TRASH, MailType.INBOX, new BaseServiceCallBack() {                                    @Override                                    public void onFailed(int type, String showMessage, String detailMessage) {                                        super.onFailed(type, showMessage, detailMessage);                                        switch (type) {                                            case ServiceCallBack.ERROR_TYPE_SESSION_TIMEOUT:                                                DialogManager.showToast(MailInboxActivity.this, "session超时,请重新登陆");                                                ActivityUtil.reLogin(MailInboxActivity.this);                                                break;                                            case ServiceCallBack.ERROR_TYPE_NODATA:                                                DialogManager.showToast(MailInboxActivity.this, "不存在该信息,请刷新列表");                                                MailInboxActivity.this.finish();                                                break;                                            default:                                                DialogManager.showToast(MailInboxActivity.this, showMessage);                                                break;                                        }                                    }                                    @Override                                    public void onSuccess(String s) {                                        super.onSuccess(s);                                        Object[] positions = selectPosition.toArray();                                        Arrays.sort(positions);                                        for (int i = positions.length - 1; i >= 0; i--) {                                            datas.removeAt((int) positions[i]);                                        }                                        selectPosition.clear();                                        adapter.notifyDataSetChanged();                                        title_name.setText("已选择" + selectPosition.size() + "封");                                    }                                });                        dialog.dismiss();                    }                });                dialog.setNegativeButton("取消", new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        dialog.dismiss();                    }                });                }        });        //全选        tv_choseall.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                selectPosition.clear();                for(int i = 0; i < datas.size(); i ++){                    selectPosition.add(i);                    adapter.refreshItemSelected(i,mailinbox_lv,true);                }                title_name.setText("已选择"+ selectPosition.size()+"封");            }        });        //取消        tv_cencal.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                tv_choseall.setVisibility(View.GONE);                rl_blow.setVisibility(View.GONE);                tv_cencal.setVisibility(View.GONE);                menu.setVisibility(View.VISIBLE);                edit.setVisibility(View.VISIBLE);                //取消删除选项                canDel = false;                pullToRefreshView.unlock();                adapter.setCanDel(canDel);                adapter.notifyDataSetChanged();                selectPosition.clear();                title_name.setText(GlobalContext.getStr(R.string.mail_inbox));            }        });    }    }}

更多相关文章

  1. Android 在非UI线程中读网络数据
  2. android项目,从服务器端返回JSON数据,在android客户端进行显示。
  3. Android之读取手机内存中的文件数据
  4. GreenDao自动生成Android数据库操作代码
  5. Android Intent传递数据从一个Activity到另一个Activity
  6. Android 实现与Eclipse数据交互
  7. Android get+post传值获取php服务端的json数据
  8. android 常用数据库字段描述

随机推荐

  1. Android(安卓)程序退出的办法
  2. 关于打印日志的使用
  3. android定时任务实现
  4. "adb"不是内部命令和sudo: adb: command
  5. Android之使用传感器获取相应数据
  6. Android(安卓)图像处理(类型转换,比例缩放,
  7. Android(安卓)引用库项目,Debug 库项目
  8. android 实现按钮旋转720°
  9. Android(安卓)– 自定义Loading圆点
  10. 【Android病毒分析报告】 - Obad