简体   繁体   English

如何从片段类到适配器类访问方法

[英]How to access a method from fragment class to adapter class

I want to use reloadTransList() method in the onClick function in the adapter class. 我想在适配器类的onClick函数中使用reloadTransList()方法。 This is the method in a fragment class. 这是片段类中的方法。

translistTab.java

public void reloadTransList() {
    productTransList.clear();
    transRecyclerView.getAdapter().notifyDataSetChanged();
    loadTransList();
}

And this is how I called it in the adapter class. 这就是我在适配器类中调用它的方式。

prodAdapter.java

translistTab translistTab = new translistTab();
translistTab.reloadTransList();

EDIT: 编辑:

The method now is being called. 现在正在调用该方法。 But the loadproducts(); 但是loadproducts(); is not working without errors. 无法正常工作。 The loadproducts() method is the loading of products in recyclerview. loadproducts()方法是在recyclerview中加载产品。 The method is running, but the recyclerview is not reloading. 该方法正在运行,但是recyclerview没有重新加载。

Maybe you forgot to add setOnCLickListner(this) in your ViewHolder inner class in the adapter. 也许您忘记了在适配器的ViewHolder内部类中添加setOnCLickListner(this)。 If you add this, you would be able to call reloadTransList() from onClick() method. 如果添加此选项,则可以从onClick()方法调用reloadTransList()。

A short answer is to pass the fragment into your adapter via constructor or setter. 一个简单的答案是通过构造函数或setter将片段传递到适配器中。

A longer, better answer is to define a custom interface that gets implemented by your fragment and passed into your adapter. 更长的更好的答案是定义一个自定义接口,该接口由您的片段实现并传递到适配器中。 The implemented interface will contain your reloadTransList() method 实现的接口将包含您的reloadTransList()方法

EDIT 编辑

I don't know exactly how your classes are setup, but the long solution would look something like the following. 我不知道您的班级是如何设置的,但是长的解决方案看起来像下面的样子。

First, define a interface. 首先,定义一个接口。

public interface MyCustomInterface {
    void onCustomAction();
}

The exact implementation can vary depending on your needs. 确切的实现可以根据您的需求而变化。

Next, in your fragment class, implement the interface. 接下来,在您的片段类中,实现接口。 Place the reloadTransList() method inside. reloadTransList()方法放入其中。

public class MyFragment extends Fragment implements MyCustomInterface {
    // All your other, unrelated Fragment code

    @Override
    public void onCustomAction() {
        reloadTransList();
    }
}

Now, in your adapter class. 现在,在您的适配器类中。 Pass the Interface (not the Fragment) into the adapter and assign it to an instance variable. 将接口(不是Fragment)传递到适配器中,并将其分配给实例变量。 I personally prefer to do that via constructor: 我个人更喜欢通过构造函数来做到这一点:

public class prodAdapter extends Adapter { // I'm only assuming the adapter you are using. It has no relevance to this

    private MyCustomInterface customInterface;

    public prodAdapter(MyCustomInterface customInterface) {
        this.customInterface = customInterface;
    }
}

From there, simply call customInterface.onCustomAction() wherever you need the reloadTransList() method fired. 从那里,只需在需要触发reloadTransList()方法的地方调用customInterface.onCustomAction() reloadTransList()

The short solution is just like the above code, except with just the fragment and no interface. 简短的解决方案与上面的代码类似,只是片段和没有接口。 Strongly recommend you go the long way. 强烈建议您走很长一段路。 Especially for testing purposes. 特别用于测试目的。

EDIT #2 编辑#2

Another advantage to the long solution is you can re-use the adapter in another class without changing it. 长期解决方案的另一个优点是您可以在不更改它的情况下在另一个类中重用适配器。 For your other fragments using the adapter, implement the MyCustomInterface . 对于使用适配器的其他片段,请实现MyCustomInterface

public class AnotherFragment extends Fragment implements MyCustomInterface {
    @Override
    public void onCustomAction() {
        // Put whatever you need here. Or nothing.
    }
}

This allows the adapter to be re-used across multiple fragments. 这允许适配器在多个片段之间重复使用。

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

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