Android ListView的Item中带EditText的全输入和全取消输入

上代码:

1、MainActicity.class

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView imgMulti;
    ListView mList;
    TextView tvSave;
    String priceTitle[] ={"黄色S","黄色M","红色S","绿色XL","红色L",};
    boolean isSelect = true;
    PriceAdapter mAdapter;
    List<PriceBean.ColorSizePrice> datas = new ArrayList<>();

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

        imgMulti = (ImageView) findViewById(R.id.img_multi);
        tvSave = (TextView) findViewById(R.id.tv_save);
        imgMulti.setOnClickListener(this);
        tvSave.setOnClickListener(this);

        isSelect = false;
        imgMulti.setImageResource(R.mipmap.ic_select_up);


        mList = (ListView) findViewById(R.id.list);
        mAdapter = new PriceAdapter(MainActivity.this,priceTitle);
        mList.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_multi:
                if(isSelect){
                    isSelect = false;
                    imgMulti.setImageResource(R.mipmap.ic_select_up);
                    mAdapter.cancleAllSame();
                    Toast.makeText(MainActivity.this,"取消了",Toast.LENGTH_SHORT).show();
                }else {
                    isSelect = true;
                    imgMulti.setImageResource(R.mipmap.ic_select_down);
                    mAdapter.setAllSame();
                    Toast.makeText(MainActivity.this,"选中了",Toast.LENGTH_SHORT).show();
                }
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_save:

                break;
        }

    }
}

2、布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.edit.multimodifile.MainActivity"
    android:orientation="vertical"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center|left"
        android:padding="10dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="价格设置"
            android:textColor="#333333"
            android:textSize="15sp"/>

        <ImageView
            android:id="@+id/img_multi"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/ic_select_up"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全部相同"
            android:textColor="#333333"
            android:textSize="15sp"/>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e0e0e0"/>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/tv_save"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="15sp"
        android:textColor="#ffffff"
        android:text="保存"
        android:background="#f41212"
        android:gravity="center"/>

</LinearLayout>

3、适配器:PriceAdapter.class

public class PriceAdapter extends BaseAdapter {

    Context context;
    String priceTitle[];
    ViewHolder holder = null;
    String unitPrice;
    private SparseArray<String> sparseArray = new SparseArray<>();//SparseArrayHashMap用法一样
//    private HashMap<Integer,String> sparseArray = new HashMap<>();
    private int curPos;
    private Runnable runnable;
    private Handler handler;
    private boolean normalMode = true;

    public PriceAdapter(Context context, String priceTitle[]) {
        this.context = context;
        this.priceTitle = priceTitle;
    }

    @Override
    public int getCount() {
        return priceTitle.length;
    }

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

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

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        view = LayoutInflater.from(context).inflate(R.layout.item_price_set, null);
        holder = new ViewHolder();
        initView(view, holder);
        holder.tvColorSize.setText(priceTitle[position]);
        holder.editUnitPrice.setText(sparseArray.get(position));
        Log.e("getView", position + "---" + sparseArray.get(position));
        holder.editUnitPrice.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) {

                curPos = position;
                sparseArray.put(position, s.toString());
                unitPrice = s.toString();

                if (runnable == null) {
                    runnable = new Runnable() {
                        @Override
                        public void run() {
                            if (!normalMode) {
                                putSameValue(position);
                            }
                            notifyDataSetChanged();
                        }
                    };
                }
                if (handler == null) {
                    handler = new Handler();
                }
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, 1000);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Toast.makeText(context, "" + unitPrice, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


    private void initView(View view, ViewHolder holder) {
        holder.tvColorSize = (TextView) view.findViewById(R.id.tv_color_size);
        holder.editUnitPrice = (EditText) view.findViewById(R.id.edit_unit_price);
        view.setTag(holder);

    }

    class ViewHolder {
        TextView tvColorSize;
        EditText editUnitPrice;
    }

    public void setAllSame() {
        normalMode = false;
        for (int i = 0; i < priceTitle.length; i++) {
            sparseArray.put(i, unitPrice);
        }
    }

    private void putSameValue(int curPos) {
        for (int i = 0; i < priceTitle.length; i++) {
            if (i != curPos) {
                sparseArray.put(i, unitPrice);
            }
        }
    }

    public void cancleAllSame() {
        normalMode = true;
        for (int i = 0; i < priceTitle.length; i++) {
            if (i == curPos) {
                sparseArray.put(i, unitPrice);
            } else {
                sparseArray.put(i, "");
            }
        }
    }
}

4、item布局文件:item_price_set.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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#ffffff"
        android:gravity="center|left"
        android:orientation="horizontal"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <TextView
            android:id="@+id/tv_color_size"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:textColor="#333333"
            android:textSize="14sp"
            android:text=""
            />

        <EditText
            android:id="@+id/edit_unit_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textCursorDrawable="@drawable/color_cursor"
            android:textSize="14sp"
            android:textColorHint="#cccccc"
            android:textColor="#333333"
            android:hint="请输入"
            android:layout_weight="1"
            android:background="@null"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#333333"
            android:textSize="14sp"
            android:text=""
            android:layout_marginLeft="10dp"
            />

    </LinearLayout>

    <!--<ImageView
        style="@style/horizontal_line"
        android:layout_marginRight="@dimen/height_15"
        android:layout_marginLeft="@dimen/height_15"/>-->


</LinearLayout>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值