简体   繁体   中英

Sending data from Fragment to Fragment

I am beginner at Android programming currently practicing Fragments. I have 3 Fragments and I would like to send some data from one to another. In ArtistFragment I have a list of artists, and when a user clicks some artist I would like my app to auto swipe to AlbumsFragment and display albums from previous selected artist only. So it should send the albumsList to the AlbumsFragment. Here are my Fragments and Adapter. Please Help.

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);
        }
    });
}

}

And Finally my 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;
}

Fragment can't communicate directly. This happens throught the Activity. Two Fragments should never communicate directly. Take a look at this: http://developer.android.com/training/basics/fragments/communicating.html

There is another way that I prefer, which is using Event Bus like otto: http://square.github.io/otto/

Using otto you can publish events from one Fragment and subscribe to that event from the other Fragment.

It is not a good idea to try to communicate directly from one fragment to another one.

The best way is to provide a Callback interface in the activity that to be implemented in the activity or in your case this could be in FragmentPagerAdapter Take a look at [http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity][1]

In the callback method you could pass whatever parameters you want and then to start for example the other fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM