安卓基础(点击项目)

代码集合

从数据库把记录好的组件放入组件Arraylist里面(units)

设置units和监听器在unitAdapter适配器里面

unitAdapter.notifyDataSetChanged(); 更新

java 复制代码
import com.example.mobileagent.model.RecordedUnit;
import com.example.mobileagent.adapter.UnitAdapter;
 
private List<RecordedUnit> units = new ArrayList<>();
private List<Object> components = new ArrayList<>();


        // 设置单元列表
        unitAdapter = new UnitAdapter(this, units, unit -> addUnitToWorkflow(unit));
        rvUnits.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvUnits.setAdapter(unitAdapter);


        units.clear();
        List<RecordedUnit> loadedUnits = dbHelper.getAllRecordedUnits();
        if (loadedUnits != null) {
            units.addAll(loadedUnits);
        }
        unitAdapter.notifyDataSetChanged();

    private void addUnitToWorkflow(RecordedUnit unit) {
        components.add(unit);
        componentAdapter.notifyItemInserted(components.size() - 1);
        Toast.makeText(this, "已添加单元: " + unit.getTitle(), Toast.LENGTH_SHORT).show();
    }

适配器

把RecyclerView.Adapter继承在UnitAdapter里面

类里有静态类static class UnitViewHolder extends RecyclerView.ViewHolder

java 复制代码
package com.example.mobileagent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.mobileagent.R;
import com.example.mobileagent.model.RecordedUnit;

import java.util.List;

public class UnitAdapter extends RecyclerView.Adapter<UnitAdapter.UnitViewHolder> {
    
    public interface OnItemClickListener {
        void onItemClick(RecordedUnit unit);
    }
    
    private final Context context;
    private final List<RecordedUnit> units;
    private final OnItemClickListener listener;
    
    public UnitAdapter(Context context, List<RecordedUnit> units, OnItemClickListener listener) {
        this.context = context;
        this.units = units;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public UnitViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_unit, parent, false);
        return new UnitViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull UnitViewHolder holder, int position) {
        RecordedUnit unit = units.get(position);
        holder.bind(unit, listener);
    }
    
    @Override
    public int getItemCount() {
        return units.size();
    }
    
    static class UnitViewHolder extends RecyclerView.ViewHolder {
        private final TextView titleText;
        private final TextView coordinatesText;
        
        public UnitViewHolder(@NonNull View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.tvUnitTitle);
            coordinatesText = itemView.findViewById(R.id.tvCoordinates);
        }
        
        public void bind(RecordedUnit unit, OnItemClickListener listener) {
            titleText.setText(unit.getTitle());
            coordinatesText.setText(String.format("X=%.1f, Y=%.1f", unit.getX(), unit.getY()));
            
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(unit);
                }
            });
        }
    }
} 

xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="150dp"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="2dp"
    app:cardBackgroundColor="#E8F5E9">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="8dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tvUnitTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单元标题"
            android:textSize="16sp"
            android:textStyle="bold"
            android:maxLines="1"
            android:ellipsize="end"
            android:gravity="center"
            android:textColor="#333333"
            android:layout_marginBottom="8dp"/>

        <TextView
            android:id="@+id/tvCoordinates"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="X=0, Y=0"
            android:textSize="14sp"
            android:textColor="#333333"
            android:gravity="center"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="点击添加"
            android:textSize="12sp"
            android:textColor="#0066CC"
            android:gravity="center"
            android:layout_marginTop="8dp"/>

    </LinearLayout>

</androidx.cardview.widget.CardView> 
相关推荐
MindCareers21 分钟前
Beta Sprint Day 1-2: Alpha Issue Fixes Initiated + Mobile Project Setup
android·c语言·数据库·c++·qt·sprint·issue
龚礼鹏22 分钟前
Android应用程序 c/c++ 崩溃排查流程三——ndk-stack工具使用
android
zhengfei61126 分钟前
CVE-2025-13156 - Vitepos WooCommerce 的销售(POS) 系统漏洞
android
麻辣长颈鹿Sir31 分钟前
CMAKE指令集
linux·运维·windows·cmake·cmake指令集
奥陌陌36 分钟前
自定义view, 图片右上角显示数字
android
TheNextByte141 分钟前
将照片从Mac传输到Android 7 种可行方法
android·macos·gitee
Alice102944 分钟前
如何在windows本地打包python镜像
开发语言·windows·python
青莲8431 小时前
Java并发编程基础与进阶(线程·锁·原子类·通信)
android·前端·面试
2509_940880221 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
_李小白1 小时前
【Android FrameWork】延伸阅读: Android 进程管理
android