将数据从Fragment发送到Fragment

时间:2014-08-21 15:22:53

标签: android fragment

我是目前正在练习片段的Android编程的初学者。我有3个片段,我想从一个到另一个发送一些数据。在ArtistFragment中,我有一个艺术家列表,当用户点击某个艺术家时,我希望我的应用程序自动滑动到AlbumsFragment并仅显示以前所选艺术家的专辑。因此它应该将albumsList发送到AlbumsFragment。这是我的碎片和适配器。请帮助。

public class ArtistsFragment extends Fragment {

private ArrayList<Artist> artistList = new ArrayList<Artist>();
private  ArrayList<Album> albumList = new ArrayList<Album>();
private ArrayList<Song> songList = new ArrayList<Song>();

private ListView artistView;
private View rootView;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    rootView = inflater.inflate(R.layout.artists_fragment, container, false);

    showArtists();
    selectArtist();
    return rootView;
}

public void showArtists() {
    // TODO Auto-generated method stub
    artistList = (ArrayList<Artist>) getArguments().getSerializable("key");
    artistView = (ListView) rootView.findViewById(R.id.artistList);
    ArtistAdapter artistAdapter = new ArtistAdapter(getActivity(), R.layout.artists_item, artistList);
    artistView.setAdapter(artistAdapter);
}

public void selectArtist(){
    artistView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position,
                long arg3) {

            Artist artist = (Artist) artistView.getAdapter().getItem(position);
            albumList = artist.getAlbumList();
            Bundle bundle = new Bundle();
            bundle.putSerializable("key", albumList);

            getActivity().getActionBar().setSelectedNavigationItem(1);

        }
    });
}

}

public class AlbumsFragment extends Fragment {

public View rootView;
private ListView albumView;
private ArrayList<Album> albumList = new ArrayList<Album>();



@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    rootView = inflater.inflate(R.layout.albums_fragment, container, false);

    showAlbums();
    selectAlbum();
    return rootView;
}



private void showAlbums() {
    albumList = (ArrayList<Album>) getArguments().getSerializable("key");
    albumView = (ListView) rootView.findViewById(R.id.albumList);
    AlbumAdapter albumAdapter = new AlbumAdapter(getActivity(), R.layout.album_item, albumList);
    albumView.setAdapter(albumAdapter);
}

public void selectAlbum(){
    albumView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            getActivity().getActionBar().setSelectedNavigationItem(2);
        }
    });
}

}

最后是我的TabsPagerAdapter。

public class TabsPagerAdapter extends FragmentPagerAdapter {

private ArrayList<Artist> artistList = new ArrayList<Artist>();
private ArrayList<Album> albumList = new ArrayList<Album>();
private ArrayList<Song> songList = new ArrayList<Song>();

public TabsPagerAdapter(FragmentManager fm, ArrayList<Artist> artistList, ArrayList<Album> albumList, ArrayList<Song> songList) {
    super(fm);
    setArtistList(artistList);
    setAlbumList(albumList);
    setSongList(songList);

    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int index) {
    // TODO Auto-generated method stub
    switch(index){
    case 0:
        ArtistsFragment artistFragment = new ArtistsFragment();
        Bundle artistBundle = new Bundle();
        artistBundle.putSerializable("key", artistList);
        artistFragment.setArguments(artistBundle);
        return artistFragment;
    case 1:
        AlbumsFragment albumsFragment = new AlbumsFragment();
        Bundle albumBundle = new Bundle();
        albumBundle.putSerializable("key", albumList);
        albumsFragment.setArguments(albumBundle);
        return albumsFragment;
    case 2:
        SongsFragment songsFragment = new SongsFragment();
        Bundle songBundle = new Bundle();
        songBundle.putSerializable("key", songList);
        songsFragment.setArguments(songBundle);
        return songsFragment;
    }
    return null;
}

2 个答案:

答案 0 :(得分:1)

片段无法直接通信。这通过活动发生。两个碎片永远不应该直接通信。看看这个:http://developer.android.com/training/basics/fragments/communicating.html

还有另一种我更喜欢的方式,就是使用像otto这样的事件总线:http://square.github.io/otto/

使用otto,您可以从一个片段发布事件,并从另一个片段订阅该事件。

答案 1 :(得分:0)

尝试直接从一个片段与另一个片段进行通信并不是一个好主意。

最好的方法是在要在活动中实现的活动中提供Callback接口,或者在您的情况下,这可以在FragmentPagerAdapter中 看看[http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity] [1]

在回调方法中,你可以传递你想要的任何参数,然后开始例如另一个片段。