recycleview onLongclick会自动调用onClick

时间:2016-06-20 07:50:27

标签: android android-recyclerview onitemclicklistener

我正在处理RecyclerView onItemClickonItemLongClick,两者都运作良好,但问题是onItemLongClick也调用了onItemClick

这是我的适配器代码:

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

}

我用它来调用它:

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 + "");

        }
    });

我也尝试过,但同样的问题是: Little robots's onitemclick code

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:5)

返回true将告诉系统我已经处理了你不必担心的事件,否则Onclick将会解雇。

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