如何从活动

时间:2017-03-11 17:05:52

标签: android android-fragments android-viewpager

我试图调用我的片段的一些方法,但问题是我使用Infinite ViewPager而不是正常的ViewPager。我尝试了以下方法来调用片段方法。

  1. 我无法在无限ViewPager片段中设置ID。
  2. 我无法在java中使用fragment标签。
  3. 我尝试了getItem方法但导致崩溃.error
  4.   

    致命的例外:主要                                                                                        处理:beatbox.neelay.dialogtest2,PID:28997                                                                               java.lang.NullPointerException:尝试调用虚方法' android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String,int)'在null对象引用上                                                                                   at beatbox.neelay.dialogtest2.Songdata.getPlayList(Songdata.java:26)                                                                                   at beatbox.neelay.dialogtest2.Songs.dataretrive(Songs.java:56)                                                                                   at beatbox.neelay.dialogtest2.MainActivity.onFragmentInteraction(MainActivity.java:134)                                                                                   at beatbox.neelay.dialogtest2.BlankFragment.onButtonPressed(BlankFragment.java:70)                                                                                   at beatbox.neelay.dialogtest2.BlankFragment $ 2.onClick(BlankFragment.java:60)                                                                                   在android.view.View.performClick(View.java:5207)                                                                                   在android.view.View $ PerformClick.run(View.java:21177)                                                                                   在android.os.Handler.handleCallback(Handler.java:739)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5441)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:738)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

    InfiniteViewPager的另一个问题是,如果用户继续像螺旋一样滑动它,位置就不一样了。 这就是我称之为InfiniteViewPager的方式

    private InfiniteViewPager pager = null;
    private MyAdapter adapter;
    private InfinitePagerAdapter myAdapter;
    
     pager = (InfiniteViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
    
    
         adapter = new MyAdapter(fragmentManager);
         myAdapter = new InfinitePagerAdapter(adapter);
        pager.setAdapter(myAdapter);
    

    我尝试这种方法来调用方法

       int pos = pager.getCurrentItem();
        Fragment activeFragment = adapter.getItem(pos);
        if(pos == 2){
            ((Songs)activeFragment).dataretrive();
            ((Songs)activeFragment).dataset();
    
        }
    

    任何提示如何调用该方法都会有所帮助。

1 个答案:

答案 0 :(得分:1)

通常,从活动中直接调用片段的方法是不好的做法。

我建议你这样做通知/听众机制。像这样的东西:

public interface SomeEventListener {
    void onEvent();
}

public interface SomeEventNotifier {
    void addListener(SomeEventListener listener);

    void removeListener(SomeEventListener listener);
}

public class ExampleActivity extends Activity implements SomeEventNotifier {
    private final List<SomeEventListener> listeners = new CopyOnWriteArrayList<>();

    @Override
    public void addListener(SomeEventListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeListener(SomeEventListener listener) {
        listeners.remove(listener);
    }

    private void notifySomeEvent() {
        for (SomeEventListener listener : listeners) {
            listener.onEvent();
        }
    }
}

public class ExamplpeFragment extends Fragment implements SomeEventListener {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (isIntresterInSomeEvent()) {
            SomeEventNotifier notifier = (SomeEventNotifier) getActivity();
            notifier.addListener(this);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (isIntresterInSomeEvent()) {
            SomeEventNotifier notifier = (SomeEventNotifier) getActivity();
            notifier.removeListener(this);
        }
    }

    private boolean isIntresterInSomeEvent() {
        //here you have to add you logic to determinate rather the instance of fragment need notification about the event
        return true;
    }

    @Override
    public void onEvent() {
        //here you will get call when activity notifySomeEvent is called.
    }
}

调用notifySomeEvent时,将通知所有附加的片段。