简体   繁体   English

动态更改列表视图项目背景

[英]Change list-view item background dynamically

I am having a list of songs and want to highlight(change background of) that song which is currently playing.It should be able to change background when my song finishes and goes to next one.I also want background to change when I select any list-view item. 我有一个歌曲列表,想突出显示正在播放的歌曲(改变背景)。当我的歌曲结束并转到下一首歌曲时,它应该能够改变背景。当我选择任何一个时,我也希望改变背景列表视图项。 Can somebody please guide me on how to move forward with this. 有人可以指导我如何继续前进。 Code:- 码:-

I am implmenting this code but more than one list-item color is changing 我正在执行此代码,但是多个列表项颜色正在更改

@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);
     }

}

I would suggest you to have aa field with the current song index in your adapter and depending on that index chose a background. 我建议您在适配器中有一个带有当前歌曲索引的字段,并根据该索引选择背景。 Then when the song stops playing you probably have some kind of callback, so there you can increase this index and invoke notifyDataSetChange() on your adapter to redraw your views. 然后,当歌曲停止播放时,您可能会进行某种回调,因此可以增加此索引并在适配器上调用notifyDataSetChange()以重notifyDataSetChange()视图。

And the last thing, when you select a list item, you should change current song index to the index of the selected item and also call notifyDataSetChange() . 最后,选择列表项时,应将当前歌曲索引更改为所选项目的索引,并调用notifyDataSetChange()

Here is some code 这是一些代码

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();
          }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM