package com.example.myapplication.recyclerview;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Adapter extends RecyclerView.Adapter {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public PuAdapter.PuViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PuViewHoder(LayoutInflater.from(mContext).inflate(R.layout.layout_pu_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public void onBindViewHolder( PuAdapter.PuViewHoder holder, int position) {
if(position % 2 != 0){
holder.imageView.setImageResource(R.drawable.icon_user1);
}else {
holder.imageView.setImageResource(R.drawable.icon_user2);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mListener.onLongClick(position);
return true;
}
});
}
@Override
public int getItemCount() {
return 0;
}
@Override
public int getItemCount() {
return 30;
}
找到视图中各个组件的
class PuViewHoder extends RecyclerView.ViewHolder{
private ImageView imageView;
public PuViewHoder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.tv_pu);
}
}
//点击方法的接口
public interface OnItemClickListener{
void onClick(int pos);
void onLongClick(int pos);
}
private Context mContext;
private PuAdapter.OnItemClickListener mListener;
public PuAdapter(Context context, PuAdapter.OnItemClickListener listener){
this.mContext = context;
this.mListener = listener;
}
package com.example.myapplication.recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
public class PuAdapter extends RecyclerView.Adapter<PuAdapter.PuViewHoder> {
private Context mContext;
private PuAdapter.OnItemClickListener mListener;
public PuAdapter(Context context, PuAdapter.OnItemClickListener listener){
this.mContext = context;
this.mListener = listener;
}
@Override
public PuAdapter.PuViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PuViewHoder(LayoutInflater.from(mContext).inflate(R.layout.layout_pu_item,parent,false));
}
@Override
public void onBindViewHolder( PuAdapter.PuViewHoder holder, int position) {
if(position % 2 != 0){
holder.imageView.setImageResource(R.drawable.icon_user1);
}else {
holder.imageView.setImageResource(R.drawable.icon_user2);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mListener.onLongClick(position);
return true;
}
});
}
@Override
public int getItemCount() {
return 30;
}
class PuViewHoder extends RecyclerView.ViewHolder{
private ImageView imageView;
public PuViewHoder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.tv_pu);
}
}
public interface OnItemClickListener{
void onClick(int pos);
void onLongClick(int pos);
}
}
mRvPu.setAdapter(new PuAdapter(PuRecyclerActivity.this, new PuAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(PuRecyclerActivity.this,"click"+pos,Toast.LENGTH_SHORT).show();
}
@Override
public void onLongClick(int pos) {
Toast.makeText(PuRecyclerActivity.this,"click..."+pos,Toast.LENGTH_SHORT).show();
}
}));
Adapter 基本用法大概就是这样,有补充和改正的地方欢迎留言