滚动期间listview在listview中的变化

时间:2016-12-20 10:05:26

标签: android listview imageview media-player

我有一个listview包含text和imageview,这个imageview实现onClickListener()用于播放某些媒体,当用户点击imageview时播放音频文件并将imageview播放按钮更改为暂停按钮,直到这一点它的工作原理如此之好我,但当我向下滚动到列表视图中的最后一项时,暂停按钮返回默认按钮(播放按钮),音频文件继续播放..为什么会发生这种情况?非常感谢

  //--------------- Play Button -------------------------------------------------------------------
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            //--------------determine the id to play ----------------------------------
             idd = items.get(position).getId();
             FilteringNums f = new FilteringNums();
             f.convert(idd);
             String attribute   = f.r;
            //------------------------------------------------------------------------- 
                 int resourceId = arg0.getResources().getIdentifier(attribute+position, "raw", activity.getPackageName());
                 final MediaPlayer Mp =MediaPlayer.create(getContext(), resourceId);
            try{        
                 int r = Mp.getCurrentPosition();
                 Mp.seekTo(r);
                 Mp.start();                

             b.setEnabled(false);
             b.setVisibility(b.INVISIBLE);
             pause.setVisibility(pause.VISIBLE);
  //------------------------------------------------------------------------------------------------

            //-------------- Pause Button ---------------------------------------------------------
             pause.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    if(Mp.isPlaying())
                    {
                    Mp.pause();
                     Log.d("media player", "is paused");
                    resume.setVisibility(resume.VISIBLE);
                    stop.setVisibility(stop.VISIBLE);
                    }
                }
                });

1 个答案:

答案 0 :(得分:0)

这是viewHolder

static class ViewHolder {
    public TextView tv_id ;
    public ImageView b;
    public ImageView pause;
    public ImageView resume;
    public ImageView stop;
}

这就是它的实现

final ViewHolder holder = new ViewHolder();
    holder.tv_id= (TextView) convertView.findViewById(R.id.sora_id);

    holder.b= (ImageView)  convertView.findViewById(R.id.list_item_btn);
    holder.pause= (ImageView)  convertView.findViewById(R.id.pause_item_btn);
    holder.resume= (ImageView)  convertView.findViewById(R.id.resume_item_btn);
    holder.stop= (ImageView)  convertView.findViewById(R.id.stop_item_btn);
    if (position % 2 == 0 )
        holder.tv_id.setText("سورة"+" "+items.get(position).getSora().toString());
    else
        holder.tv_id.setText(items.get(position).getSora().toString()); 
    holder.tv_id.setTextColor(colors2[colorPos2]);
    holder.tv_id.setTypeface(null, Typeface.BOLD);
    holder.b.setBackgroundResource(R.drawable.speaker);
    holder.pause.setBackgroundResource(R.drawable.pause);
    holder.resume.setBackgroundResource(R.drawable.play);
    holder.stop.setBackgroundResource(R.drawable.stop);