Android ListView的item为CheckBox,EditText,Spinner的时候处理滑动数据错乱

MyAdapter 中的关键代码

package com.listviewtest;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuqiang on 2017/3/6.
 */

public class MyAdapter extends BaseAdapter {
    private Context context;
	//spinner数据源
    private String [] items={"请选择","同意","不同意"};
	//保存CheckBox选中状态
    private Map<Integer,Boolean> isCheck=new HashMap<Integer, Boolean>();
	//保存Spinner选中的position
    private Map<Integer,Integer> isSpin=new HashMap<Integer, Integer>(); 
	//保存EditText输入的值
    private Map<Integer,String> isText=new HashMap<Integer, String>();
    private int postion;
    public MyAdapter() {
        super();
    }
    public MyAdapter(Context context) {
        super();
        this.context=context;
		//数据初始化
        for (int i=0;i<20;i++){
            isCheck.put(i,false);
            isSpin.put(i,0);
            isText.put(i,"");
        }
    }

    @Override
    public int getCount() {
        return 20;
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
    ViewHolder vh;
    ArrayAdapter<String> adapter;
    @Override
    public View getView(int position, View v, ViewGroup parent) {
        if(v==null){
            v= LayoutInflater.from(context).inflate(R.layout.list_item,null);
            vh=new ViewHolder(v);
			//在控件初始化之后绑定相应事件
			//CheckBox选项发生改变
            vh.checkBox.setOnCheckedChangeListener(new MyOnCheckedChangeListener(vh.checkBox));
			//spinner选中项发生改变
            vh.spinner.setOnItemSelectedListener(new MyOnOnItemSelectedListener(vh.spinner));
            adapter=new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,items);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			//EditText文本框值发生改变
            vh.editText.addTextChangedListener(new MyTextChangedListener(vh.editText));
            v.setTag(vh);
        }else{
            vh= (ViewHolder) v.getTag();
        }
		//在这里给控件添加Tag值为当前ItemView的position
        vh.checkBox.setTag(position);
        vh.spinner.setTag(position);
        vh.editText.setTag(position);
        vh.spinner.setAdapter(adapter);
		//为控件赋值
        vh.checkBox.setChecked(isCheck.get(position));
        vh.spinner.setSelection(isSpin.get(position));
        vh.editText.setText(isText.get(position));
        return v;
    }

    class MyTextChangedListener implements TextWatcher{
        private EditText et_text;

        public MyTextChangedListener(EditText et_text) {
            this.et_text = et_text;
        }

        public MyTextChangedListener() {
            super();
        }

        @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) {
		    //获取该控件的tag值,并以次值为key来保存数据
            int index= (int) et_text.getTag();
            isText.put(index,s.toString());
        }
    }

    class MyOnOnItemSelectedListener implements AdapterView.OnItemSelectedListener{
        public MyOnOnItemSelectedListener(Spinner sp_test) {
            this.sp_test = sp_test;
        }
        private Spinner sp_test;

        public MyOnOnItemSelectedListener() {
            super();
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		//获取该控件的tag值,并以次值为key来保存数据
            int index= (int) sp_test.getTag();
            isSpin.put(index,position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }

    class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
        private CheckBox checkBox;

        public MyOnCheckedChangeListener(CheckBox checkBox) {
            this.checkBox = checkBox;
        }

        public MyOnCheckedChangeListener() {
            super();
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		//获取该控件的tag值,并以次值为key来保存数据
            int index= (int) checkBox.getTag();
            isCheck.put(index,isChecked);
        }
    }

    class ViewHolder{
        private Spinner spinner;
        private CheckBox checkBox;
        private EditText editText;
       public ViewHolder (){
           super();
       }
        public ViewHolder (View view){
            spinner= (Spinner) view.findViewById(R.id.sp_test);
            checkBox= (CheckBox) view.findViewById(R.id.ch_test);
            editText= (EditText) view.findViewById(R.id.et_test);
        }
    }

}

通过清单文件和xml属性设置,完成EditText弹出输入框EditText失去焦点需要二次点击情况和CheckBox和listView点击事件冲突问题

//清单文件添加android:windowSoftInputMode属性
 	<activity android:name=".MainActivity"
                  android:windowSoftInputMode="adjustPan|stateHidden"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
//listView页面添加descendantFocusability属性
	<ListView
        android:id="@+id/list_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:descendantFocusability="beforeDescendants"
        android:layout_alignParentStart="true"/>

//item页面

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    >

    <CheckBox
        android:id="@+id/ch_test"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center"
        />

    <EditText
        android:id="@+id/et_test"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        />

    <Spinner
        android:id="@+id/sp_test"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        />
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值