安卓基础(点击项目)

代码集合

从数据库把记录好的组件放入组件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> 
相关推荐
二流小码农13 分钟前
鸿蒙开发:应用内如何做更新
android·ios·harmonyos
兰琛1 小时前
Compose仿微信底部导航栏NavigationBar :底部导航控制滑动并移动
android·android jetpack
wzj_what_why_how1 小时前
Kotlin JVM 注解详解
android·kotlin
雨白1 小时前
Android UI入门:XML与常用控件的使用
android
试行2 小时前
Android获取设备信息
android
monkey_slh2 小时前
JS逆向案例—喜马拉雅xm-sign详情页爬取
android·开发语言·javascript
奔跑吧 android3 小时前
【android bluetooth 案例分析 04】【Carplay 详解 3】【Carplay 连接之车机主动连手机】
android·bluetooth·carplay·bt·gd·aosp13
奔跑吧 android3 小时前
【android bluetooth 案例分析 04】【Carplay 详解 2】【Carplay 连接之手机主动连车机】
android·bluetooth·carplay·bt·aosp13
叶羽西3 小时前
Android15 userdebug版本不能remount
android
TeleostNaCl4 小时前
Windows | 总误按Num Lock?修改注册表永久禁用Numlk键使小键盘一直输入数字
windows·经验分享·电脑