listview长安出现复选框可以多选删除

来源:互联网 发布:手机定位窃听软件 编辑:程序博客网 时间:2024/04/29 20:05
主布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/white"    android:orientation="vertical">    <RelativeLayout        android:id="@+id/data_rela"        android:layout_width="match_parent"        android:layout_height="match_parent">        <com.jiawabang.pullableview.PullToRefreshLayout            android:id="@+id/refresh_view"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_above="@+id/relative">            <include                android:id="@+id/header_refresh"                layout="@layout/refresh_head" />            <!-- 支持所有实现Pullable接口的View -->            <com.jiawabang.pullableview.PullableScrollView                android:id="@+id/refresh_pullable_scroll"                android:layout_width="match_parent"                android:layout_height="match_parent">                <com.jiawabang.View.MyListView                    android:id="@+id/list"                    android:layout_width="match_parent"                    android:layout_height="0dp"                    android:layout_weight="1"                    android:divider="@color/qianhuise"                    android:dividerHeight="1px"                    android:footerDividersEnabled="true"                    android:headerDividersEnabled="true"                    android:scrollbars="vertical" />            </com.jiawabang.pullableview.PullableScrollView>            <include                android:id="@+id/bottom_load_more"                layout="@layout/load_more" />        </com.jiawabang.pullableview.PullToRefreshLayout>        <RelativeLayout            android:id="@+id/relative"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_marginBottom="10dp">            <Button                android:id="@+id/cancle"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerVertical="true"                android:layout_marginLeft="20dp"                android:background="@drawable/yanzheng"                android:text="撤销"                android:textColor="@color/white"                android:textSize="16sp" />            <Button                android:id="@+id/delete"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentRight="true"                android:layout_centerVertical="true"                android:layout_marginRight="20dp"                android:background="@drawable/yanzheng"                android:text="删除"                android:textColor="@color/white"                android:textSize="16sp" />        </RelativeLayout>    </RelativeLayout>    <LinearLayout        android:id="@+id/nodata_linear"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/qianhuise"        android:gravity="center">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/nodata" />    </LinearLayout>    <LinearLayout        android:id="@+id/error_linear"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/qianhuise"        android:gravity="center"        android:visibility="gone">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/lostshuaxin" />    </LinearLayout></LinearLayout> 
layout = (RelativeLayout) view.findViewById(R.id.relative);    cancle = (Button) view.findViewById(R.id.cancle);    delete = (Button) view.findViewById(R.id.delete);    cancle.setOnClickListener(this);    delete.setOnClickListener(this);    initData(pageSize, pageIndex);    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {            if (flag == 1) {                Adapter.ViewHolder holder = (Adapter.ViewHolder) view.getTag();                holder.check.toggle();                Adapter.getIsCheckedMap().put(position, holder.check.isChecked());                adapter.notifyDataSetChanged();            } else {                shezhiyidu(totailList.get(position).wmid);                holder = (Adapter.ViewHolder) view.getTag();                onclickposition = position;            }        }    });    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {        @Override        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {            flag = 1;            LogUtils.LOGI("tag", "onLongClick");            layout.setVisibility(VISIBLE);            delstr = "";            for (int i = 0; i < totailList.size(); i++) {                Adapter.getVisibleMap().put(i, CheckBox.VISIBLE);            }            adapter.notifyDataSetChanged();            return false;        }    });}
@Overridepublic void onClick(View v) {    switch (v.getId()) {        case R.id.delete:            ArrayList<XiaoXiPageModel> removeList = new ArrayList<XiaoXiPageModel>();            for (int i = 0; i < totailList.size(); i++) {                if (adapter.getIsCheckedMap().get(i)) {                    removeList.add(totailList.get(i));                    delstr = delstr + totailList.get(i).wmid + ",";                    LogUtils.LOGI("tag", "==checkid=" + delstr);                }            }            layout.setVisibility(View.GONE);            for (int i = 0; i < totailList.size(); i++) {                adapter.getVisibleMap().put(i, CheckBox.GONE);                adapter.getIsCheckedMap().put(i, false);            }            totailList.removeAll(removeList);            if (!StringUtils.isEmpty(delstr) && delstr.contains(",")) {                delstr = delstr.substring(0, delstr.length() - 1);                LogUtils.LOGI("tag", "==checkid,,,=" + delstr);            }            deldata();            flag = 0;            break;        case R.id.cancle:            layout.setVisibility(View.GONE);            for (int i = 0; i < totailList.size(); i++) {                adapter.getVisibleMap().put(i, CheckBox.GONE);                adapter.getIsCheckedMap().put(i, false);            }            adapter.notifyDataSetChanged();            flag = 0;            break;    }}




package com.jiawabang.Adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView;import com.jiawabang.Model.XiaoXiPageModel;import com.jiawabang.R;import java.util.ArrayList;import java.util.HashMap;/** * Created by Administrator on 2016/7/26 0026. */public class Adapter extends BaseAdapter {    private Context context;    private LayoutInflater inflater = null;    private static HashMap<Integer, Integer> visiblecheck;//用来记录是否显示checkBox    public static HashMap<Integer, Boolean> ischeck;    private ArrayList<XiaoXiPageModel> totailList;    public Adapter(Context context, ArrayList<XiaoXiPageModel> totailList) {        this.context = context;        this.totailList = totailList;        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        visiblecheck = new HashMap<Integer, Integer>();        ischeck = new HashMap<Integer, Boolean>();        initstuts();    }    private void initstuts() {        for (int i = 0; i < totailList.size(); i++) {            getIsCheckedMap().put(i, false);            getVisibleMap().put(i, CheckBox.GONE);        }    }    public static HashMap<Integer, Integer> getVisibleMap() {        return visiblecheck;    }    public static HashMap<Integer, Boolean> getIsCheckedMap() {        return ischeck;    }    public int getCount() {        // TODO Auto-generated method stub        return totailList.size();    }    public Object getItem(int position) {        // TODO Auto-generated method stub        return totailList.get(position);    }    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    public View getView(final int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        if (convertView == null) {            holder = new ViewHolder();            convertView = inflater.inflate(R.layout.item_fuwuxiapxi, parent, false);            holder.check = (CheckBox) convertView.findViewById(R.id.check);            holder.txtName = (TextView) convertView.findViewById(R.id.txtName);            holder.weidu_imageview = (ImageView) convertView.findViewById(R.id.weidu_imageview);            holder.riqi = (TextView) convertView.findViewById(R.id.riqi);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        holder.txtName.setText(totailList.get(position).context);        holder.riqi.setText(totailList.get(position).createtime);        holder.check.setChecked(getIsCheckedMap().get(position));        holder.check.setVisibility(getVisibleMap().get(position));        if (totailList.get(position).isread == 0) {            holder.weidu_imageview.setVisibility(View.VISIBLE);        } else {            holder.weidu_imageview.setVisibility(View.GONE);        }        return convertView;        // TODO Auto-generated method stub    }    public class ViewHolder {        public TextView txtName, riqi;        public CheckBox check;        public ImageView weidu_imageview;    }}

item布局文件



1 0
原创粉丝点击