安卓基础(点击项目)

代码集合

从数据库把记录好的组件放入组件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> 
相关推荐
雨白1 小时前
Jetpack系列(二):Lifecycle与LiveData结合,打造响应式UI
android·android jetpack
new_zhou2 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
Rocket MAN3 小时前
Rovo Dev CLI Windows 安装与使用指南
windows
kk爱闹3 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
每次的天空4 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
恋猫de小郭5 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
断剑重铸之日6 小时前
Android自定义相机开发(类似OCR扫描相机)
android
随心最为安6 小时前
Android Library Maven 发布完整流程指南
android
岁月玲珑6 小时前
【使用Android Studio调试手机app时候手机老掉线问题】
android·ide·android studio
fzyz1236 小时前
Windows系统下WSL从C盘迁移方案
人工智能·windows·深度学习·wsl