如何实现RecyclerView onClick侦听器和Long click侦听器方法

时间:2020-04-05 18:15:22

标签: android android-recyclerview

我想实现onClick侦听器和Longclick侦听器:

ViewHolder类扩展了RecyclerView.ViewHolder实现了View.OnClickListener,View.OnLongClickListener {

        TextView titleMyText, descTextView;
        ImageView flagImageView;

        public ViewHolderMy(@NonNull View itemView) {
            super(itemView);

            titleMyText = itemView.findViewById(R.id.title_text);
            descTextView = itemView.findViewById(R.id.description_text);
            flagImageView = itemView.findViewById(R.id.flag_country);


//  ----------------Listener part----------------------------------------
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);

        }


        @Override
        public void onClick(View v) {

            clickListener.onItemClick(getAdapterPosition(), v);

        }

        @Override
        public boolean onLongClick(View v) {
            clickListener.onItemLongClick(getAdapterPosition(), v);
            return false;
        }
    }

    public interface ClickListener {
        void onItemClick(int position, View v);

        void onItemLongClick(int position, View v);
    }

    public void setOnItemClickListener(ClickListener clickListener) {
        RecyclerAdapter.clickListener = clickListener;

1 个答案:

答案 0 :(得分:0)

我添加了一些代码:

RecyclerView.java

class ViewHolderMy extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        TextView titleMyText, descTextView;
        ImageView flagImageView;

        public ViewHolderMy(@NonNull View itemView) {
            super(itemView);

            titleMyText = itemView.findViewById(R.id.title_text);
            descTextView = itemView.findViewById(R.id.description_text);
            flagImageView = itemView.findViewById(R.id.flag_country);


//  ----------------Listener part----------------------------------------
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);

        }


        @Override
        public void onClick(View v) {

            clickListener.onItemClick(getAdapterPosition(), v);

        }

        @Override
        public boolean onLongClick(View v) {
            clickListener.onItemLongClick(getAdapterPosition(), v);
            return false;
        }
    }

    public interface ClickListener {
        void onItemClick(int position, View v);

        void onItemLongClick(int position, View v);
    }

    public void setOnItemClickListener(ClickListener clickListener) {
        RecyclerAdapter.clickListener = clickListener;
    }
}

MainActivity.java

package com.example.myrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerAdapter recyclerAdapter;


    int [] images = {R.drawable.bd,R.drawable.india, R.drawable.pak,R.drawable.saudi, R.drawable.srilanka,
            R.drawable.bhutan,R.drawable.china, R.drawable.england,R.drawable.finland, R.drawable.maldiv};

    String [] title, description;

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

        recyclerView=findViewById(R.id.recyclerView);

        title = getResources().getStringArray(R.array.contry_name);
        description = getResources().getStringArray(R.array.country_description);

        recyclerAdapter=new RecyclerAdapter(this,title,description,images);
        recyclerView.setAdapter(recyclerAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

//   --------------- On Item click listener additional part ----------------------------

        recyclerAdapter.setOnItemClickListener(new RecyclerAdapter.ClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Toast.makeText(MainActivity.this, "on Item click"+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(int position, View v) {
                Toast.makeText(MainActivity.this, "Longclick"+position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}