更改图像按钮资源onclick

时间:2014-11-05 23:52:37

标签: android onclick imagebutton

我想更改与ImageButton onClick相关联的图片。我试图这样做,但它不起作用:

ImageButton play;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.listview_row, null);
    play = (ImageButton) vi.findViewById(R.id.imagePlay);

    play.setImageResource(R.drawable.play);
    play.invalidate();

        play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isPlaying){  
                    mMediaPlayer.stop();  
                    mMediaPlayer.release();
                    mMediaPlayer = null;
                    play.setImageResource(R.drawable.play);
                    play.invalidate();
                    isPlaying = false;
                }
                else{
                    mMediaPlayer = MediaPlayer.create(context, songs[position]);                
                    mMediaPlayer.start();
                    isPlaying = true;
                    play.setImageResource(R.drawable.pause);
                    play.invalidate();

                    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                        public void onCompletion(MediaPlayer mp) {
                            isPlaying = false;
                            play.setImageResource(R.drawable.play);
                            play.invalidate();

                        }
                    });
                }
            }
        });

这样,图像保持为播放按钮,永不改变暂停按钮。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您已在getView方法之外定义了ImageButton play。对于您添加的每一行,您都要为其分配一个新的ImageButton(添加的行中的一个)。

当您的onClick(View v)方法被触发时,play是添加的最后一个按钮。因此,首先在play方法中将您点击的按钮分配到onClick()

ImageButton play;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

        // SAME

        play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                play = (ImageButton) v.findViewById(R.id.imagePlay);

                if(isPlaying){  
                    // SAME
                }
                else{
                    // SAME
                }
            }
        });

答案 1 :(得分:0)

现在播放/暂停按钮正在工作,但我希望一旦媒体播放器停止播放,播放图标就会恢复。这种方式onCompletion()不起作用。我怎样才能让它发挥作用?

play.setImageResource(R.drawable.play);
        play.invalidate();

        play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                play = (ImageButton) v.findViewById(R.id.imagePlay);

                if(isPlaying){  
                        mMediaPlayer.stop();  
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                        play.setImageResource(R.drawable.play);
                        play.invalidate();
                        isPlaying = false;
                }
                else{
                    mMediaPlayer = MediaPlayer.create(context, songs[position]);                
                    mMediaPlayer.start();
                    isPlaying = true;
                    play.setImageResource(R.drawable.stop);
                    play.invalidate();

                    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                        public void onCompletion(MediaPlayer mp) {
                            isPlaying = false;

                            play.setImageResource(R.drawable.play);
                            play.invalidate();
                        }
                    });
                }
            }
        });