RecyclerView的使用与添加单击(长按)监听事件

使用

  1、添加依赖
    无论从IDE设置项目环境中搜索添加依赖还是在Gradle的配置文件中添加均可,是标准的RecyclerView就行
    因为没有找到网上所谓的v7支持包中的,个人使用最新androidx合成系列中的RecyclerView

implementation ‘androidx.recyclerview:recyclerview:1.1.0’

  2、资源文件
    (具体配置项视个人布局而定)

app:layout_constraintTop_toTopOf=“parent”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/search_list"/>

  3、定义适配器

//先定义ViewHolder布局块类,再写入泛型
public class JobsAdapter extends RecyclerView.Adapter<JobsAdapter.ViewHolder> {
    private List<Job> jobs;

    public JobsAdapter(List<Job> jobList){
        jobs = jobList;
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView jobCategory,jobDuration,jobPosition,jobSalary,jobDate;
        private ViewHolder (View view) {
            super(view);
            jobCategory = view.findViewById(R.id.job_category);
            jobDuration = view.findViewById(R.id.job_duration);
            jobPosition = view.findViewById(R.id.job_position);
            jobSalary = view.findViewById(R.id.job_salary);
            jobDate = view.findViewById(R.id.job_date);
        }
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job,parent,false);
        return new ViewHolder(view,onItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Job job = jobs.get(position);
        holder.jobCategory.setText(job.getCategory());
        holder.jobDuration.setText(job.getDuration());
        holder.jobSalary.setText(job.getSalary());
        holder.jobDate.setText(job.getDate());
    }

    @Override
    public int getItemCount() {
        return jobs.size();
    }
}

实现监听

  因为RecyclerView并没有自带的单击长按事件监听,主要通过个人自定义实现:
  1、自定义监听

在适配器中定义接口与方法
在ViewHolder实现单击事件的回调
这样通过适配器设置了监听器,每个ViewHolder被点击都会使用onItemClick接口方法(个人重写)

public class JobsAdapter extends RecyclerView.Adapter<JobsAdapter.ViewHolder> {
    private List<Job> jobs;
    private OnItemClickListener onItemClickListener;

    public JobsAdapter(List<Job> jobList){
        jobs = jobList;
    }
    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView jobCategory,jobDuration,jobPosition,jobSalary,jobDate;
        OnItemClickListener onItemClickListener;
        private ViewHolder (View view,OnItemClickListener onItemClickListener) {
            super(view);
            jobCategory = view.findViewById(R.id.job_category);
            jobDuration = view.findViewById(R.id.job_duration);
            jobPosition = view.findViewById(R.id.job_position);
            jobSalary = view.findViewById(R.id.job_salary);
            jobDate = view.findViewById(R.id.job_date);
            this.onItemClickListener = onItemClickListener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(onItemClickListener != null)
                onItemClickListener.onItemClick(getAdapterPosition());
        }
    }
    public interface  OnItemClickListener{
        void onItemClick(int postion);
    }
    public void setItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job,parent,false);
        return new ViewHolder(view,onItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Job job = jobs.get(position);
        holder.jobCategory.setText(job.getCategory());
        holder.jobDuration.setText(job.getDuration());
        holder.jobPosition.setText(job.getPosition());
        holder.jobSalary.setText(job.getSalary());
        holder.jobDate.setText(job.getDate());
    }


    @Override
    public int getItemCount() {
        return jobs.size();
    }
}

或者

public class JobsAdapter extends RecyclerView.Adapter<JobsAdapter.ViewHolder> {
    private List<Job> jobs;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public JobsAdapter(List<Job> jobList){
        jobs = jobList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        View itemView;
        TextView jobCategory,jobDuration,jobPosition,jobSalary,jobDate;

        private ViewHolder (View view) {
            super(view);
            itemView = view;
            jobCategory = view.findViewById(R.id.job_category);
            jobDuration = view.findViewById(R.id.job_duration);
            jobPosition = view.findViewById(R.id.job_position);
            jobSalary = view.findViewById(R.id.job_salary);
            jobDate = view.findViewById(R.id.job_date);
        }
    }
    public interface  OnItemClickListener{
        void onItemClick(int position);
    }
    public void setItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
    public interface  OnItemLongClickListener{
        void onItemLongClick(int position);
    }
    public void setItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        if(onItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getAdapterPosition();
                    onItemClickListener.onItemClick(position);
                }
            });
        }
        if(onItemLongClickListener!=null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getAdapterPosition();
                    onItemLongClickListener.onItemLongClick(position);
                    return true;
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Job job = jobs.get(position);
        holder.jobCategory.setText(job.getCategory());
        holder.jobDuration.setText(job.getDuration());
        holder.jobPosition.setText(job.getPosition());
        holder.jobSalary.setText(job.getSalary());
        holder.jobDate.setText(job.getDate());
    }


    @Override
    public int getItemCount() {
        return jobs.size();
    }
}
JobsAdapter adapter = new JobsAdapter(jobs);
adapter.setItemClickListener(new JobsAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(int postion) {
        Intent intent = new Intent(SearchActivity.this,DetailActivity.class);
        intent.putExtra("job",jobs.get(postion));
        startActivity(intent);
    }
});
searchList.setAdapter(adapter);

  2、使用OnItemTouchListener拦截动作进行控制(暂时没有研究)

你可能感兴趣的:(Android)