2017-04-11 10 views
0

모든 행에 재생 및 일시 중지 버튼이있는 노래의 목록보기가 있습니다. 내 목록보기에서 두 번 일시 중지 아이콘을 가질 수 없습니다. 아이콘을 재생하려면 먼저 모두 재설정해야 아이콘을 일시 중지하도록 선택한보기를 설정하십시오. 어떻게하면됩니까? 아니면 더 나은 솔루션을 제공 할 수 있습니까?한 번의 클릭으로 listview 내의 모든 이미지보기 리소스를 변경합니다.

모델 클래스에서 (제품) :

public int currentPosition= -1; 

어댑터 에서 :

public interface PlayPauseClick { 
    void playPauseOnClick(int position); 
} 
private PlayPauseClick callback; 
public void setPlayPauseClickListener(PlayPauseClick listener) { 
    this.callback = listener; 
} 
. 
. 
. 


    holder.playPauseHive.setImageResource(product.getPlayPauseId()); 
    holder.playPauseHive.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (callback != null) { 
       callback.playPauseOnClick(position); 
       if (position == product.currentPosition) { 
        product.setPlayPauseId(R.drawable.ic_pause); 
        //set the image to pause icon 
       }else{ 
        //set the image to play icon 
        product.setPlayPauseId(R.drawable.ic_play); 
       } 
       notifyDataSetChanged(); 
      } 
     } 
    }); 

내 콜백활동 내부 :

내 코드입니다
@Override 
public void playPauseOnClick(int position) { 
    final Product product = songList.get(position); 
    if(product.currentPosition == position){ 
     product.currentPosition = -1; //pause the currently playing item 
    }else{ 
     product.currentPosition = position; //play the item 
    } 
    this.adapter.notifyDataSetChanged(); 
} 

답변

0

제 경우에는 변수를 사용하여 현재 재생 항목 위치를 저장합니다.

int x = -1; //-1 can indicate nothing was currently playing 

는 그래서 playPauseOnClick()에서이

@Override 
public void playPauseOnClick(int position) { 
    if(x == position){ 
     x = -1; //pause the currently playing item 
    }else{ 
     x = position; //play the item 
    } 
    this.adapter.notifyDataSetChanged(); 
} 

처럼 난 당신이 정말로 그들을 필요로하지 않기 때문에 product.setPlayPauseId()이 제거 이유를 뭔가를 할 수 있다고 가정 해 봅시다. 이전에 생성 한 x 개의 변수를 기반으로 재생 또는 일시 중지 아이콘을 설정하면됩니다. 당신이 당신의 어댑터가 당신을 위해 모든 일을 할 것입니다 adapter.notifyDataSetChanged() 전화 그래서 한 번 getView()

Product product = songList.get(position); 
if (position == x) { 

    //set the image to pause icon 
}else{ 
    //set the image to play icon 
} 

이 그런 짓을. 값 -1이없는 변수 x이 일시 중지 아이콘을 표시 할 때마다 일시 중지 아이콘도 한 위치에 표시되도록 할 수 있습니다.

희망이 도움이됩니다.

+0

나는 내 코드와 내 질문을 답을 기반으로 편집했지만 아직 기회가 없었습니다. – Majid