2013-07-17 77 views
0

我有歌曲列表,并要突出(变化的背景),这首歌是目前playing.It应该能够当我的歌完成就可以更改背景并进入下一个。当我选择任何列表视图项目时,我也希望更改背景。 有人请指导我如何前进与此。 代码: -更改列表视图项目背景动态

我implmenting这个代码,但多个列表项的颜色变化

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    // TODO Auto-generated method stub 
    super.bindView(view, context, cursor); 

    final View vi=inflater.inflate(layout, null, false); 
    TextView titleS=(TextView)view.findViewById(R.id.TitleSong); 
    TextView artistS=(TextView)view.findViewById(R.id.Artist); 
    int Title_index; 
    int Artist_index; 



     Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
     Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 
     titleS.setText(cursor.getString(Title_index)); 
     artistS.setText(cursor.getString(Artist_index)); 

    if(cursor.getPosition()==MainActivity.songPosition){ 
     titleS.setBackgroundColor(Color.BLUE); 
    } 

} 
+1

查看是否添加'else'语句一起去了'if'帮助。添加'else {titleS.setBackgroundColor(Color.WHITE); }'。 – Vikram

+0

感谢它现在的工作。 –

回答

0

我建议你有AA字段在适配器当前歌曲指数,取决于索引选择了一个背景。然后,当歌曲停止播放时,您可能会有某种回调,因此您可以增加此索引并在适配器上调用notifyDataSetChange()来重新绘制视图。

而最后一件事,当你选择一个列表项,你应该改变当前歌曲索引到所选项目的索引,也称notifyDataSetChange()

下面是一些代码

getView(int position, View convertView, ViewGroup parent) { 
    //initialize view 

    if (position == currentSongPosition) { 
     yourView.setBackground(...); 
    } 

    yourView.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       currentSongPosition = position; 
       notifyDataSetChange(); 
      } 
    }); 

} 
+0

我应该如何实现适配器?我的意思是根据什么改变背景? 我不能使用的onClick方法 –

+0

我写了一些代码在那里做。实际上,你可以做到这一点onClick – Desert

+0

因为我使用CUrsorAdapter所以我应该使用它,而不是在bindView方法? –

相关问题