完成安卓的记账本的基本框架的构成:主要今天完成的是外框和fragment和适配器
适配器的代码
package com.eb.writeaccount; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Switch; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ListAdapter; import androidx.recyclerview.widget.RecyclerView; public class MyAdapter extends ListAdapter<Word,MyAdapter.MyViewHolder> { private MyViewModel myViewModel; MyAdapter( MyViewModel wordViewModel) { super(new DiffUtil.ItemCallback<Word>() { @Override public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) { return oldItem.getId() == newItem.getId(); } @Override public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) { return (oldItem.getMoney().equals(newItem.getMoney()) && oldItem.getAtime().equals(newItem.getMoney()) ); } }); this.myViewModel = wordViewModel; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View itemView; itemView = layoutInflater.inflate(R.layout.word,parent,false); final MyViewHolder holder = new MyViewHolder(itemView); return holder; } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { final Word word = getItem(position); holder.textViewNumber.setText(String.valueOf(position + 1)); holder.textViewmoney.setText(word.getMoney()); holder.textViewtime.setText(word.getAtime()); } public void onViewAttachedToWindow(@NonNull MyViewHolder holder) { super.onViewAttachedToWindow(holder); holder.textViewNumber.setText(String.valueOf(holder.getAdapterPosition() + 1)); } static class MyViewHolder extends RecyclerView.ViewHolder { TextView textViewNumber,textViewmoney,textViewtime; MyViewHolder(@NonNull View itemView) { super(itemView); textViewNumber = itemView.findViewById(R.id.textViewnumber); textViewmoney = itemView.findViewById(R.id.textViewmoney); textViewtime = itemView.findViewById(R.id.textViewtime); } } }