Capture selected RecyclerView item to another screen
I have my search screen. When typing, it brings up the search result. The result is shown in a recyclerview. My Recyclerview was modeled with some textviews and an image. I wanted that when touching the Recyclerview item, that the textview text was passed to another fragment (screen) that I have. I can do even the call part of the screen. I just can't rescue the textview text on the other screen. I even instantiate the textviews from the other screen that this modeled the recycler.
My adapter class:
public class CursosAdapterImg extends RecyclerView.Adapter<CursosAdapterImg.CursosHolder> {
List<Curso>listaCursos;
private OnNoteListener mOnNoteListener;
public CursosAdapterImg(List<Curso> listaCursos, Context context,OnNoteListener onNoteListener) {
this.listaCursos = listaCursos;
this.mOnNoteListener = onNoteListener;
}
@NonNull
@Override
public CursosHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View vista = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_cursos_img, parent, false);
RecyclerView.LayoutParams layoutParams =
new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
vista.setLayoutParams(layoutParams);
return new CursosHolder(vista, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull CursosHolder holder, int position) {
// holder.txtCodigo.setText(listaCursos.get(position).getCodigo().toString());
holder.txtNome.setText(listaCursos.get(position).getNome().toString());
holder.txtProfessor.setText(listaCursos.get(position).getProfessor().toString());
holder.txtCategoria.setText(listaCursos.get(position).getCategoria().toString());
if(listaCursos.get(position).getImagem()!=null){
holder.idImagem.setImageBitmap(listaCursos.get(position).getImagem());
}else{
holder.idImagem.setImageResource(R.drawable.sem_foto);
}
}
@Override
public int getItemCount() {
return listaCursos.size();
}
public class CursosHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txtNome,txtCodigo,txtProfessor, txtCategoria;
ImageView idImagem;
OnNoteListener onNoteListener;
public CursosHolder(View itemView,OnNoteListener onNoteListener) {
super(itemView);
txtNome= (TextView) itemView.findViewById(R.id.nomeCurso);
//txtCodigo= (TextView) itemView.findViewById(R.id.txtCodigo);
txtProfessor= (TextView) itemView.findViewById(R.id.Professor);
txtCategoria= (TextView) itemView.findViewById(R.id.Categoria);
idImagem= itemView.findViewById(R.id.idImagem);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener{
void onNoteClick(int position);
}
}
Below my search screen returning a Recyclerview:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_consultar_lista_nome, container, false);
listaCursos=new ArrayList<>();
recyclerCursos= (RecyclerView) vista.findViewById(R.id.idRecyclerNome); // se der problema, mude aqui
recyclerCursos.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerCursos.setHasFixedSize(true);
txt_titulo = (TextView) vista.findViewById(R.id.titulo);
professor = (TextView) vista.findViewById(R.id.Professor);
campoNome = (EditText) vista.findViewById(R.id.campoNome);
botaoConsultar = (Button) vista.findViewById(R.id.btnConsultar);
request= Volley.newRequestQueue(getContext());
botaoConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
carregarWEBService();
}
});
return vista;
}
private void carregarWEBService() {
progresso = new ProgressDialog(getContext());
progresso.setMessage("Buscando...");
progresso.show();
String url = "http://192.168.0.5/webservices/consultarListaImagemUrlNome.php?nome="+ campoNome.getText().toString();
jsonObjectReq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
request.add(jsonObjectReq);
}
@Override
public void onErrorResponse(VolleyError error) {
progresso.hide();
Toast.makeText(getContext(), "Não foi possível listar os cursos " +error.toString() , Toast.LENGTH_SHORT).show();
Log.i("ERROR", error.toString());
}
@Override
public void onResponse(JSONObject response) {
progresso.hide();
Curso curso = null;
JSONArray json = response.optJSONArray("curso"); // nome da tabela curso
try {
for(int i=0; i<json.length();i++){
curso = new Curso();
JSONObject jsonObject = null;
jsonObject = json.getJSONObject(i);
curso.setNome(jsonObject.optString("nome"));
curso.setProfessor(jsonObject.optString("professor"));
curso.setCategoria(jsonObject.optString("categoria"));
curso.setDado(jsonObject.optString("imagem"));
listaCursos.add(curso);
}
progresso.hide();
CursosAdapterImg adapter = new CursosAdapterImg(listaCursos,getContext(),this);
recyclerCursos.setAdapter(adapter);
}catch (JSONException e){
e.printStackTrace();
progresso.hide();
Toast.makeText(getContext(), "Não foi possível listar os cursos " +response , Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNoteClick(int position) {
listaCursos.get(position);
//Falta passar os parâmetros
consultarCursoUrl fragmentConsulta = new consultarCursoUrl();
/* Estava testando nesse bloco passagem de parâmetros para outra fragment
Bundle arguments = new Bundle();
arguments.putString( "professor" , "Testando a passagem");
fragmentConsulta.setArguments(arguments); */
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main,fragmentConsulta).commit();
}
And this code below, is my screen that is called after touching the recycle item:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_consultar_curso_url, container, false);
campoCodigo = vista.findViewById(R.id.codigo);
campoNome = vista.findViewById(R.id.txt_nome);
campoCategoria = vista.findViewById(R.id.txt_categoria);
campoProfessor = vista.findViewById(R.id.txt_professor);
btnAtualizar = vista.findViewById(R.id.btnAtualizar);
btnDeletar = vista.findViewById(R.id.btnDeletar);
btnConsultar = vista.findViewById(R.id.btnConsultar);
imgFoto = vista.findViewById(R.id.imagemId);
/* Estava fazendo teste de parametros nesse bloco
Bundle arguments = getArguments();
String nomeProfessor = arguments.getString("professor");
campoNome.setText(nomeProfessor);
*/
request = Volley.newRequestQueue(getContext());
btnConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
carregarWEBService();
}
});
if(solicitarPermissoesVersoesSuperiores()){
imgFoto.setEnabled(true);
}else{
imgFoto.setEnabled(false);
}
imgFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarDialog();
}
});
btnAtualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarWEBServiceAtualizar();
}
});
btnDeletar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarWEBServiceDeletar();
}
});
return vista;
}
1 answers
Dealing with data contained in RecyclerView is a bit complex as it does not contain the onitemclicklistiner() method as in a ListView. For this, you will have to create an interface in the RecyclerView adapter to capture the position of the clicked item.
See my example below RecyclerView Adapter Code:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.exemplifique.apfarma.R;
import java.util.ArrayList;
import java.util.List;
public class Adapter_Construtor_Bula extends RecyclerView.Adapter<Adapter_Construtor_Bula.NewsViewHolder> implements Filterable {
private Context mContext;
private List<Constructor_Bula> mData ;
private List<Constructor_Bula> mDataFiltered ;
private ContactsAdapterListener listener;
/*É necessário criar uma interface para capturar o evento de click*/
public interface ContactsAdapterListener {
void onContactSelected(Constructor_Bula construtorBula);
}
Adapter_Construtor_Bula(Context mContext, List<Constructor_Bula> mData, ContactsAdapterListener listener) {
this.mContext = mContext;
this.mData = mData;
this.mDataFiltered = mData;
this.listener = listener;
}
@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View layout;
layout = LayoutInflater.from(mContext).inflate(R.layout.custon_recycleview,viewGroup,false);
return new NewsViewHolder(layout);
}
@Override
public void onBindViewHolder(@NonNull NewsViewHolder newsViewHolder, int position) {
newsViewHolder.container.setAnimation(AnimationUtils.loadAnimation(mContext,R.anim.fade_scale_animation));
newsViewHolder.txtTitle.setText(mDataFiltered.get(position).getTitle());
newsViewHolder.txtDescription.setText(mDataFiltered.get(position).getDescription());
}
@Override
public int getItemCount() {
return mDataFiltered.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String Key = constraint.toString();
if (Key.isEmpty()) {
mDataFiltered = mData ;
}
else {
List<Constructor_Bula> lstFiltered = new ArrayList<>();
for (Constructor_Bula row : mData) {
if (row.getTitle().toLowerCase().contains(Key.toLowerCase())){
lstFiltered.add(row);
}
}
mDataFiltered = lstFiltered;
}
FilterResults filterResults = new FilterResults();
filterResults.values= mDataFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mDataFiltered = (List<Constructor_Bula>) results.values;
notifyDataSetChanged();
}
};
}
class NewsViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle,txtDescription;
RelativeLayout container;
NewsViewHolder(@NonNull View itemView) {
super(itemView);
container = itemView.findViewById(R.id.container);
txtTitle = itemView.findViewById(R.id.txtTitleMed);
txtDescription = itemView.findViewById(R.id.txtDescription);
//Esta parte também é escencial.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onContactSelected(mDataFiltered.get(getAdapterPosition()));
}
});
}
}
}
Then I capture the click event at the position in the Activity, in my case in the fragment, as follows:
public class Fragment_Bula extends Fragment implements Adapter_Construtor_Bula.ContactsAdapterListener{
CollectionReference collectionReference;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bula, container, false);
/*
* Aqui eu pego os valores da minha coleção do firebase e adiciono na minha lista de nome ''list''.
*
* Não se apegue nesta parte pois é só um complemento para melhor entendimento.
*
*/
FirebaseFirestore db = FirebaseFirestore.getInstance();
List<Constructor_Bula> list = new ArrayList<>();
collectionReference = db.collection("Livro");
collectionReference.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
String nome = documentSnapshot.getString("Nome");
String formula = documentSnapshot.getString("Principio ativo");
String tipo = documentSnapshot.getString("Tipo");
for (int i = 0 ; i < 10 ; i++){
//Adicionando valores a lista.
list.add(new Constructor_Bula(nome,formula,tipo));
}
}
}
});
// O que você precisa entender é daqui para baixo.
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
Adapter_Construtor_Bula adapter = new Adapter_Construtor_Bula(getContext(),list,Fragment_Bula.this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
/*
* Usei getContext() e "view.findViewById(...)" pois estou em um fragmento mas se você tiver em uma acitivity pode * fazer assim:
*
* RecyclerView recyclerView = findViewById(R.id.recyclerView);
* Adapter_Construtor_Bula adapter = new Adapter_Construtor_Bula(this,list,Fragment_Bula.this);
* recyclerView.setLayoutManager(new LinearLayoutManager(this);
*/
return view;
}
//Método de captura dos valores da posição do recyclerView semelhante ao onItemClikLister() do ListView;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onContactSelected(Constructor_Bula construtorBula) {
// Para capturar o valor da posição eu faço daseguinte maneira
String nome = construtorBula.getTitle();
/* o .getTitle() é o valor contido no Getter do meu construtor contido na
* minha classe Construtor_Bula da variável ''private String title''
* no qual eu uso para configurar meu RecyclerView
*/
//Por fim, basta usar o valor contido na String nome da forma que quiser.
}
}