Smit.Satodia Smit.Satodia - 5 months ago 32
Android Question

recycleview onLongclick automatically calls onClick

I am working on

RecyclerView
onItemClick
and
onItemLongClick
, both work good but the problem is that
onItemLongClick
also calls
onItemClick
.

Here is my adaptor code:

package Adaptor;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wings.example.recycleview.MainActivity;
import com.wings.example.recycleview.R;

import java.util.ArrayList;

public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> {


Context context;
private ArrayList<String> arrayList;
private static onClickListner onclicklistner;

public SampleAdapter(Context context) {
this.context = context;
arrayList = MainActivity.arrayList;
}

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

TextView txt_pos;
SampleAdapter sampleAdapter;

public ViewHolder(View itemView, SampleAdapter sampleAdapter) {
super(itemView);

itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);

txt_pos = (TextView) itemView.findViewById(R.id.txt_pos);
this.sampleAdapter = sampleAdapter;

itemView.setOnClickListener(this);
}


@Override
public void onClick(View v) {
onclicklistner.onItemClick(getAdapterPosition(), v);
}

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

public void setOnItemClickListener(onClickListner onclicklistner) {
SampleAdapter.onclicklistner = onclicklistner;
}

public interface onClickListner {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}



@Override
public int getItemCount() {

return arrayList.size();

}

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.txt_pos.setText(arrayList.get(position));
}

@SuppressLint("InflateParams")
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_recycler_row_sample, viewGroup, false);
return new ViewHolder(view, this);
}

}


And I called it using:

sampleAdapter.setOnItemClickListener(new SampleAdapter.onClickListner() {
@Override
public void onItemClick(int position, View v) {
Log.e(TAG+"ON ITEM CLICK", position + "");

}

@Override
public void onItemLongClick(int position, View v) {
Log.e(TAG + "ON ITEM LONG CLICK", position + "");

}
});


And I've also tried but same problem is there:
Little robots's onitemclick code

Any help would be highly appreciated.

Answer

Returning true will tell the system i have handle the event you dont have to worry chill yourself otherwise Onclick will fire.

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