简体   繁体   中英

Passing data from RecyclerView to Fragment?

I am currently creating an application which consists of a Fragment, an Adapter for a RecyclerView and a RecyclerView which is accessed through the fragment. I need data to be passed from the RecyclerView back to the fragment however am unable to do so as the Fragment isn't identified through the Intent. When the user selects an item from the RecyclerView this item should be then passed through to the fragment. I have the RecyclerView using onBackPressed() in order to navigate back to the fragment which works fine, however no data seems to pass. Please see below what I currently have:

CustomAdapter.java

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener{
          public void onItemClick(View view, int position);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
         holder.text1.setText(String.valueOf(text1.get(position)));
         holder.text2.setText(String.valueOf(text2.get(position)));
         holder.text3.setText(String.valueOf(text3.get(position)));

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mOnItemClickListener.onItemClick(v, holder.getAdapterPosition());
        }
    });
}

InputToFragment.java

        @Override
        public void onItemClick(View view, int position) {
            // Need to pass data through to Fragment, however unable to do so as it's not identified
            // within the Intent
            onBackPressed();
        }
    });

Fragment.java

       // Nothing related to Passing data within the Fragment

Its for your fragment

YourFragment : Fragment() {
    OnItemClickListener listener = new OnItemClickListener(){
        @Override
         public void onItemClick(View view, int position){
             //this called if item at adapter clicked
         }
    }

  ....
   //whatever lifecycle you choose for initialize adapter
   onCreateView(){
       adapter = new YourAdapter(listener);
   }

   public interface OnItemClickListener{
          public void onItemClick(View view, int position);
   }

}

and this for your adapter

private OnItemClickListener listener; 

YourConstructorAdapter(OnItemClickListener listener){
   this.listener = listener;
}

 @Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
     holder.text1.setText(String.valueOf(text1.get(position)));
     holder.text2.setText(String.valueOf(text2.get(position)));
     holder.text3.setText(String.valueOf(text3.get(position)));

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // this code calling method from fragment
        listener.onItemClick(v, position);
    }
});

I'm highly recommend migrate to kotlin and use higher-order function, its very simple.

You can use interface, here is how to do it

create interface to handle click:-

public interface OnCustomItemSelectListener {
    void onCustomItemSelected(String data);
}

not extend it in your fragment or activity:-

public class MyActivity implements OnCustomItemSelectListener {
    @Override
    public void onCustomItemSelected(String data) {
        /*store it if neede */
        /* additional code */
    }

}

In recycler view pass this activity to recycler view constructor and inside your custom adapter you can assign this activity to listener interface and call the onCustomItemSelected method from overridden onClick(), which will be in your previous activity or fragment.

OnCustomItemSelectListener itemSelectListener;
public CustomAdapter(Activity listenerAct, .....){
  this.itemSelectListener = listenerAct;
}


       



@Override
 public void onClick(View view)  
 {  
    itemSelectListener.onCustomItemSelected(data);  
 }  

this will pass data to onCustomItemSelected() method in your fragment/activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM