【问题标题】:Calling activity method from FragmentPagerAdapter从 FragmentPagerAdapter 调用活动方法
【发布时间】:2016-03-10 16:25:25
【问题描述】:

我的MainActivity 有一个double 纬度值,我想将它传递给FragmentPagerAdapter 内的片段,所以我在MainActivity.java 中创建了这个getter 方法:

public double getLatitude() {
   return this.latitude;
}

现在我试图在创建片段时将此值传递给片段(getItem() 方法是FragmentPagerAdapter 类的一部分):

    public Fragment getItem(int position) {
            if (position == 0) {
                return new Fragment1();
            } else if (position == 1) {
                return new Fragment2();
            } else {
                Fragment3 fragment3 = new Fragment3();
                Bundle args = new Bundle();
                args.putDouble(fragment3.ARG_PARAM1, getActivity().getLatitude());
                fragment3.setArguments(args);
                return fragment3;
            }
        }

但是,我收到一个编译错误,上面写着Error:(156, 68) error: cannot find symbol method getActivity()

当我创建一个片段的新实例时,如何获取我的主要活动的getLatitude()

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    为什么不在适配器的构造函数中传递该值,例如

    假设您的适配器类名称是 ViewPagerPagerAdapter 然后在该适配器中执行以下操作

    public class ViewPagerPagerAdapter extends FragmentPagerAdapter {
    
    private double mLattitude;
    public ViewPagerPagerAdapter (double lattitude) {
        this.mLattitude = lattitude;
    }
    
    //Now use that mLattitude wherever you want
    
    public Fragment getItem(int position) {
                if (position == 0) {
                    return new Fragment1();
                } else if (position == 1) {
                    return new Fragment2();
                } else {
                    Fragment3 fragment3 = new Fragment3();
                    Bundle args = new Bundle();
                    args.putDouble(fragment3.ARG_PARAM1, mLattitude);
                    Log.d("WHATEVER", "WHATEVER: " + String.valueOf(mLattitude));
                    fragment3.setArguments(args);
                    return fragment3;
                }
            }
    }
    

    现在,当您从 MainActivity 启动适配器时,请执行

    ViewPagerAdapter adapter = new ViewPagerAdapter(lattitude);
    

    【讨论】:

    • 如果纬度不变,这将起作用。如果是这样,那么您将不得不添加一个 setter 方法并在每次纬度变化时从活动中更新您的适配器..
    • 那么我们可以将在构造函数中传递的上下文转换为活动并访问我们想要的任何内容,例如 ((MainActivity)context).getLatitude()
    • 是的,我同意。我认为这样会更好。此外,如果您使用接口,则不需要强制转换。例如。当您创建一个用于许多活动的适配器时,您不必每次都检查活动实例并进行强制转换。
    • 那么当位置改变时更新分形纬度的最佳方法是什么?
    【解决方案2】:

    FragmentPagerAdapter 不是Fragment,即它没有getActivity() 方法。如果您需要从活动中访问纬度,只需将活动引用传递给您的 FragmentPagerAdapter 构造函数。在这里使用一个接口,像LatitudeProvider 这样的东西会更加灵活。

    例子:

    class MainActivity extends Activity implements LatitudeProvider {
    
        // pager adapter creation
        new MyFragmentPagerAdapter(this);
    
        @Override
        public double getLatitude() {
            return this.latitude;
        }
    }
    

    您的片段寻呼机适配器:

    interface LatitudeProvider {
        double getLatitude();
    }
    
    public class MyFragmentManagerAdapter extends FragmentPagerAdapter {
        private final LatitudeProvider latitudeProvider;
    
        public MyFragmentManagerAdapter(LatitudeProvider latitudeProvider) {
            this.latitudeProvider = latitudeProvider;
        }
    
        ...
        // Now you can get your latitude like this:
        latitudeProvider.getLatitude();
    }
    

    【讨论】:

    • 接口的声明是否也应该放在 MainActivity 中?
    • 不,它应该与适配器在同一个文件中,或者,在一个单独的文件中,无论你喜欢什么。在 MainActivity 中声明它没有多大意义,因为它不属于活动。接口描述了提供纬度值的东西,它可以是任何东西。所以,不要创建这个额外的依赖。也许您稍后会决定将纬度逻辑移动到一个单独的类中,或者您可能会有多个提供纬度的活动。在 MainActivity 之外拥有纬度提供者对我来说更有意义。
    【解决方案3】:

    getActivity() 返回上下文而不是 MainActivity 的对象。据我所知,没有办法在 android 中获取 Activity 的当前对象。 所以你应该将它设为公开和静态,如下所示。

    public static double getLatitude() {
       return this.latitude;     // make latitude public and static as well 
    }
    

    现在在您的 MainActivity.java 中

    MainActivity.getLatitude();
    

    如果值没有变化。然后你可以在构造函数中传递它。

    【讨论】:

    • 这不是一个好主意。在可以避免的情况下,不要将数据设为静态。如果你这样做,你的架构会很糟糕。stackoverflow.com/questions/1766715/…
    • 好的,我同意你的看法。但是在这个问题中我们如何避免静态?
    • 见我上面的回答。或者,@sam_0829 的回答是,两者都比静态效果好得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2012-03-28
    • 2012-09-21
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多