listview条目中按钮点击事件的实现

作者: wxyass 分类: Android 发布时间: 2017-03-10 14:53

转载: http://blog.csdn.net/waniu123/article/details/49532451

大家都知道Android中listview是一个常用的控件,也是一个比较复杂的控件,一般实现listview的条目点击事件就是给listview实现setOnItemClickListener的方法就行,但是当我们遇到listview中的有按钮的时候想实现按钮的点击事件这样做是不行的,比如QQ空间的说说评论和点赞其实就是相当于两个按钮。

一般实现这两个按钮的点击事件归结为一种思想—接口回调。具体实现的话有两种方法,一种是通过实现接口方法,一种是通过实现抽象方法。当然接口和抽象类在java中其实差不多的东西,虽然两者有很大不同,但是他们有着一样的功能。

两种方法都有实现,其中还应当注意一般每个控件都有一个setTag()的方法,这个方法在这中问题上起着至关重要的作用。一般可以用来记录按钮的位置之类的.

方法一:
抽象类的实现:

package com.example.listviewbuttonclick.adapter;  

import android.view.View;  
import android.view.View.OnClickListener;  

public abstract class IClick implements OnClickListener {  
    @Override  
    public void onClick(View v) {  
        listViewItemClick((Integer) v.getTag(), v);  
    }  

    public abstract void listViewItemClick(int position, View v);  

}  

适配器

package com.example.listviewbuttonclick.adapter;  

import java.util.List;  

import com.example.listviewbuttonclick.R;  

import android.content.Context;  
import android.util.Log;  
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 ContentAdapter extends BaseAdapter {  

    private static final String TAG = "ContentAdapter";  
    private List<String> mContentList;  
    private LayoutInflater mInflater;  
    private IClick mListener;  

    public ContentAdapter(Context context, List<String> contentList,  
            IClick listener) {  
        mContentList = contentList;  
        mInflater = LayoutInflater.from(context);  
        mListener = listener;  
    }  

    @Override  
    public int getCount() {  
        Log.i(TAG, "getCount");  
        return mContentList.size();  
    }  

    @Override  
    public Object getItem(int position) {  
        Log.i(TAG, "getItem");  
        return mContentList.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        Log.i(TAG, "getItemId");  
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        Log.i(TAG, "getView");  
        ViewHolder holder = null;  
        if (convertView == null) {  
            convertView = mInflater.inflate(R.layout.list_item, null);  
            holder = new ViewHolder();  
            holder.textView = (TextView) convertView  
                    .findViewById(R.id.textView1);  
            holder.button = (Button) convertView.findViewById(R.id.button1);  
            convertView.setTag(holder);  
        } else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
        holder.textView.setText(mContentList.get(position));  
        holder.button.setOnClickListener(mListener);  
        holder.button.setTag(position);  
        return convertView;  
    }  

    public class ViewHolder {  
        public TextView textView;  
        public Button button;  
    }  

}   

方法二:
接口实现:

package com.example.listviewbuttonclick.adapter.inter;  

import android.view.View;  

public interface InterClick {  
    public void commentClick(View v);  

    public void shareClick(View v);  

}

适配器

package com.example.listviewbuttonclick.adapter;  

import java.util.List;  

import com.example.listviewbuttonclick.R;  
import com.example.listviewbuttonclick.adapter.inter.InterClick;  

import android.content.Context;  
import android.util.Log;  
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 ContentAdapter extends BaseAdapter implements OnClickListener {  

    private static final String TAG = "ContentAdapter";  
    private List<String> mContentList;  
    private LayoutInflater mInflater;  
    private InterClick mCallback;  

    public ContentAdapter(Context context, List<String> contentList,  
            InterClick callback) {  
        mContentList = contentList;  
        mInflater = LayoutInflater.from(context);  
        mCallback = callback;  
    }  

    @Override  
    public int getCount() {  
        Log.i(TAG, "getCount");  
        return mContentList.size();  
    }  

    @Override  
    public Object getItem(int position) {  
        Log.i(TAG, "getItem");  
        return mContentList.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        Log.i(TAG, "getItemId");  
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        Log.i(TAG, "getView");  
        ViewHolder holder = null;  
        if (convertView == null) {  
            convertView = mInflater.inflate(R.layout.list_item, null);  
            holder = new ViewHolder();  
            holder.textView = (TextView) convertView  
                    .findViewById(R.id.textView1);  
            holder.button1 = (Button) convertView.findViewById(R.id.button1);  
            holder.button2 = (Button) convertView.findViewById(R.id.button2);  

            convertView.setTag(holder);  
        } else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
        holder.textView.setText(mContentList.get(position));  
        holder.button1.setOnClickListener(this);  
        holder.button2.setOnClickListener(this);  

        // 设置位置,获取点击的条目按钮  
        holder.button1.setTag(position);  
        holder.button2.setTag(position);  
        return convertView;  
    }  

    public class ViewHolder {  
        public TextView textView;  
        public Button button1;  
        public Button button2;  

    }  

    // 响应按钮点击事件,调用子定义接口,并传入View  
    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
        case R.id.button1:  
            mCallback.commentClick(v);  
            break;  
        case R.id.button2:  
            mCallback.shareClick(v);  
            break;  
        default:  
            break;  
        }  
    }  
}  

这两种分别是不同的方法实现的按钮点击事件,具体实现都在源码中
源码下载: http://oss.wxyass.com/wxyass/demo/listviewButtonClick.rar

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

邮箱地址不会被公开。 必填项已用*标注