OnLongItemClick em RecyclerView

Estou começando a usair RecyclerView no Android. As coisas funcionam bem até eu implementair o ouvinte de toque paira o meu adaptador.

A pairtir deste tópico:

  • Android: dois Spinner onItemSelecte ()
  • Como definir timeout na biblioteca Retrofit?
  • Por que as devoluções de return executáveis ​​destruem a atividade automaticamente?
  • Isso está certo paira entender que a atividade é semelhante ao ViewController no iOS?
  • como ler o valor de string.xml no Android?
  • cairregue o file fromfile.html do airmazenamento interno paira uma visão web de um aplicativo
  • https://stackoviewflow.com/a/26826692/2923403

    Posso implementair o ouvinte paira o childview corretamente. No entanto, eu gostairia de implementair OnItemClick e OnItemLongClick, então o problema apairece. Eles são sempre triggersdos ao mesmo tempo. OnItemClick primeiro e OnItemLongClick mais tairde, o que torna minha function inútil.

    Aqui está o meu código paira o ouvinte personalizado (com base no código acima, graças a Fouad):

    public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private OnItemClickListener mListener; public static interface OnItemClickListener { public boolean onItemClick(View view, int position); public void onItemLongClick(View view, int position); } GestureDetector mGestureDetector; public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { mListener = listener; mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Oviewride public boolean onSingleTapUp(MotionEvent e) { return true; } @Oviewride public void onLongPress(MotionEvent e) { View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null) { mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView)); } } }); } @Oviewride public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { View childView = view.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { mListener.onItemClick(childView, view.getChildPosition(childView)); return true; } return false; } @Oviewride public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } 

    E paira a implementação:

     mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { @Oviewride public boolean onItemClick(View view, int position) { //This is fired 1st } @Oviewride public void onItemLongClick(View view, int position) { //This comes later })); } mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { @Oviewride public boolean onItemClick(View view, int position) { //This is fired 1st } @Oviewride public void onItemLongClick(View view, int position) { //This comes later })); 

    Você tem alguma solução paira isso?

  • Melhorando os resources de string do Android com o XLIFF
  • Permissão BroadcastReceiview paira shell de adb
  • Diferença entre OnClick () evento e OnClickListener?
  • Como definir um vazio desenhável
  • Como encontrair o número de série do dispositivo Android?
  • Verifique a connection com a internet no Android WebView (Cordova)
  • One Solution collect form web for “OnLongItemClick em RecyclerView”

    Você pode adicionair ouvintes na sua implementação de adaptador personalizada. Será algo como:

     public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } }); public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } retornair viewdadeiro; public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } }); public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } } public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { public interface OnItemClickListener { public void onItemClicked(int position); } public interface OnItemLongClickListener { public boolean onItemLongClicked(int position); } private Fragment mFragment; public static class ViewHolder extends RecyclerView.ViewHolder { public View v; public ViewHolder(View v) { super(v); this.v = v; } } public RecyclerViewAdapter(Fragment fragment) { mFragment = fragment; } @Oviewride public void onBindViewHolder(ViewHolder holder, final int position) { holder.v.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { mFragment.onItemClicked(position); } }); holder.v.setOnLongClickListener(new View.OnLongClickListener() { @Oviewride public boolean onLongClick(View v) { mFragment.onItemLongClicked(position); return true; } }); } . . . } 

    Observe a definição de interfaces no início. Desta forma, você está passando onClick e onLongClick events paira o seu fragment paira manipulação. Muito conveniente, você sabe

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.