0

In my app I have a Fragment that implements a FirebaseRecyclerAdapter to list Firebase objects.

I would need to implement an onClick method to launch another fragment with detail info from the selected object.

I have searched on this question, but I haven´t found any answer that could be applied to my scenario.

This is the fragment code:

public class EnclaveFragment extends Fragment {

    private String enclave;
    private TextView mText;

    private RecyclerView mListaEnclaves;
    private DatabaseReference mDatabase;

    public void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Enclave, EnclavesViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Enclave, EnclavesViewHolder>  (
                Enclave.class,
                R.layout.enclave_row,
                EnclavesViewHolder.class,
                mDatabase.orderByChild("Comunidad_enclave").equalTo(enclave)) {


            protected void populateViewHolder(EnclavesViewHolder viewHolder, Enclave model, int position) {
                viewHolder.setTitulo(model.getNombre_enclave());
                viewHolder.setDescripcion(model.getDescripcion_enclave());
                viewHolder.setImage(getActivity().getApplicationContext()  , model.getImagen_Enclave());
            }
        };

        mListaEnclaves.setAdapter(firebaseRecyclerAdapter);
    }


    public static class EnclavesViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public EnclavesViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setTitulo(String titulo){
            TextView titulo_enclave = (TextView) mView.findViewById(R.id.titulo_enclave);
            titulo_enclave.setText(titulo);
        }

        public void setDescripcion(String descripcion){
            TextView descripcion_enclave = (TextView) mView.findViewById(R.id.descripcion_enclave);
            descripcion_enclave.setText(descripcion);
        }

        public void setImage(Context ctx, String imagen){
            ImageView imagen_enclave = (ImageView) mView.findViewById(R.id.imagen_enclave);
            Picasso.with(ctx).load(imagen).into(imagen_enclave);
        }
    }

    public EnclaveFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            enclave = bundle.getString("KEY_DETAIL", "Madrid");
            Log.d("Enclave recibido",enclave);
        }

        View rootView = inflater.inflate(R.layout.enclave_connect, container, false);

        mListaEnclaves = (RecyclerView) rootView.findViewById(R.id.lista_enclaves);
        mListaEnclaves.setHasFixedSize(true);
        mListaEnclaves.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Enclaves");
        mText = (TextView) rootView.findViewById(R.id.ccaatxt);
        mText.setText(enclave);

        return rootView;
    }

}
iRuth
  • 2,687
  • 3
  • 25
  • 32
  • Please show what have you tried so far – Oleg Bogdanov Dec 31 '16 at 03:53
  • Possible duplicate of [RecyclerView onClick](http://stackoverflow.com/questions/24471109/recyclerview-onclick) – Oleg Bogdanov Dec 31 '16 at 04:04
  • @OlegBogdanov, I don´t see why do you find my question as duplicate. You are not helping me. – NewAtFirebase Dec 31 '16 at 04:16
  • many people have already explained how to implement onClick within recycler's view adapter, how is your case unique here? – Oleg Bogdanov Dec 31 '16 at 04:17
  • @OlegBogdanov, I am not an expert, I am learning, and the question you have considered is not about implementing onClick inside a fragment activity. May be you can help me further. – NewAtFirebase Dec 31 '16 at 04:20
  • maybe you can explain exactly what you need and what you tried? – Oleg Bogdanov Dec 31 '16 at 04:20
  • @OlegBogdanov, I need to click on one of the rows and open another fragment that should show more information about the selected object. I have tried including: mView.setOnClickListener(); inside public EnclavesViewHolder(View itemView) {--} – NewAtFirebase Dec 31 '16 at 04:25
  • so which part is not clear to you? onClicks are answered on SO, how to run fragments are answered million times on SO, what is missing? – Oleg Bogdanov Dec 31 '16 at 04:26
  • @OlegBogdanov, happy new year!, Hope next year you may be a better SO member and help other SO members, I need help to solve my issue, if you don´t want to help me, don´t waste your time and my time. Thank you. – NewAtFirebase Dec 31 '16 at 04:33
  • Likewise, hope next year you would appreciate reading the SO rules – Oleg Bogdanov Dec 31 '16 at 04:34

1 Answers1

0

Register an onClick listener for the individual view(s) that, when clicked, should take some action, as described in this question. The RecyclerView and its adapter have nothing to do with handling any events on any of its child item views.

Community
  • 1
  • 1
Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302