android 仿Listview增删数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_38201918/article/details/70135932

android 仿Listview增删数据

最近想要实现如下图。有的功能有,点击减号删除,点击蓝色字体部分添加居住地。

这里写图片描述


分析写法

第一下看到这个,我的想法是使用listview来写,然后再item中实例化减号和EditText,点击减号删除这一行,让ListView的最后一项改称为自定义的布局,可是当我使用这种方法来写的时候,出现了一个问题,因为我是在Item点击事件中进行实例化的,所以相当于要双击才会有反应。
很简单,第一次点击的响应是被item拦截了,被item消化了,这个时候才实例化了减号,然后减号才有了点击事件。自己估计了一下,可能要对点击事件进行修改,但由于鄙人对这个方面不是很了解,本着简化的想法,突然萌生了一种想法。


先上代码,已经包装成为了一个工具类,备注也写得很详细了


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baibian.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 这是一个用来代替ListView的一个布局,里面内置了方法有,
 * 初始化数据,获取所有editview的字符串
 */
public class LinearLayout_Inflaterable {
    private LayoutInflater layoutInflater;
    private Context context;
    private LinearLayout linearLayout;
    private View view;//点击添加的视图view
    private List<String> edit_string_list=null;//文本框的字符串list
    private List<String> data;//传入的数据源
    private List<View> layoutlist;
    private int maxChildCount;
    public LinearLayout_Inflaterable(Context context, LinearLayout linearLayout, View view , List<String> data,int maxChildCount){
        this.context=context;
        this.linearLayout=linearLayout;
        this.view=view;
        this.data=data;
        this.maxChildCount=maxChildCount-1;
        layoutlist=new ArrayList<>();

        layoutInflater= LayoutInflater.from(context);
    }
    public void initlayout(){
        for (int i =0;i<data.size();i++){
            init_add_layout(data.get(i));
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_layout();
            }
        });
    }
    //获取所有edittext的数据
    public List<String> getEdit_String_List(){
        edit_string_list=new ArrayList<String>();
        for(int i=0;i<linearLayout.getChildCount();i++){
            EditText editText=(EditText) linearLayout.getChildAt(i).findViewById(R.id.edit_text);
            String string= editText.getText().toString();
            edit_string_list.add(string);
        }
        return edit_string_list;
    }
    //初始化时有元素传入的添加布局
    private void init_add_layout(String string ){
       final View textlayout=layoutInflater.inflate(R.layout.edit_information_listitem,null);
        layoutlist.add(textlayout);

        linearLayout.addView(textlayout);
        View remove_btn=(View) textlayout.findViewById(R.id.remove_btn);
         EditText textView=(EditText) textlayout.findViewById(R.id.edit_text);
        textView.setText(string);

        remove_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linearLayout.removeView(textlayout);
            }});
    }
    //点击增加的无数据传入的时候使用添加布局
    private void add_layout(){
        if (linearLayout.getChildCount()<=maxChildCount){
            final View textlayout=layoutInflater.inflate(R.layout.edit_information_listitem, null);
            layoutlist.add(textlayout);
            linearLayout.addView(textlayout);
            View remove_btn=(View) textlayout.findViewById(R.id.remove_btn);
            EditText textView=(EditText) textlayout.findViewById(R.id.edit_text);
            remove_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linearLayout.removeView(textlayout);
                }
            });
        }else {
            int realmaxChildCount =maxChildCount+1;
            Toast.makeText(context,context.getString(R.string.maxadd)+ realmaxChildCount  +context.getString(R.string.maxadd2),Toast.LENGTH_SHORT).show();
        }

    }
    //获取linearlayout子布局数量
    public int getChildCount(){
        return linearLayout.getChildCount();
    }
}

使用的代码

 house_linearlayout_inflaterable=new LinearLayout_Inflaterable(this,house_LinearLayout,add_house_layout,data,10
        );
        house_linearlayout_inflaterable.initlayout();

xml文件 edit_informationlayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:clickable="true"
        android:id="@+id/remove_btn"
        android:layout_gravity="center_vertical"
        android:src="@mipmap/red_minus"
        android:layout_width="20dp"
        android:layout_height="20dp" />
    <EditText
        android:singleLine="true"
        android:id="@+id/edit_text"
        android:maxLength="15"
        android:paddingTop="3dp"
        android:background="@null"
        android:textSize="15sp"
        android:hint="请添加居住地"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />
</LinearLayout>

使用的地方

            <LinearLayout
                android:layout_marginLeft="3dp"
                android:layout_marginTop="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_gravity="center_vertical"
                    android:src="@mipmap/gray_house"
                    android:layout_width="20dp"
                    android:layout_height="20dp" />
                <TextView
                    android:textSize="15sp"
                    android:text="居住地"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/house_LinearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </LinearLayout>
            <include
                android:id="@+id/add_house_layout"
                layout="@layout/edit_information_listitem_bottom"/>

edit_information_listitem_bottom

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="20dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_gravity="center_vertical"
        android:src="@mipmap/blue_add"
        android:layout_width="20dp"
        android:layout_height="20dp" />
    <TextView
        android:padding="3dp"
        android:textColor="@color/baibian_back_color"
        android:textSize="15sp"
        android:text="添加居住地"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

解决的思路

利用layoutInflater来对layout进行布局的填充,点击加号的时候就填充一个新的布局,同时实例化减号,减号的响应事件是直接删除当前item.


新手入门作品,能力有限,希望大神可以进行指点,谢谢。

猜你喜欢

转载自blog.csdn.net/github_38201918/article/details/70135932