简体   繁体   English

Android,Access Fragment方法来自单独的类

[英]Android, Access Fragment method from separate class

I have a Fragment : 我有一个片段

public class CustomFrag extends Fragment{
    ...
    public void refreshList(){
        ...
    }
}

I have a separate Class : 我有一个单独的班级

public class SomeClass{
    ...
}

I am trying to call refreshList() from SomeClass : 我试图从SomeClass调用refreshList()

String tagName = "android:switcher:" + R.id.pager + ":" + 1;
CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
f2.refreshList();

But this shows Cannot resolve method 'getActivity' . 但是这表明Cannot resolve method 'getActivity' If I add to the class: 如果我加入课程:

extends Fragment

All the warnings go away, but the app crashes, with a null-pointer exception to the CustomFrag f2 = (CustomFrag)... line. 所有警告消失,但应用程序崩溃, CustomFrag f2 = (CustomFrag)...行的空指针异常。

I have another fragment, contained within the same parent as CustomFrag , and the method call described above works great. 我有另一个片段,包含在与CustomFrag相同的父节点中,并且上面描述的方法调用效果很好。

How can I access the CustomFrag methods from SomeClass ? 如何从SomeClass访问CustomFrag方法? This question (and similar) are asked all over, but most do not have accepted answers, or else have very vague ones that are of little help. 这个问题(和类似的问题)都被问到了,但是大多数问题都没有得到接受,或者有一些非常含糊的问题。

Thanks in advance. 提前致谢。

Edit: Temp solution 编辑:临时解决方案

I am calling methods that belong to SomeClass from within the two aforementioned Fragments. 我在前面提到的两个片段中调用属于SomeClass方法。 What I came up with is the following: 我想出的是以下内容:

Within FragOne FragOne

public class FragOne extends Fragment{

    ...

    String tagName = "android:switcher:" + R.id.pager + ":" + 1;
    CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
}

And then: 然后:

public class FragOne extends Fragment{

    ...

    String tagName = "android:switcher:" + R.id.pager + ":" + 1;
    CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);

    SomeClass obj = new SomeClass(...);
    obj.someMethod(f2);
}

Where someMethod then can use 那么someMethod可以使用

f2.refreshList();

This solves the issue I have been having, but it would still be nice to know a more direct way to access the Fragment's methods via a separate Class. 这解决了我一直遇到的问题,但是通过一个单独的类来了解更直接的方法来访问Fragment的方法仍然是一件好事。

Further answers that solve this problem are welcome, and will be accepted as solution. 解决此问题的其他答案是受欢迎的,并将被接受为解决方案。

HomeFragment.java HomeFragment.java

public class HomeFragment extends Fragment {

    private static HomeFragment instance;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        instance = this;

        return view;
    }

    public static HomeFragment getInstance() {
        return instance;
    }

    public void myMethod() {
        // do something...
    }

}

AnotherClass.java AnotherClass.java

public Class AnotherClass() {
     // call this method
     HomeFragment.getInstance().myMethod();
}

The issue is that you need to get the activity context of the fragments. 问题是您需要获取片段的活动上下文。 You cannot just make up a new class extending a fragment class, without actually instantiating the fragment and associating it with an activity, and hope to get back your activity context from it. 您不仅可以构建一个扩展片段类的新类,而无需实际实例化片段并将其与活动相关联,并希望从中获取活动上下文。

If you wish to use SomeClass independent of the Fragment, you can declare a variable to hold the parent activity 如果您希望独立于Fragment使用SomeClass ,则可以声明一个变量来保存父活动

private FragmentActivity mActivity;

and then in the constructor, you may pass the activity context reference into it. 然后在构造函数中,您可以将活动上下文引用传递给它。

public SomeClass (FragmentActivity activity) {
    mActivity = activity;
    ...
}

Afterwards, you can call 之后,你可以打电话

mActivity.getSupportFragmentManager().findFragmentByTag(tagName);

when you need it within SomeClass . 当你在SomeClass需要它时。

Create interface for that 为此创建接口

public interface Callback{
void refreshList();
}

This is your fragment 这是你的片段

public class CustomFrag extends Fragment implements Callback{
...

new SomeClass().setListener(CustomFrag .this);

@Override
  public void refreshList(){

//do work
 }
}

Write your class 写你的课

 public class SomeClass{
 private Callback mCallback;

 public void setListener(Callback callBack){
 mCallback=callBack;
  }
}

call Fragment refreshListview from your SomeClass like this 像你这样从SomeClass调用Fragment refreshListview

mCallback.refreshListView();

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

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