(Android学习之路)Android中listView结合自定义适配器,并实现item中button点击事件

此次是用listView结合BaseAdapter实现。

一、在activity_main中写一listview组件


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@id/button_add">
    ListView>
    <Button
        android:id="@+id/button_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="addItem"/>
RelativeLayout>

二、新建listView显示layout:list_view_layout.xml

(Android学习之路)Android中listView结合自定义适配器,并实现item中button点击事件_第1张图片


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/listView_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="你好"/>
    <Button
        android:id="@+id/button_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:text="点我"/>
LinearLayout>

三、新建一bean类:ListBean

public class ListBean {
    private String mText; //listView要显示的数据
    private int mCurrentPos; //当前位置
    private String mCurrentData; //当前位置数据

    public ListBean(String text){
        this.mText = text;
    }
    public ListBean(){}

    public String getmText() {
        return mText;
    }

    public void setmText(String mText) {
        this.mText = mText;
    }

    public int getmCurrentPos() {
        return mCurrentPos;
    }

    public void setmCurrentPos(int mCurrentPos) {
        this.mCurrentPos = mCurrentPos;
    }

    public String getmCurrentData() {
        return mCurrentData;
    }

    public void setmCurrentData(String mCurrentData) {
        this.mCurrentData = mCurrentData;
    }
}

四、自定义一适配器MyAdapter,继承自BaseAdapter

package com.dfxh.listviewdemo;

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

import java.util.List;

/**
 * 
 *      author : WangChaowei
 *      time   : 2018/2/27.
 *      desc   : 自定义适配器,继承自BaseAdapter
 * 
*/
public class MyAdapter extends BaseAdapter { private Context mContext; private List mData; private CallBackInterface mCallBackInterface; //回调接口 /** * 有参构造 * @param context * @param data */ public MyAdapter(Context context, List data, CallBackInterface callBack){ this.mContext = context; this.mData = data; this.mCallBackInterface = callBack; } /** * listView的item总数 */ @Override public int getCount() { return mData.size(); } /** * 获取每一项数据 * @param position * @return */ @Override public Object getItem(int position) { return mData.get(position); } /** * 获取数据ID * @param position * @return */ @Override public long getItemId(int position) { return position; } /** * 项显示的view * @param position * @param convertView * @param parent * @return */ @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; ListBean listBean = mData.get(position); //convertView用于将加载好的布局进行缓存,先判断是否为空,可优化listView if (convertView == null){ viewHolder = new ViewHolder(); //1、引用layout布局 convertView = LayoutInflater.from(mContext).inflate(R.layout.list_view_layout, parent, false); //2、填充当前项的数据 viewHolder.textView = convertView.findViewById(R.id.listView_TextView); viewHolder.button = convertView.findViewById(R.id.button_item); convertView.setTag(viewHolder); //3、在view上保存所需数据 }else { viewHolder = (ViewHolder) convertView.getTag(); } //设置数据 viewHolder.textView.setText(listBean.getmText()); listBean.setmCurrentPos(position); //listview中的点击事件 viewHolder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCallBackInterface.callBackClick(position); } }); return convertView; } /** * ViewHolder通常出现在适配器里,为的是listview滚动的时候快速设置值,而不必每次都重新创建很多对象,从而提升性能 */ private static class ViewHolder{ TextView textView; Button button; } }

五、新建一回调接口,用于操作在Item中button的点击回调(也可用抽象类回调,或者自定义点击事件)

public interface CallBackInterface {
    void callBackClick(int position);
}

六、MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener,CallBackInterface{
    private Button button_add;

    private ListView listView;
    private MyAdapter myAdapter;
    private List mData;

    private ListBean listBean;
    private int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }
    /**
     * 初始化组件
     */
    private void initView() {
        button_add = (Button)findViewById(R.id.button_add);
        listView = (ListView)findViewById(R.id.listView);

        button_add.setOnClickListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"Item点击,第" + position + "行",Toast.LENGTH_SHORT).show();
                button_add.setText("获取该行的值:" + mData.get(position).getmText());
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mData = new ArrayList<>();
        mData.add(new ListBean("来数数吧"));

        myAdapter = new MyAdapter(this,mData,this);
        listView.setAdapter(myAdapter); //listView关联适配器
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_add :
                listBean = new ListBean();
                listBean.setmText(String.valueOf(num));

                mData.add(listBean);
                myAdapter.notifyDataSetChanged(); //更新适配器

                num ++;
            break;
        }
    }

    @Override
    public void callBackClick(int position) {
        Toast.makeText(this,"按钮点击,当前行:" + (position + 1),Toast.LENGTH_SHORT).show();
    }
}


这样只能点击Item中的button有点击事件。
若可点击Item,也可点击Item中的button,就要在button 布局中加入取消焦点:

(Android学习之路)Android中listView结合自定义适配器,并实现item中button点击事件_第2张图片

android:focusable="false"

这样既可点击Item,也可以点击Item中的button,并都有事件触发

你可能感兴趣的:(Android)