Adapter,基本使用方法

Adapter基本使用方法

RecyclerView中适配器使用

先建一个类继承RecyclerView.Adapter

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;
    }
}

找到到你的RecyclerView视图

@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));
    }

设置RecyclerView 的样式和事件

 @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;
            }
        });
    }

返回的RecyclerView个数

@Override
    public int getItemCount() {
     
        return 0;
    }
 @Override
    public int getItemCount() {
     
        return 30;
    }

RecyclerView在activity中不能直接设置点击事件,但是可以在Adapter中自定义点击事件

找到视图中各个组件的

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);
    }

Adapter中还应该传入上下文和监听者

private Context mContext;
    private PuAdapter.OnItemClickListener mListener;

    public PuAdapter(Context context, PuAdapter.OnItemClickListener listener){
     

        this.mContext = context;
        this.mListener = listener;
    }

Adapter中的完整代码

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);
    }

}

activity中代码

 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 基本用法大概就是这样,有补充和改正的地方欢迎留言

你可能感兴趣的:(安卓,安卓)