android ListView嵌入EditText焦点和值错误问题


ListView嵌入EditText,会发现两个问题

1.EditText无法获取焦点

2.给EditText输入值以后滑动ListView会发现很多Item的EditText值会自动赋值,导致很多重复,或者其他各种EditText值的问题


解决办法:

1.EditText无法获取焦点问题

①.在Manifest.xml中你使用的Activity添加android:windowSoftInputMode="adjustPan" />

②.ListView 布局文件 加入属性  android:descendantFocusability="beforeDescendants"


完整Demo下载地址:http://download.csdn.net/detail/getchance/9465556


2.EditText输入值滑动后乱显示问题

请参考代码:

package cn.getchance.testlistvieweditext;

/**
 * Created by chengyi on 16/3/18.
 */
public class Line{
    private int num;
    private String text;
    private boolean focus;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isFocus() {
        return focus;
    }

    public void setFocus(boolean focus) {
        this.focus = focus;
    }
}

package cn.getchance.testlistvieweditext;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
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.EditText;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by chengyi on 16/3/18.
 */
public class MyBaseAdapter extends BaseAdapter {


    private ArrayList<Line> lines;
    private LayoutInflater inflater;
    private Context context;

    public MyBaseAdapter(ArrayList<Line> list, Context context) {
        this.lines = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return lines.get(position);
    }

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

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

        ViewHolder vh;
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater.inflate(R.layout.listview_item, null);
            EditText et = (EditText)convertView.findViewById(R.id.et);
            TextView tv = (TextView)convertView.findViewById(R.id.tv);
            vh = new ViewHolder();
            vh.et = et;
            vh.tv = tv;
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        final Line line = lines.get(position);

        // step 1: remove android.text.TextWatcher added in step 5 to make sure android.text.TextWatcher
        //         don't trigger in step 2;
        // why?
        //
        // note: When an object of a type is attached to an Editable,
        //       TextWatcher's methods will be called when the EidtText's text is changed.
        //
        //       EditText use a ArrayList<TextWatcher> type object to store the listener, so we must
        //       make sure there's only one TextWatcher object in this list;
        //
        // Avoid triggering TextWatcher's method in step 2 we remove it at first time.
        //
        if (vh.et.getTag() !=null&&vh.et.getTag() instanceof TextWatcher) {
            vh.et.removeTextChangedListener((TextWatcher) (vh.et.getTag()));
        }

        // step 2: set text and focus after remove android.text.TextWatcher(step 1);
        vh.et.setHint(position + ".");

        // set text
        if (TextUtils.isEmpty(line.getText())) {
            vh.et.setTextKeepState("");
        } else {
            vh.et.setTextKeepState(line.getText());
        }

        // set focus status
        // why?
        //
        // note: ListView has a very elegant recycle algorithm. So views in ListView is not reliable.
        //       Especially in this case, EditText is an item of ListView. Software input window may cause
        //       ListView relayout leading adapter's getView() invoke many times.
        //       Above all if we change EditText's focus state directly in EditText level(not in Adapter).
        //       The focus state may be messed up when the particularly view reused in other position.
        //
        //       So using data source control View's state is the core to deal with this problem.

        if (line.isFocus()) {
            vh.et.requestFocus();
        } else {
            vh.et.clearFocus();
        }

        // step 3: set an OnTouchListener to EditText to update focus status indicator in data source
        // why?
        //
        // in step 2, we know we must control view state through data source. We use OnTouchListener
        // to watch the state change and update the data source when user move up fingers(ACTION_UP).
        // We don't want to consume the touch event, simply return false in method onTouch().

        vh.et.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    check(position);
                }
                return false;
            }
        });

        // step 4: set TextWatcher to EditText to listen text changes in EditText to updating the text in data source
        // why?
        //
        // again, use data source to control view state.
        // When user edit the text in one EditText item and scroll the ListView. The particularly EditText item will be
        // reuse in adapter's getView(), this may lead text messed up in ListView.
        // How to deal with this problem?
        // Easy! We update the text in data source at the same time when user is editing. TextWatcher is the best way to
        // do this.

        final TextWatcher watcher = 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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    line.setText(null);
                } else {
                    line.setText(String.valueOf(s));
                }
            }
        };
        vh.et.addTextChangedListener(watcher);

        // step 5: Set watcher as a tag of EditText.
        // so we can remove the same object which was setted to EditText in step 4;
        // Make sure only one callback is attached to EditText
        vh.et.setTag(watcher);

        return convertView;
    }


    private void check(int position) {
        for (Line l : lines) {
            l.setFocus(false);
        }
        lines.get(position).setFocus(true);
    }


    private class ViewHolder {
        public EditText et;
        public TextView tv;
    }

}

package cn.getchance.testlistvieweditext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private MyBaseAdapter  adapter;
    private ArrayList<Line> list;

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

        list = new ArrayList<>();

        for(int i =0;i<100;i++){
            Line l = new Line();
            l.setFocus(false);
            l.setNum(i);
            l.setText("测试"+i);
            list.add(l);
        }

        lv = (ListView)findViewById(R.id.lv);
        adapter = new MyBaseAdapter(list,this);
        lv.setAdapter(adapter);
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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="cn.getchance.testlistvieweditext.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
         />
</RelativeLayout>


listview_item.xml

<?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="40dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/shape_white" />


    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="2dp"
        android:background="@drawable/shape_white" />
</LinearLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.getchance.testlistvieweditext">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值