一个可以增减的带EditText的RecyclerView列表

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ItemAdapter adapter;
    private ArrayList<Map<String, String>> itemsList;

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

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        itemsList = new ArrayList<>();
        adapter = new ItemAdapter(itemsList);
        recyclerView.setAdapter(adapter);

        // 添加按钮点击事件
        Button addButton = findViewById(R.id.addButton);
        Button execute = findViewById(R.id.execute);
        EditText editText = findViewById(R.id.editText);
        EditText editTextwsign = findViewById(R.id.editTextwsign);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> stringStringMap = new HashMap<>();
                String packagestr = editText.getText().toString();
                String sign = editTextwsign.getText().toString();
                stringStringMap.put("pack", packagestr);
                stringStringMap.put("sign", sign);
                itemsList.add(stringStringMap);
                adapter.notifyItemInserted(itemsList.size() - 1);
                editText.setText("");
                editTextwsign.setText("");
            }
        });
        execute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = itemsList.toString();
                Log.e("执行的数据", str);
            }
        });
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:clickable="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_01"
            style="@style/fragment_text"
            android:text="包名:"
            android:textSize="16sp" />


        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:layout_marginBottom="7dp"
            android:layout_toRightOf="@+id/tv_01"
            android:background="@null"
            android:gravity="right|center_vertical"
            android:hint="请输入包名"
            android:textColor="#ff3d3d3d"
            android:textColorHint="#ffcccccc"
            android:textSize="12sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:clickable="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_02"
            style="@style/fragment_text"
            android:text="签名:"
            android:textSize="16sp" />


        <EditText
            android:id="@+id/editTextwsign"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:layout_marginBottom="7dp"
            android:layout_toRightOf="@+id/tv_02"
            android:background="@null"
            android:gravity="right|center_vertical"
            android:hint="请输入签名"
            android:textColor="#ff3d3d3d"
            android:textColorHint="#ffcccccc"
            android:textSize="12sp" />

    </RelativeLayout>

    <Button
        android:id="@+id/addButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add" />

    <Button
        android:id="@+id/execute"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="execute" />
</LinearLayout>

package com.example.mytest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Map;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
    private ArrayList<Map<String, String>> itemsList;

    public ItemAdapter(ArrayList<Map<String, String>> itemsList) {
        this.itemsList = itemsList;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        Map<String, String> item = itemsList.get(position);
        holder.editText.setText(item.get("pack"));
        holder.editTexttwo.setText(item.get("sign"));
    }

    @Override
    public int getItemCount() {
        return itemsList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        EditText editText;
        EditText editTexttwo;
        ImageButton deleteButton;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.editText);
            editTexttwo = itemView.findViewById(R.id.editTexttwo);
            deleteButton = itemView.findViewById(R.id.deleteButton);

            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        itemsList.remove(position);
                        notifyItemRemoved(position);
                    }
                }
            });
        }
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="包名输入项" />
    <EditText
        android:id="@+id/editTexttwo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="签名输入项" />
    <ImageButton
        android:id="@+id/deleteButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_menu_delete" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值