如何将事件分配给按钮数组

时间:2014-10-29 05:17:05

标签: android

我有一个列表和自定义适配器。一个项目包含文本,2个按钮和一个搜索栏。

public class RecordAdapter extends BaseAdapter {
    private MediaPlayer mediaPlayer;
    private LayoutInflater inflater;
    private ArrayList<RecordBean> recordBeans;
    private final Handler handler = new Handler();
    private Context ctx;
    private int pos;
    private AudioManager am;

    public RecordAdapter(final Context ctx, ArrayList<RecordBean> recordBeans) {
        this.recordBeans = recordBeans;
        this.ctx = ctx;
        inflater = LayoutInflater.from(ctx);
        am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public int getCount() {
        return recordBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        pos = position;
        if (convertView == null) {
            view = inflater.inflate(R.layout.recorditem, parent, false);
            holder = new ViewHolder();
            holder.date = (TextView) view.findViewById(R.id.recordate);
            holder.from = (TextView) view.findViewById(R.id.recordFrom);
            holder.to = (TextView) view.findViewById(R.id.recordTo);
            holder.seekBar = (SeekBar) view.findViewById(R.id.seekBar);
            holder.start = (Button) view.findViewById(R.id.btnStart);
            holder.stop = (Button) view.findViewById(R.id.btnStop);
            holder.start.setTextColor(Color.WHITE);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.date.setText(" " + recordBeans.get(position).getDate());
        holder.from.setText(" c: " + recordBeans.get(position).getFrom());
        holder.to.setText(" на: " + recordBeans.get(position).getTo());

        holder.seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                seekChange(v);
                return false;
            }
        });
        holder.start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                releaseMP();
                try {
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(recordBeans.get(pos).getFile());
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                            holder.start.setTextColor(Color.RED);
                            holder.seekBar.setMax(mediaPlayer.getDuration());
                            startPlayProgressUpdater(holder.seekBar, holder.start);

                        }
                    });
                    mediaPlayer.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (mediaPlayer == null) {
                    holder.start.setTextColor(Color.WHITE);
                    Toast.makeText(ctx, "Запись невозможно воспроизвести!", Toast.LENGTH_LONG).show();
                    return;
                }
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Log.d("LOG_TAG", "onCompletion");
                        holder.start.setTextColor(Color.WHITE);
                    }
                });
            }
        });
        holder.stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mediaPlayer == null)
                    return;

                mediaPlayer.stop();
                holder.start.setTextColor(Color.WHITE);
            }
        });
        return view;
    }

    private void seekChange(View v) {
        if (mediaPlayer.isPlaying()) {
            SeekBar sb = (SeekBar) v;
            mediaPlayer.seekTo(sb.getProgress());
        }
    }
    private void releaseMP() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();
                mediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static class ViewHolder {
        TextView date;
        TextView from;
        TextView to;
        SeekBar seekBar;
        Button start;
        Button stop;
    }

    public void startPlayProgressUpdater(final SeekBar seek, final Button start) {
        seek.setProgress(mediaPlayer.getCurrentPosition());
        if (mediaPlayer.isPlaying()) {
            Runnable notification = new Runnable() {
                public void run() {
                    startPlayProgressUpdater(seek,start);
                }
            };
            handler.postDelayed(notification, 1000);
        } else {
            mediaPlayer.pause();
            seek.setProgress(0);
            start.setTextColor(Color.WHITE);
        }
    }
}

即我有一个包含音频文件和播放按钮的列表。当您单击要播放的文件时播放。通过点击停止 - 停止。但问题是我可以通过单击任何文件中的“停止”按钮来停止文件。例如,运行第一个文件并开始播放音乐。按下第二个文件上的停止,音乐停止了。

  1. 如果单击并拖动搜索栏,则滚动浏览该歌曲可以正常工作。如果你只是点击一首歌的中间,就像滚动不起作用。
  2. 图片 - http://dropmefiles.com/uP18Q

0 个答案:

没有答案