| | |
| | | package com.example.agvcontroller; |
| | | |
| | | import android.graphics.Color; |
| | | import android.text.Editable; |
| | | import android.text.TextWatcher; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.EditText; |
| | | import android.widget.TextView; |
| | | import android.widget.ImageButton; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | |
| | | |
| | | public class EditeAdapter extends RecyclerView.Adapter<EditeAdapter.ViewHolder> { |
| | | |
| | | private List<Item> itemList; |
| | | private static List<AGVCar> itemList; |
| | | |
| | | public EditeAdapter(List<Item> itemList) { |
| | | public EditeAdapter(List<AGVCar> itemList) { |
| | | this.itemList = itemList; |
| | | } |
| | | |
| | | public static class ViewHolder extends RecyclerView.ViewHolder { |
| | | EditText agvNo; |
| | | EditText ip; |
| | | ImageButton btn; |
| | | |
| | | public ViewHolder(@NonNull View itemView) { |
| | | super(itemView); |
| | | agvNo = itemView.findViewById(R.id.agvNo); |
| | | ip = itemView.findViewById(R.id.ip); |
| | | btn = itemView.findViewById(R.id.delete_button); |
| | | |
| | | // 设置 agvNo 的文本变化监听 |
| | | agvNo.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) { |
| | | // 文本变化时 |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void afterTextChanged(Editable s) { |
| | | // 文本变化之后 |
| | | int position = getAdapterPosition(); |
| | | if (position != RecyclerView.NO_POSITION) { |
| | | AGVCar item = itemList.get(position); |
| | | item.setAgvNo(s.toString()); // 更新数据源 |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 设置 ip 的文本变化监听 |
| | | ip.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) { |
| | | // 文本变化时 |
| | | } |
| | | |
| | | @Override |
| | | public void afterTextChanged(Editable s) { |
| | | // 文本变化之后 |
| | | int position = getAdapterPosition(); |
| | | if (position != RecyclerView.NO_POSITION) { |
| | | AGVCar item = itemList.get(position); |
| | | item.setIp(s.toString()); // 更新数据源 |
| | | } |
| | | } |
| | | }); |
| | | |
| | | itemView.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | |
| | | Log.i("List","123"); |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | } |
| | |
| | | void onItemClick(View view,int position); |
| | | } |
| | | |
| | | public void removeItem(int position) { |
| | | itemList.remove(position); |
| | | notifyItemRemoved(position); |
| | | notifyItemRangeChanged(position, itemList.size()); // 更新剩余项的位置 |
| | | } |
| | | |
| | | @Override |
| | | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
| | | if (itemList == null || itemList.isEmpty()) { |
| | | // 默认显示页面 |
| | | //return 1; |
| | | } else { |
| | | Item item = itemList.get(position); |
| | | AGVCar item = itemList.get(position); |
| | | int status = item.getStatus(); |
| | | switch (status) { |
| | | case 0: |
| | |
| | | holder.itemView.setBackgroundColor(Color.parseColor("#90EE90")); |
| | | break; |
| | | } |
| | | holder.agvNo.setText("AGV_NO: " + item.getAgvNo()); |
| | | holder.ip.setText("AGV_IP: " + item.getIp()); |
| | | // 清除旧的 TextWatcher |
| | | holder.agvNo.removeTextChangedListener((TextWatcher) holder.agvNo.getTag()); |
| | | holder.ip.removeTextChangedListener((TextWatcher) holder.ip.getTag()); |
| | | |
| | | holder.itemView.setTag(position); |
| | | // 设置当前文本 |
| | | holder.agvNo.setText(item.getAgvNo()); |
| | | holder.ip.setText(item.getIp()); |
| | | |
| | | // 添加新的 TextWatcher |
| | | TextWatcher agvNoWatcher = 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) { |
| | | item.setAgvNo(s.toString()); |
| | | } |
| | | }; |
| | | holder.agvNo.addTextChangedListener(agvNoWatcher); |
| | | holder.agvNo.setTag(agvNoWatcher); |
| | | |
| | | TextWatcher ipWatcher = 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) { |
| | | item.setIp(s.toString()); |
| | | } |
| | | }; |
| | | holder.ip.addTextChangedListener(ipWatcher); |
| | | holder.ip.setTag(ipWatcher); |
| | | |
| | | } |
| | | // 设置删除按钮的点击事件 |
| | | holder.btn.setOnClickListener(v -> { |
| | | Log.i("List",itemList.toString()); |
| | | removeItem(position); // 调用删除方法 |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | if (itemList == null || itemList.isEmpty()) { |
| | | return 1; |
| | | return 0; |
| | | } else { |
| | | return itemList.size(); |
| | | } |