A
A
Andrey2016-01-08 21:29:20
Android
Andrey, 2016-01-08 21:29:20

CardView & RecyclerView & Fragment, how to implement a fragment in an adapter?

Hello source data.
The standard menu DrawerLayout & NavigationView, fragments are attached to the menu items, which are displayed in the FrameLayout.
The fragment displayed in the FrameLayout consists of a CardView in conjunction with a RecyclerView. The connection between them is carried out by an adapter that inherits from RecyclerView.Adapter with an indication of the class that will store links to the widgets of the list item, i.e. class that implements ViewHolder. There is also a listener that is responsible for handling the click on the CardView.
How can this listener implement a replacement in FrameLayout, a fragment with a CardView for another fragment? To change the CardView to the fragment in the FrameLayout that is clicked.

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

    public static class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CardView cv;
        TextView cvTitle;
        TextView cvContent;
        ImageView cvPhoto;

        public ContentViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            cvTitle = (TextView) itemView.findViewById(R.id.text_title);
            cvContent = (TextView) itemView.findViewById(R.id.text_content);
            cvPhoto = (ImageView) itemView.findViewById(R.id.text_photo);

            itemView.setOnClickListener(this);
        }

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

                    break;
                case 1:

                    break;
                case 2:

                    break;
            }
            Log.i("RecyclerView", "Вы щёлкнули на позиции " + getAdapterPosition());
        }
    }

    List<CardContent> mCardContents;
   
    public RVAdapter(List<CardContent> mCardContents) {
        this.mCardContents = mCardContents;
    }
   
    @Override
    public int getItemCount() {
        return mCardContents.size();
    }

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        ContentViewHolder pvh = new ContentViewHolder(v);

        return pvh;
    }
    @Override
    public void onBindViewHolder(ContentViewHolder contentViewHolder, int i) {
        contentViewHolder.cvTitle.setText(mCardContents.get(i).title);
        contentViewHolder.cvContent.setText(mCardContents.get(i).content);
        contentViewHolder.cvPhoto.setImageResource(mCardContents.get(i).photoId);
        //Log.i("RecyclerView", "Позиция " + i);

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Lebedev, 2016-01-09
@kamiLLxiii

Slightly confusingly written, but sort of understood. Do you need to replace the current fragment by clicking on an element of the list?
Then you should create another interface and use composition. Transfer the implementation of this interface to the RVAdapter and ContentViewHolder constructors, and create it itself at the level where fragments should be managed. For example, like this:

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

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

    public static class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        CardView cv;
        TextView cvTitle;
        TextView cvContent;
        ImageView cvPhoto;
        OnItemClickListener cvClickListener;

        public ContentViewHolder(View itemView, OnItemClickListener clickListener) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            cvTitle = (TextView) itemView.findViewById(R.id.text_title);
            cvContent = (TextView) itemView.findViewById(R.id.text_content);
            cvPhoto = (ImageView) itemView.findViewById(R.id.text_photo);
            cvClickListener = clickListener;

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            cvClickListener.onItemClicked(getAdapterPosition());
            Log.i("RecyclerView", "Вы щёлкнули на позиции " + getAdapterPosition());
        }
    }

    List<CardContent> mCardContents;
   
    public RVAdapter(List<CardContent> mCardContents, OnItemClickListener itemClickListener) {
        this.mCardContents = mCardContents;
        this.mItemClickListener = itemClickListener;
    }
   
    @Override
    public int getItemCount() {
        return mCardContents.size();
    }

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        ContentViewHolder pvh = new ContentViewHolder(v, mItemClickListener);

        return pvh;
    }
    @Override
    public void onBindViewHolder(ContentViewHolder contentViewHolder, int i) {
        contentViewHolder.cvTitle.setText(mCardContents.get(i).title);
        contentViewHolder.cvContent.setText(mCardContents.get(i).content);
        contentViewHolder.cvPhoto.setImageResource(mCardContents.get(i).photoId);
        //Log.i("RecyclerView", "Позиция " + i);

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question