将数据从 RecyclerView 传递到 Fragment?

Passing data from RecyclerView to Fragment?

我目前正在创建一个应用程序,它包含一个片段、一个用于 RecyclerView 的适配器和一个通过片段访问的 RecyclerView。我需要将数据从 RecyclerView 传递回片段,但是我无法这样做,因为未通过 Intent 识别片段。当用户从 RecyclerView 选择一个项目时,这个项目应该被传递到片段。我有使用 onBackPressed() 的 RecyclerView,以便导航回工作正常的片段,但是似乎没有数据通过。请在下面查看我目前拥有的内容:

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

你可以使用接口,这里是如何做的

创建处理点击的界面:-

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

不要在您的片段中扩展它或 activity:-

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

}

在回收器视图中,将此 activity 传递给回收器视图构造函数,在您的自定义适配器中,您可以将此 activity 分配给侦听器接口,并从覆盖的 onClick() 调用 onCustomItemSelected 方法,这将是在您之前的 activity 或片段中。

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


       



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

这会将数据传递给 fragment/activity

中的 onCustomItemSelected() 方法

这是给你的片段

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);
   }

}

这是你的适配器

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);
    }
});

我强烈建议迁移到 kotlin 并使用高阶函数,它非常简单。