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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36347429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:45:16  来源:igfitidea点击:

How to call method in fragment from adapter Android

javaandroidandroid-fragments

提问by Evan Laksana

i need help so i have a fragment which have 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 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);
    }

回答by Satya siva prasad

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

回答by huseyin

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

回答by Akash Bisariya

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

回答by assif_tiger

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