recycleview的点击,长按事件

时间:2022-04-03 15:50:44

用接口回调的方式实现点击,长按事件

1:activity:数据的集成

package com.bawei.test.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.bawei.test.R;
import com.bawei.test.adapter.MyAda;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    private ArrayList<String> list;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
                rv.setLayoutManager(manager);

                MyAda ada = new MyAda(MainActivity.this,list);
                rv.setAdapter(ada);

                *//点击事件
                ada.setOnitemClick(new MyAda.OnitemClick() {
                    @Override
                    public void itemclick(int postion) {
                        Toast.makeText(MainActivity.this,"点击了"+postion,Toast.LENGTH_SHORT).show();
                    }
                });*
                //长按
                *ada.setOnitemLongClick(new MyAda.OnitemLongClick() {
                    @Override
                    public void lonitemclick(int postion) {
                        Toast.makeText(MainActivity.this,"长按了"+postion,Toast.LENGTH_SHORT).show();
                    }
                });*

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv= (RecyclerView) findViewById(R.id.rv);

        getdata();
    }

    //数据的放入
    public void getdata(){
        String url="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3972095941,1780837612&fm=111&gp=0.jpg";
        list=new ArrayList<>();
        for(int i=0;i<50;i++){
            list.add(url);
        }
        Message message = new Message();
        message.what=1;
        handler.sendMessage(message);
    }
}

2:适配器

package com.bawei.test.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bawei.test.R;

import java.util.ArrayList;

import it.sephiroth.android.library.picasso.Picasso;

/** * 1:类的用途 * 2:@author 张倩 * 3:@date 2017/9/7 08:28 */

public class MyAda extends RecyclerView.Adapter<MyAda.MyViewHolder>{

    private Context con;
    private ArrayList<String> list;

    public MyAda(Context con, ArrayList<String> list) {
        this.con = con;
        this.list = list;
    }

//创建view
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=View.inflate(con,R.layout.item,null);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;
    }

//绑定数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        Picasso.with(con).load(list.get(position)).into(holder.iv);
        //单击接口的回调
        if(onitemClick!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onitemClick.itemclick(position);
                }
            });
        }
        //长按
        if(onitemLongClick!=null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onitemLongClick.lonitemclick(position);
                    return true;
                }
            });
        }

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        CheckBox box;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.iv);
            box= (CheckBox) itemView.findViewById(R.id.box);
        }
    }


    *//接口回调,定义点击事件
    private OnitemClick onitemClick;
    public interface OnitemClick{
        void itemclick(int postion);
    }
    public void setOnitemClick(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }
    //接口回调,定义长按事件
    private OnitemLongClick onitemLongClick;
    public interface OnitemLongClick{
        void lonitemclick(int postion);
    }
    public void setOnitemLongClick(OnitemLongClick onitemLongClick) {
        this.onitemLongClick = onitemLongClick;
    }
}*