更改适配器内的视图

时间:2015-06-24 16:57:00

标签: android

我必须展示几个独立的媒体播放器。我选择在Listfragment中使用Arrayadapter来构建视图并处理媒体播放器,以实现这一目标。不幸的是,我找不到从适配器内部更新当前媒体播放器的相应搜索栏和时间戳的方法。我尝试在Adapter中使用Runnable以及父视图的post方法。似乎没什么用。非常感谢帮助。

MyAdapter

public class MusicPlayerAdapter extends ArrayAdapter<Song> {
    private final Context context;
    private final List<Song> songs;
    private MediaPlayer mediaPlayer = null;
    private int resource;
    private Handler mediaHandler;
    private SeekBar seekbar;
    private TextView timestamp;
    private Runnable run;
    private Boolean playerNotPrepared = true;


    public MusicPlayerAdapter(Context context,
                              int resource,
                              List<Song> songs) {
        super(context, resource, songs);
        this.context = context;
        this.resource = resource;
        this.songs = songs;

        mediaHandler = new Handler();
        run = new Runnable() {
            @Override
            public void run() {
                mediaUpdate();
            }
        };
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //return super.getView(position, convertView, parent);
        Song currentSong = songs.get(position);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final Uri songUri = Uri.parse(currentSong.getUrl());

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        final View entry = inflater.inflate(resource, parent, false);

        TextView songTitle = (TextView) entry.findViewById(R.id.song_title);
        ImageButton playPauseButton
                = (ImageButton) entry.findViewById(R.id.play_pause_button);

        seekbar = (SeekBar) entry.findViewById(R.id.seekBar);
        timestamp = (TextView) entry.findViewById(R.id.timestamp);

        songTitle.setText(currentSong.toString());

        playPauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (playerNotPrepared) {

                    try {
                        mediaPlayer.setDataSource(context, songUri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    mediaPlayer.prepareAsync();

                    mediaPlayer.setOnPreparedListener(new MediaPlayer
                            .OnPreparedListener() {


                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            playerNotPrepared = false;
                            mp.start();
                            seekbar.setMax(mp.getDuration());
                        }
                    });

                    mediaPlayer.setWakeMode(
                            context, PowerManager.PARTIAL_WAKE_LOCK);
                } else if (! mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                } else if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
            }
        });

        parent.post(run);
        return entry;
    }

    private void mediaUpdate() {
        if (! playerNotPrepared) {
            Integer currentPos = mediaPlayer.getCurrentPosition();
            seekbar.setProgress(currentPos);
            timestamp.setText(currentPos.toString());
            notifyDataSetChanged();
        }
        mediaHandler.postDelayed(run, 1000);
    }
}

从ListFragment中调用所有内容 的 ListFragment

public class SongFragment extends ListFragment {
    private RigDBAccess rig;
    private RigBand currentBand;

    public SongFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = this.getArguments();
        String apiKey = bundle.getString("apiKey");
        rig = (RigDBAccess) bundle.getSerializable("rig");
        currentBand = (RigBand) bundle.getSerializable("currentBand");

        setListAdapter(
                new MusicPlayerAdapter(
                        getActivity(),
                        R.layout.fragment_song,
                        currentBand.getSongs()));
    }
}

0 个答案:

没有答案