【问题标题】:How to call method in fragment from adapter Android如何从适配器Android调用片段中的方法
【发布时间】:2016-04-01 03:18:59
【问题描述】:

我需要帮助,所以我有一个片段,它有一个 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);
}

【问题讨论】:

  • 在 Adapter 类中创建一个接口,让我们调用具有 onClickHandler(View v) 方法的 buttonClickHandler 接口,现在当你在片段内初始化适配器时,同时初始化这个接口并调用你的 doEdit 方法。

标签: java android android-fragments


【解决方案1】:

更新您的适配器构造函数以接受 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();

【讨论】:

  • 最简单的方法之一。谢谢你。
【解决方案2】:

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

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

【讨论】:

  • onItemClickListener i Fragment 部分是什么?
  • @ArnoldBrown 这是一个接口,可让您将数据从 RecyclerView 发送到 Fragment。
【解决方案3】:

使用接口创建回调

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

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)

示例代码: 您可以找到一个示例代码here
免责声明:示例代码基于上述思想,但不是很容易理解。

【讨论】:

    【解决方案4】:

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

    这里是带有项目点击操作的回收站视图的示例应用程序。 检查一次它可能对您有用。 https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0

    【讨论】:

      【解决方案5】:

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

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

      然后

      myFragmentNew.MethodName();
      

      【讨论】:

      • 如何从 onBindViewholder 调用方法而不是从构造函数?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多