如何从适配器Android中调用片段中的方法



我需要帮助,所以我有一个片段,它有一个RecycleView,在RecycleView里面有一个按钮。

点击后的按钮必须打开已经在基本片段中声明的对话框,所以我只调用openDialog(DIALOG_CHECK);

现在,我如何在我的适配器上调用该对话框?我已经在片段中创建了一个方法,并从适配器中调用它,并生成一个错误"Java lang null pointer"

这是我的代码:

DeliveryFragment delivFrag = new DeliveryFragment();
holder.editButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         delivFrag.doEdit();
     }
});

并且在片段中

public void doEdit(){
   openDialog(DIALOG_EDIT_ITEM);
}

更新适配器构造函数以接受Fragment作为参数。

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);

适配器类别:

public CustomAdapter(Context context, int id, HomeFragment fragment) {
    this.fragment = fragment;
}

呼叫适配器:

((FragmentName)fragment).methodName();

这只是一个简单的例子,可以更好地理解。使用接口。

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {    
  private static OnItemClickListener mOnItemClickLister;
  public interface OnItemClickListener {
    void onItemClicked(View view, int pos);
  }
  public void setOnItemClickListener(OnItemClickListener listener) {    
    mOnItemClickLister = listener;
  }
  public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {    
    Button mBtnTest;
    Context mContext;
    //We also create a constructor that accepts the entire item row
    //and does the view lookups to find each subview.
    public ViewHolder(Context context, View itemView) {    
      //Stores the itemView in a public final member variable that can be used
      //to access the context from any ViewHolder Instance
      super(itemView);    
      mContext = context;
      mBtnTest = (Button) itemView.findViewById(R.id.message_button);
      itemView.setOnClickListener(this);
    }
    @Override public void onClick(View v) {
      int position = v.getLayoutDirection();
      mOnItemClickLister.onItemClicked(v, position);
    }
  }
}   

碎片部分

class FragmentTest extends Fragment implements OnItemClickListener {    
  TestAdapter adapter = new TestAdapter(); //you can initialize according to  your logic
  //set the fragment as a listener to adapter
  this.adapter.setOnItemClickListener(onItemClickListener);
  public void onItemClicked(View view, int pos) {
    //do whatever you want here...
  }
}

创建一个带有接口的回调

它是软件工程中最常用的模式之一。

A->B

A和B表示实体,并且->表示的方向通信

在我们的例子中,它可以表示为

适配器->碎片

为了在实体之间进行沟通,我们需要某种桥梁。在这些模式中,接口充当桥梁。

步骤1:创建一个接口

interface FragmentCallback{
    public void doSomething(){
    }
}

步骤2:让Fragment实现接口并覆盖方法

class YourFragment implements FragmentCallback{
       // Your fragment code
     @Override
     public void doSomething(){
        Log.d(TAG, "Clicked")
     }
}

步骤3:在适配器中启动回调

class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
      public FragmentCallback callback;
      public MyAdapter(FragmentCallback callback){
            thid.call = callback;     //<--This is how you initialise it     
      }
      callback.doSomething(). //<-- This is how you call callback method
}

##如何在Fragment中创建Adapter实例?

MyAdapter adapter = new MyAdapter(this)

示例代码:你可以在这里找到一个示例代码
免责声明:示例代码基于上述思想,但不太容易理解。

您必须在Adapter类中编写一个接口,并在调用适配器的片段中实现该功能。

这是一个示例应用程序,用于回收者查看项目点击操作。检查一次,它可能对你有用。https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0

您可以将Fragment实例发送到Adapter的构造函数中,然后您可以使用此实例来调用该片段中的方法。

public MyCartRecycleAdapter(Context context, List<CartData> list, MyFragmentNew myFragmentNew) {
    this.list = list;
    this.mContext = (Activity) context;
    this.myFragmentNew = myFragmentNew;
}

然后

myFragmentNew.MethodName();

最新更新