简体   繁体   English

将数据从片段发送到片段

[英]Sending data from Fragment to Fragment

I am beginner at Android programming currently practicing Fragments. 我是目前正在练习Fragments的Android编程的初学者。 I have 3 Fragments and I would like to send some data from one to another. 我有3个片段,我想将一些数据从一个发送到另一个。 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. 在ArtistFragment中,我有一个艺术家列表,当用户单击某个艺术家时,我希望我的应用自动滑动到AlbumsFragment并仅显示以前选择的艺术家的专辑。 So it should send the albumsList to the AlbumsFragment. 因此,它应该将albumsList发送到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. 最后是我的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 看一下这个: 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/ 我更喜欢另一种方式,例如使用otto等事件总线: http : //square.github.io/otto/

Using otto you can publish events from one Fragment and subscribe to that event from the other Fragment. 使用otto可以发布一个片段中的事件,并订阅另一个片段中的事件。

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] 最好的方法是在活动中提供要在活动中实现的Callback接口,或者在您的情况下,可以在FragmentPagerAdapter中查看[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. 在回调方法中,您可以传递所需的任何参数,然后启动其他片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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