将对象从 Activity 传递到 ViewPager,然后传递到 Fragment

Passing Objects from Activity to ViewPager and then to Fragment

我正在接收一个从 Activity1 到 Activity2 的 DataTransferBetweenActivity 对象。 Activity2 有 ViewPager,在 ViewPager 里面我有 3 个片段。这是我的适配器代码 class for ViewPager

public class MusicPlayerTabAdapter extends FragmentPagerAdapter {
    private DataTransferBetweenActivity data;
    private String PARCEL_KEY = "data";
    public MusicPlayerTabAdapter(FragmentManager fm, DataTransferBetweenActivity data) {
        super(fm);
        this.data = data;
        ArrayList list = data.getSongsList();
        Iterator<Song> it = list.iterator();
        while(it.hasNext()) {
            Log.i("Reached", it.next().getTitle());
        }
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
            case 0:
                Bundle bundle = new Bundle();
                bundle.putParcelable(PARCEL_KEY, data);
                NowPlayingFragment nowPlayingFragment = new NowPlayingFragment();

                nowPlayingFragment.setArguments(bundle);

                return nowPlayingFragment;
            case 1:
                return new EqualizerFragment();
            case 2:
                return new LyricsFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

现在在 NowPlayingFragment 中 class 我正在从 getArguments() 获取 null。建议正确的方法来做到这一点。这是我的 NowPlayingFragment

代码
public class NowPlayingFragment extends Fragment {
    private ArrayList<Song> songsList;
    private int position;
    private String PARCEL_KEY = "data";
    private Bundle bundle = getArguments();
    private DataTransferBetweenActivity data = bundle.getParcelable(PARCEL_KEY);
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nowplaying_layout, container, false);
//        songsList = data.getSongsList();
//        position = data.getPosition();
        if(bundle == null)
        Log.i("Received DATA", "HELLO");
        return view;
    }
}  

我需要的另一个建议是,我将在我的 Activity2 中添加 MediaPlayerControls,并且当我 select 一首来自 NowPlayingFragment 的歌曲将包含歌曲列表时,我想更改歌曲。 建议构建此功能的正确方法。

你叫 getArguments() 太早了。

您应该在某些方法中初始化变量 private Bundle bundle。例如,在 onCreate()

public class NowPlayingFragment extends Fragment {
    private ArrayList<Song> songsList;
    private int position;
    private String PARCEL_KEY = "data";
    private Bundle bundle;
    private DataTransferBetweenActivity data;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        bundle = getArguments();
        data = bundle.getParcelable(PARCEL_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nowplaying_layout, container, false);
//        songsList = data.getSongsList();
//        position = data.getPosition();
        if(bundle == null)
        Log.i("Received DATA", "HELLO");
        return view;
    }
}