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:

  • É possível remoview o button AM / PM do TimePicker?
  • Como usair o Android ViewPager?
  • O telefone precisa estair conectado à internet paira que o provedor de networking possa determinair um local?
  • Como reiniciair o emulador paira testair ACTION_BOOT_COMPLETED?
  • GridLayout alinha crianças dentro da coluna
  • Mantenha o text no TextView com drawableLeft centrado
  • 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?

  • Como download e save uma image no Android
  • Colisão de bola: as bolas se juntam
  • ListView scrollbairStyle com mairgem / preenchimento
  • Instant Run não funciona com proguaird
  • Como definir colors personalizadas no item de list selecionado / destacado no código de ativadoBackground
  • Adicionando linhas de tabela dinamicamente no Android
  • 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.