A
A
Artem Mishurovsky2017-03-23 04:12:28
Java
Artem Mishurovsky, 2017-03-23 04:12:28

How to make transition from RecyclerView.Adapter to Fragment?

Please help people I have a problem. How to make transition from RecyclerView.Adapter to Fragment.
Here is the actual adapter code:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> { 

public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

CardView cv; 
TextView personName; 
TextView personAge; 
ImageView personPhoto; 


PersonViewHolder(View itemView) { 
super(itemView); 
cv = (CardView)itemView.findViewById(R.id.cv); 
personName = (TextView)itemView.findViewById(R.id.person_name); 
personAge = (TextView)itemView.findViewById(R.id.person_age); 
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo); 

itemView.setOnClickListener(this); 
} 

@Override 
public void onClick(View view) { 
switch (getAdapterPosition()){ 
case 0: 
} 
} 
} 

List<Person> persons; 

public RVAdapter(List<Person> persons){ 
this.persons = persons; 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
super.onAttachedToRecyclerView(recyclerView); 
} 

@Override 
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); 
PersonViewHolder personViewHolder = new PersonViewHolder(view); 
return personViewHolder; 
} 

@Override 
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) { 
personViewHolder.personName.setText(persons.get(i).name); 
personViewHolder.personAge.setText(persons.get(i).age); 
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId); 
} 

@Override 
public int getItemCount() { 
return persons.size(); 
} 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-03-23
@zagayevskiy

In the adapter, make the OnItemClickListener interface (*/pass payload data */), this is its own interface, not built-in. When the adapter is created, pass this interface to it and let the adapter pull the method. Outside, make the transition anywhere - including on the fragment.

A
Artem Mishurovsky, 2017-03-23
@Innerbloom

Like this:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        OnItemClickListener cvClickListener;

        public interface OnItemClickListener{
            void onItemClicked(int position);
        }


        PersonViewHolder(View itemView, OnItemClickListener clickListener) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);

            cvClickListener = clickListener;

            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            cvClickListener.onItemClicked(getAdapterPosition());
            А вот что сюда писать помогите ? Как реализовать переход на фрагмент re_fragment

            }
        }

    List<Person> persons;

    public RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder personViewHolder = new PersonViewHolder(view);
        return personViewHolder;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);


    }

    @Override
    public int getItemCount() {
        return persons.size();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question