Janco Boscan
Janco Boscan

Reputation: 71

Open a new fragment and pass data when click on my Recycler CardView Grid?

Hello I need help to open a new fragment and pass data when clicked on my Recycler CardView Grid.

Android Grid Image

I want to click on for example the champion Aatrox (first grid) and open a new fragment with Aatrox Informatión. the same with the others champions of League of Legends.

I know that is inside of onClick function but I dont know how to do it.

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setClickable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){

            }
        });

Here is my full ChampAdapter.java

public class ChampAdapter extends RecyclerView.Adapter<ChampAdapter.ViewHolder> {

    public List<ChampionItemModel> champItem;

    public ChampAdapter(List<ChampionItemModel> champItem){this.champItem = champItem;}

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView champName;
        TextView roleChamp;
        ImageView champImg;

        public ViewHolder(View itemView) {
            super(itemView);
            this.champName = (TextView)itemView.findViewById(R.id.champ_name);
            this.roleChamp = (TextView)itemView.findViewById(R.id.champ_role);
            this.champImg = (ImageView)itemView.findViewById(R.id.champ_image);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_champs,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setClickable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){

            }
        });

        holder.champName.setText(champItem.get(position).champName);
        holder.roleChamp.setText(champItem.get(position).roleChamp);
        holder.champImg.setImageResource(champItem.get(position).champImg);
    }

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

}

Upvotes: 0

Views: 1925

Answers (2)

suku
suku

Reputation: 10937

Below is a general method on how to communicate between fragments, so it should be applicable to your issue also.

Place the recyclerView inside a fragment.

I am assuming you are able to get the position of the Adapter. Put recyclerview in a fragment call RV and it's response is to be seen in fragment say RVvalues. You use an interface called PassRVValues

Code in RV fragment:

public class RV extends Fragment{
   int RVposition;
   PassRVValues passRVValues;
//
 your code for recyclerview and other things
//
 RVposition = position_value_obtained;
 passRVValues.sendToOtherFragment(RVposition);

Here's the code for the interface. Make a new java class having just the interface.

public interface PassRVValues{
   sendToOtherFragment(int value_from_RVFragment);
}

Code in the activity

   public class MainActivity implements PassRVValues{
   //
     some code
   //
   @Override
   public void sendToOtherFragment(int use_value_from_RVFragment){
   //
    Do something with data if you want
   //
   RVvalues Frag = (RVValues)getFragmentManager().findFragmentById(R.id.rvvalues);
   Frag.ShowDataBasedOnPositionInRV(something_based_from_any_process_in_sendToOtherFragment_Method);
   }

Now for the code in the RVValues fragment

public class RVValues extends Fragment{
    //again any codes//
    public void ShowDataBasedOnPositionInRV(int data_based_on_RV_position){
   //do something//
}

This is the way to implement inter-fragment communication in the simplest manner. Hope this helps! Cheers!

Upvotes: 0

marktani
marktani

Reputation: 7808

First you should embed the RecyclerView inside a fragment, like you normally would, let's call it ChampionOverviewFragment.

Now you should have a SingleChampionFragment with a static newInstance method that accepts as parameters everything that you need to build the champion information (for example a String with the id of your champ). We want to open this fragment when we click on one of the cards in your cardview.

Your activity now only has one HostFragment that you fill with the ChampionOverviewFragment in its onCreate method. See my answer on how to create nested fragments.

Your onClick method can now look like this:

@Override
public void onClick(View v){
    ((MainActivity) holder.itemView.getContext()).openChampionFragment(holder.getChampionId);
}

Of course, then your MainActivity has to include the following method:

public void openChampionFragment(String id)
    this.hostFragment.replaceFragment(SingleChampionFragment.newInstance(id));
}

If you also need backstack navigation, refer to the tutorial I linked in the other answer.

Upvotes: 1

Related Questions