android ListView点击item删除事件的实现

时间:2022-05-25 19:47:58

android ListView点击item删除事件的实现android ListView点击item删除事件的实现


2016 -10 -12   修改

之前只是为自己做个笔记,没想到还挺多人需要这个,现在对代码进行了优化,并附上源码,希望能够帮助到需要的同行

activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add" />

</LinearLayout>


---------------------------------------------------------------

message_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add" />

</LinearLayout>

--------------------------------------------------------------------------------------------

package com.example.yangyoulistviewadddelete;


import java.util.ArrayList;


import com.example.yangyoulistviewadddelete.MessageAdapter.DeleteCallback;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends Activity implements OnClickListener,
        DeleteCallback {


    private ListView lv;
    private Button add;
    ArrayList<String> data = new ArrayList<String>();
    int count ;
    private MessageAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        add = (Button) findViewById(R.id.add);
        adapter = new MessageAdapter(this);
        adapter.setData(data);
        adapter.setDeleteCallback(this);
        lv.setAdapter(adapter);
        add.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        count = count+1;
        data.add("A" + count);
        adapter.setData(data);
        adapter.notifyDataSetInvalidated();
    }


    @Override
    public void deletePosition(int position) {
        data.remove(position);
        adapter.setData(data);
        adapter.notifyDataSetInvalidated();
    }
}

-----------------------------------------------------------------------------------------------------------------------------------

package com.example.yangyoulistviewadddelete;


import java.util.ArrayList;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;


public class MessageAdapter extends BaseAdapter {


    private ArrayList<String> data;
    private LayoutInflater mInflater;
    private int saveposition;
    private DeleteCallback Callback;


    public MessageAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
    }


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


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }


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


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {


        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.message_item, null);
            holder.name = (TextView) convertView.findViewById(R.id.tv_titel);
            holder.viewBtn = (Button) convertView.findViewById(R.id.bt_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.name.setText(data.get(position));


        holder.viewBtn.setTag(position);// TODO 这里是关键


        holder.viewBtn.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
                saveposition = Integer.parseInt(v.getTag().toString());// TODO
                Callback.deletePosition(saveposition);
            }
        });


        return convertView;
    }


    // 提取出来方便点
    public final class ViewHolder {
        public TextView name;
        public Button viewBtn;
    }


    public interface DeleteCallback {


        void deletePosition(int saveposition);
      
    }
    public void setDeleteCallback(DeleteCallback  Callback ){
        this.Callback=Callback;
    }


    public void setData(ArrayList<String> data2) {
       this.data=data2;
    } 
}

/**
 * ListView 与ScrollView 布局嵌套 高度计算工具类
 *
 */
public class ListViewAdapterUtil {
public static  void setListViewHeightBasedOnChildren(ListView listView) {  
       ListAdapter listAdapter =  listView.getAdapter();   
        if (listAdapter == null) {  
            // pre-condition  
            return;  
        }  
  
        int totalHeight = 0;  
        if(listAdapter.getCount() == 0){
        return;
        }else{
        for (int i = 0; i < listAdapter.getCount(); i++) {  
                 View listItem = listAdapter.getView(i, null, listView);  
                 listItem.measure(0, 0);  
                 totalHeight += listItem.getMeasuredHeight();  
             }  
       
             ViewGroup.LayoutParams params = listView.getLayoutParams();  
             params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
             listView.setLayoutParams(params);  
         }  
        }
       
}android ListView点击item删除事件的实现

源码下载地址 http://download.csdn.net/detail/anroidyanyou/9651567