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

时间:2015-01-04 12:12:15

标签: android android-fragments android-mediaplayer

我从一个Activity1到Activity2收到一个DataTransferBetweenActivity对象。 Activity2有ViewPager,在ViewPager里面我有3个片段。以下是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类中,我从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,当我从NowPlayingFragment中选择一首歌曲时,我想要更改歌曲。 建议构建此功能的正确方法。

1 个答案:

答案 0 :(得分:2)

您过早致电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;
    }
}