2016-03-19 10 views
0

ListViewの特定の行で1つの項目(ImageView)を変更したいとします。今私はそれを変更するにはonItemClickListenerビューを提供して変更しようとしました。それも動作します!ListViewの1つの要素を変更する

しかし、何とか上下にスクロールすると、他のリストビューの行も変更されます。私は問題について何かを見つけた"view recycling"しかし、私は本当にそれのための解決策を見つけませんでした。

誰かが役に立ちますか?

EDIT:ここ いくつかのコード:

マイリスナー:

LV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     ... 
     updateEQAnimation(position,view); 
     ... 
     } 

それを

private void updateEQAnimation(int index, View view){ 

    ColorStateList sColorStatePlaying; 
    ColorStateList sColorStateNotPlaying; 

    sColorStatePlaying = ColorStateList.valueOf(context.getResources().getColor(R.color.colorPrimary)); 

    if(view == null) 
     return; 
    AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.anim_current_song); 
    ImageView EQ_ANIM = (ImageView) view.findViewById(R.id.imageView_eq_animation); 
    EQ_ANIM.setVisibility(View.VISIBLE); 
    EQ_ANIM.setImageDrawable(animation); 
    EQ_ANIM.setImageTintList(sColorStatePlaying); 
    animation.start();} 

GetViewメソッドに変更する必要がある関数:

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    View rview = view; 
    holder = null; 

    if (rview == null) 
    { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     rview= inflater.inflate(R.layout.row_song, null, true); 
     holder = new ViewHolder(rview); 
     rview.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) rview.getTag(); 
    } 

    mArtLoader.loadBitmap(AL_songlist.get(position).getAlbumArtPath(), holder.imgAlbumart); 
    holder.txtTitle.setText(AL_songlist.get(position).getTitle()); 
    holder.txtArtist.setText(AL_songlist.get(position).getArtist()); 

    if(Main.songService.svc_mpcontroller != null && Main.global_music_controller != null) { 
     if (AL_songlist.get(position).getID() == Main.global_music_controller.get_current_id()) { 
      ColorStateList sColorStatePlaying; 
      ColorStateList sColorStateNotPlaying; 

      sColorStatePlaying = ColorStateList.valueOf(context.getResources().getColor(R.color.colorPrimary)); 
      AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.anim_current_song); 
      holder.imgEQAnimation.setVisibility(View.VISIBLE); 
      holder.imgEQAnimation.setImageDrawable(animation); 
      holder.imgEQAnimation.setImageTintList(sColorStatePlaying); 
      animation.start(); 
     } 
    } 

    return rview; 
} 
+0

にelseステートメントを追加する必要があります次に、あなたのアダプタのgetViewに次のことを行うことができます変更してから、そのIDをさらに操作してください。 –

+0

私のコードは現在 –

+0

に編集されています。 getItemId heint a inti int geri veriyo。 –

答えて

1

まずはリサイクルの説明をし、問題を解決します。一度に画面上に8つのリスト項目が表示されているとします。リストを下にスクロールすると、項目番号1が画面外になり、項目番号10が画面に表示され、項目番号10は項目番号1の表示を再利用します。これは、アダプタ番号getViewメソッドで項目番号10を正しく更新しない限り、項目番号10は項目番号1とまったく同じように見えることを意味します。

ビューリサイクルを理解したので、クリックしていないにもかかわらず他のアイテムに新しいドロウアブルが表示される理由を確認できます。アダプタのArrayListであろうと、アダプタが使用するカスタムオブジェクトであろうと、アイテムにそのイメージを持つアダプタに使用するデータセットをデータセットに格納する必要があります。

if(hasImage) 
{ 
    ColorStateList sColorStatePlaying; 
    ColorStateList sColorStateNotPlaying; 

    sColorStatePlaying = ColorStateList.valueOf(context.getResources().getColor(R.color.colorPrimary)); 
    AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.anim_current_song); 
    ImageView EQ_ANIM = (ImageView) view.findViewById(R.id.imageView_eq_animation); 
    EQ_ANIM.setVisibility(View.VISIBLE); 
    EQ_ANIM.setImageDrawable(animation); 
    EQ_ANIM.setImageTintList(sColorStatePlaying); 
    animation.start(); 
} 
else 
{ 
    // Set the default image 
} 

編集: あなたは、あなたがしたい項目にgetItemId()を使用する必要がありますあなたのgetView

if(Main.songService.svc_mpcontroller != null && Main.global_music_controller != null) { 
     if (AL_songlist.get(position).getID() == Main.global_music_controller.get_current_id()) { 
      ColorStateList sColorStatePlaying; 
      ColorStateList sColorStateNotPlaying; 

      sColorStatePlaying = ColorStateList.valueOf(context.getResources().getColor(R.color.colorPrimary)); 
      AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(context, R.drawable.anim_current_song); 
      holder.imgEQAnimation.setVisibility(View.VISIBLE); 
      holder.imgEQAnimation.setImageDrawable(animation); 
      holder.imgEQAnimation.setImageTintList(sColorStatePlaying); 
      animation.start(); 
     } 
    } 
    else 
     holder.imgEQAnimation.setVisibility(View.GONE); 
+0

ええええええええええ、私はそれを試してみましょう。だから私がそれを理解すれば、例えばブール値を置くリストを作ることができます(デフォルト=すべてfalse)。そして、私がクリックしたものは、私は真実にしなければなりません。 list.get(position)が真であるかどうかは、getViewで毎回確認する必要がありますか?私はそれを正しく理解しましたか? –

+0

それはどのように動作します。現在、アダプターのデータセットは何ですか? list.get(position)を呼び出す場合は、何を返しますか? – Dom

+0

私はまだ同じ問題を抱えています。私のデータセットはカスタムオブジェクト配列で、そのうちの1つにブール値があり、イメージを変更する必要があることを示しています –

関連する問題