ListView中的item存在EditText时,焦点问题解决方法

问题描述

ListView中的item存在EditText时,发现 edittext每次焦点丢失 item的onItemClick 事件焦点丢失。

解决方法

 package com.threepapa.virtualmachine.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.threepapa.virtualmachine.R;
import com.threepapa.virtualmachine.bean.SongBean;
import com.threepapa.virtualmachine.utils.DebugLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2017/3/2.
 */

public class EditListAdapter extends BaseAdapter {

    private List mData;
    private int index = -1;

    public void setData(List data) {
        mData = data;
        this.notifyDataSetChanged();
    }

    public void addItem() {
        final SongBean songBean = new SongBean();
        songBean.setName("添加测试");
        songBean.setMac("AABBCCDDEEFF");
        mData.add(0, songBean);
        this.notifyDataSetChanged();
    }

    public void deletItem(int pos) {
        DebugLog.e(pos + "删除的position");
        mData.remove(pos);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listview_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        holder.tvMac.setText(mData.get(i).getName());
        EditText etMac = holder.etMac;

        //使listview中的edittext获取焦点
        etMac.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                // 在TOUCH的UP事件中,要保存当前的行下标,因为弹出软键盘后,整个画面会被重画
                // 在getView方法的最后,要根据index和当前的行下标手动为EditText设置焦点
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = i;
                }
                return false;
            }
        });

        etMac.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                DebugLog.e(i + " onTextChanged-----" + s.toString());
                //如果该edittext有默认内容,还要在if那里进行过滤
                if (index >= 0 && s.length() > 0 && index == i) {
                    mData.get(index).setMac(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                DebugLog.e(" mac " + s.toString());

            }
        });

        etMac.setText(mData.get(i).getMac());
        etMac.clearFocus();

        if (index != -1 && index == i) {
            // 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
            etMac.requestFocus();
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DebugLog.e(i+"??????????????"+holder.tvMac.getText());
            }
        });
        holder.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugLog.e(" 删除设备 position = " + i);
                deletItem(i);
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_mac)
        TextView tvMac;
        @BindView(R.id.btn_del)
        Button btnDel;
        @BindView(R.id.et_mac)
        EditText etMac;
        @BindView(R.id.rl_item)
        RelativeLayout rlItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}

你可能感兴趣的:(ListView中的item存在EditText时,焦点问题解决方法)