简体   繁体   English

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

[英]How to call method in fragment from adapter Android

I need help so I have a fragment which has a RecycleView and inside the RecycleView there is a button.我需要帮助,所以我有一个片段,它有一个RecycleView ,在RecycleView里面有一个按钮。

The button after click must open the dialog which already declared in base fragment so I only call like openDialog(DIALOG_CHECK);单击后的按钮必须打开已经在基本片段中声明的对话框,所以我只调用openDialog(DIALOG_CHECK);

Now how can I call that dialog on my adapter I already make a method in fragment and call it from the adapter and make an error "Java lang null pointer"现在如何在我的适配器上调用该对话框我已经在片段中创建了一个方法并从适配器调用它并生成错误“Java lang null pointer”

This is my code :这是我的代码:

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

And in fragment并在片段中

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

Update your adapter constructor to accept the Fragment as a parameter.更新您的适配器构造函数以接受 Fragment 作为参数。

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

Adapter Class:适配器类:

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

Call in Adapter :调用适配器:

((FragmentName)fragment).methodName();

Just a simple example for better understanding.只是一个简单的例子,以便更好地理解。 Use an interface.使用接口。

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

Fragment Part片段部分

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...
  }
}

Create a callback with an interface使用接口创建回调

It is one of the most used patterns in Software Engineering.它是软件工程中最常用的模式之一。

A -> B A -> B

A and B represent the entities and -> represents the direction of communication A和B代表实体,->代表通信方向

In our case, it can be represented as在我们的例子中,它可以表示为

Adapter -> Fragment适配器 -> 片段

To communicate between the entities we need some kind of bridge.为了在实体之间进行通信,我们需要某种桥梁。 In these patterns, an interface serves as a bridge.在这些模式中,接口充当桥梁。

Step 1: Create an interface第一步:创建接口

interface FragmentCallback{

    public void doSomething(){

    }

}

Step 2: Make your Fragment implements the interface and override the method第二步:让你的 Fragment 实现接口并覆盖方法

class YourFragment implements FragmentCallback{

       // Your fragment code

     @Override
     public void doSomething(){
        Log.d(TAG, "Clicked")
     }

}

Step 3: Initiate the Callback in Adapter第三步:在Adapter中发起回调

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

}

##How to create an instance of Adapter in Fragment? ##如何在Fragment中创建Adapter的实例?

MyAdapter adapter = new MyAdapter(this)

Sample code: You can find a sample code here .示例代码:您可以在此处找到示例代码。
Disclaimer: The sample code is based on the above idea but it is not very easy to understand.免责声明:示例代码基于上述思想,但不是很容易理解。

You have to write an interface in your Adapter class and implement that functionality in your fragment from where your calling your adapter.您必须在 Adapter 类中编写一个接口,并在调用适配器的片段中实现该功能。

Here is sample app for recycler view with item click action.这是带有项目单击操作的回收站视图示例应用程序。 Check once it may useful to you.检查一次它可能对您有用。 https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0 https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0

You can send Fragment Instance in to constructor of your Adapter and then you can use this instance to call the method in that fragment.您可以将 Fragment Instance 发送到您的 Adapter 的构造函数中,然后您可以使用此实例来调用该片段中的方法。

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

and then然后

myFragmentNew.MethodName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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