package com.blog; 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 java.util.ArrayList; import java.util.HashMap; public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> { private String[] examples; public SimpleAdapter(ArrayList<HashMap<String, String>> mDataList, ViewHolderListener viewHolderListener) { this.viewHolderListener = viewHolderListener; } public void setViewHolderListener(ViewHolderListener title) { } public interface ViewHolderListener { void onItemClicked(View view, int adapterPosition); void onItemClick(View view, int position); } private final ViewHolderListener viewHolderListener; public SimpleAdapter(String[] strs, ViewHolderListener viewHolderListener) { examples = strs; this.viewHolderListener = viewHolderListener; } @NonNull @Override public SimpleAdapter.ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item_layout, parent, false); return new SimpleAdapter.ViewHolder(view, viewHolderListener); } @Override public void onBindViewHolder(@NonNull SimpleAdapter.ViewHolder holder, int position) { holder.tvContent.setText(examples[position]); } @Override public int getItemCount() { return examples.length; } static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private final TextView tvContent; private final ViewHolderListener viewHolderListener; ViewHolder(View itemView, ViewHolderListener viewHolderListener) { super(itemView); tvContent = itemView.findViewById(R.id.tv_content); this.viewHolderListener = viewHolderListener; itemView.findViewById(R.id.all_layout).setOnClickListener(this); } @Override public void onClick(View view) { if (null != viewHolderListener) { viewHolderListener.onItemClicked(view, getAdapterPosition()); } } } }这段代码写的对吗
时间: 2024-03-04 08:54:01 浏览: 73
Kotlin案例分析.docx
这段代码看起来是一个简单的 RecyclerView Adapter 的实现,主要用于展示一组字符串列表。代码中使用了 ViewHolder 模式来优化列表项的创建和绑定操作,同时也定义了一个 ViewHolderListener 接口来处理列表项的点击事件。不过,代码中的两个构造函数都没有使用 mDataList 参数,可能是未完成的代码或者是复制粘贴错误。
阅读全文