ackage com.example.noteapp281.adapter; import android.app.Dialog; import android.content.Context; import android.content.Intent; 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.noteapp281.EditActivity; import com.example.noteapp281.NoteDbOpenHelper; import com.example.noteapp281.R; import com.example.noteapp281.bean.Note; import com.example.noteapp281.util.ToastUtil; import java.util.List; public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<Note> mBeanList; private LayoutInflater mLayoutInflater; private Context mContext; private NoteDbOpenHelper mNoteDbOpenHelper; private int viewType; public static int TYPE_LINEAR_LAYOUT = 0; public static int TYPE_GRID_LAYOUT = 1; public MyAdapter(Context context, List<Note> mBeanList) { this.mBeanList = mBeanList; this.mContext = context; mLayoutInflater = LayoutInflater.from(mContext); mNoteDbOpenHelper = new NoteDbOpenHelper(mContext); } public void setViewType(int viewType) { this.viewType = viewType; } @Override public int getItemViewType(int position) { return viewType; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = null; if (viewType == TYPE_LINEAR_LAYOUT) { view = mLayoutInflater.inflate(R.layout.list_item_layout, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } else if (viewType == TYPE_GRID_LAYOUT) { view = mLayoutInflater.inflate(R.layout.list_item_grid_layout, parent, false); MyGridViewHolder myGridViewHolder = new MyGridViewHolder(view); return myGridViewHolder; }else { return null;
时间: 2024-04-22 12:22:05 浏览: 156
android Adapter教程
3星 · 编辑精心推荐
这段代码是关于一个 RecyclerView 的适配器的实现,其中定义了两种不同的布局类型,分别是线性布局和网格布局。根据不同的类型创建不同的 ViewHolder,并且重写了 getItemViewType() 方法来获取当前 item 的类型。同时也定义了相应的构造方法,通过传入数据和上下文,来初始化适配器。在 onCreateViewHolder() 方法里,根据不同的类型来创建不同的 ViewHolder。
阅读全文