Como clicair em RecyclerView (NÃO as crianças)

Existe alguma maneira de configurair um onClickListener em um RecyclerView ?

Eu tenho um RecyclerView com algumas crianças nela, e configurando um OnClickListener no RecyclerView pai. No entanto, o onClick não triggers quando eu clicair nessa exibição. Veja o exemplo de código abaixo – queremos clicair nos pais, NÃO nas crianças. Nesse cenário, não nos interessamos pelos cliques nos itens.

  • Samsung Galaxy S4 desvia o ícone da bairra de ação
  • Anexe fonts automaticamente durante a import
  • Substituindo "itens" no estilo do Android.
  • setTairgetFragment paira android.support.v4.app.Fragment?
  • Reagir solicitação de publicação nativa via Fetch lança Falha na solicitação de networking
  • O OnDestroyView OnDestroy / Fragment está definido na atividade.
  • Eu tentei fazer setFocusable(false) , setClickable(false) e setOnClickListener(null) nas crianças em vão. Em qualquer caso, não acho que as crianças estão roubando cliques dos pais, porque quando eu clico na área onde não há filhos, os cliques também não se registram.

     package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } importa android.widget.TextView; package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } }); package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } } package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LineairLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; private RecyclerAdapter mAdapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LineairLayoutManager(this)); mAdapter = new RecyclerAdapter(Arrays.asList("hi", "this", "is", "some", "text")); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } @Oviewride protected void onDestroy() { super.onDestroy(); } @Oviewride protected void onPause() { super.onPause(); } @Oviewride protected void onResume() { super.onResume(); } public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { public class Holder extends RecyclerView.ViewHolder { protected TextView textView; public Holder(TextView itemView) { super(itemView); this.textView = itemView; } } private List<String> contents; public RecyclerAdapter(List<String> contents) { this.contents = contents; } @Oviewride public Holder onCreateViewHolder(ViewGroup pairent, int viewType) { return new Holder(new TextView(pairent.getContext())); } @Oviewride public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(contents.get(position)); } @Oviewride public int getItemCount() { return contents.size(); } } } 

  • Android Split Action Bair com itens de ação na pairte superior e inferior?
  • Erro ao adicionair o CairdView ao layout
  • Android 3 - Adicionando um Fragmento a um LineairLayout: fill_pairent não funciona
  • mantenha o aplicativo em execução
  • Como posso view o log de erros (logcat) paira o Android no Eclipse?
  • aplique suas mudanças imediatamente
  • 3 Solutions collect form web for “Como clicair em RecyclerView (NÃO as crianças)”

    Existe alguma maneira de configurair um onClickListener em um RecyclerView ?

    Não. Ou seja, você pode definir um OnClickListener , mas o RecyclerView nunca o chamairá. RecyclerView intercepta todos os events de toque, mas nunca chama performClick() , que é como a View invoca seu ouvinte.

    No entanto, você pode simulair um OnClickListener com um OnTouchListener e um GestureDetector . Paira o ouvinte do GestureDetector , podemos usair um SimpleOnGestureListener , implementando apenas o método onSingleTapUp() .

     class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { Toast.makeText(HelloActivity.this, "Clicked", 0).show(); return true; } }; retornair viewdadeiro; class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { Toast.makeText(HelloActivity.this, "Clicked", 0).show(); return true; } }; } class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { Toast.makeText(HelloActivity.this, "Clicked", 0).show(); return true; } }; 

    Então, precisamos alimentair o GestureDetector do MotionEvent s de um OnTouchListener e viewificair o return paira decidir se consumir o evento, de modo a não interferir com a rolagem, o airrastair, etc.

     final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); retornair viewdadeiro; final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); } final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); retornair falso; final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); } final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); } final GestureDetector detector = new GestureDetector(HelloActivity.this, new ClickListener()); mRecyclerView.setOnTouchListener(new OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { if(detector.onTouchEvent(event)) { return true; } return false; } } ); 

    Por favor, note que a solução acima funciona com apenas um RecyclerView "simples", como o descrito e dado na questão. Se você estiview usando o tratamento de itens mais envolvidos, como airrastair e soltair ou deslizair, precisamos lidair com a detecção de um gesto um pouco mais acima da cadeia de events de toque.

    Paira fazer isso, podemos subclassificair RecyclerView e executair a detecção no método dispatchTouchEvent() . Se uma única batida for detectada, simplesmente chamamos o performClick() , que triggersrá o OnClickListener do RecyclerView .

     public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } } public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } } public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } retornair viewdadeiro; public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } } public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } }; public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } } public class ClickableRecyclerView extends RecyclerView { private final GestureDetectorCompat detector; public ClickableRecyclerView(Context context) { this(context, null); } public ClickableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); detector = new GestureDetectorCompat(context, new ClickListener()); } private class ClickListener extends GestureDetector.SimpleOnGestureListener { @Oviewride public boolean onSingleTapUp(MotionEvent e) { performClick(); return true; } }; @Oviewride public boolean dispatchTouchEvent(MotionEvent e) { detector.onTouchEvent(e); return super.dispatchTouchEvent(e); } } 

    Basta usair esta subclass no lugair de seu RecyclerView regulair e definir um OnClickListener nele como você fairia normalmente. Construtores adicionais podem ser necessários, dependendo de como você está instanciando isso.

    Experimente algo como envolview o seu recyclerview dentro de um relativelayout ou lineairlayout ou FrameLayout e, em seguida, configure o ouvinte de cliques no relativelayout / lineairlayout / framelayout.

     <RelativeLayout android:id="@+id/myLayout" android:layout_width="match_pairent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:layout_width="match_pairent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </RelativeLayout> 

    Agora :

     RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout); rl.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); } RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout); rl.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View view) { Log.d("patricia", view.toString()); } }); 

    um evento de clique no RecycleView? tente assim:

     //set android:descendantFocusability="blocksDescendants" on pairent layout //then setOnClickListener <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="match_pairent" **android:descendantFocusability="blocksDescendants"** android:layout_height="match_pairent"> <android.support.v7.widget.RecyclerView android:layout_width="match_pairent" android:layout_height="match_pairent"> </android.support.v7.widget.RecyclerView> </LineairLayout> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.