我有一个在后台播放媒体的活动,我有一个列表视图,其中有特定于歌词的行,整个歌词都在一个数组中。现在我希望当媒体播放器到达特定时间时,相关歌词(行)滚动到列表视图的中心,并且该行以不同的阴影突出显示。为此,我在 getview 方法中使用了 smoothScrollto() 函数。然而,一旦 if else 条件执行,我的列表就会以一种不稳定的方式上下滚动,而且它的重复性如此之大,以至于无法阅读。下面是我的代码:
类级变量:
String [] LyricsE;
String [] LyricsH;
ListView listView;
private ArrayAdapter dataAdapter;
创建:
dataAdapter = new arrayAdapter(MainActivity.this, LyricsH);
listView.setAdapter(dataAdapter);
自定义类:
public class arrayAdapter extends ArrayAdapter<String[]> {
private final Context context;
private final String[] lyrics;
public arrayAdapter(Context context, String[]lyrics ) {
super(context, R.layout.list);
this.context = context;
this.lyrics = lyrics;
}
public int getCount() {
return lyrics.length;
}
public String[] getItem(int position) {
return (lyrics);
}
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
}
TextView textView = ((TextView) convertView.findViewById(R.id.textView2));
textView.setText(lyrics[position]);
if(mediaPlayer.getCurrentPosition()/1000 > 0 && mediaPlayer.getCurrentPosition()/1000 < 20){
listView.smoothScrollToPosition(3);
System.out.println("FIrst if");
}else if(mediaPlayer.getCurrentPosition()/1000 > 20 && mediaPlayer.getCurrentPosition()/1000 < 54){
listView.smoothScrollToPosition(4);
System.out.println("2 if");
}else if(mediaPlayer.getCurrentPosition()/1000 > 54 && mediaPlayer.getCurrentPosition()/1000 < 67){
listView.smoothScrollToPosition(5);
System.out.println("3 if");
}else if(mediaPlayer.getCurrentPosition()/1000 > 67 && mediaPlayer.getCurrentPosition()/1000 < 75){
listView.smoothScrollToPosition(6);
System.out.println("4 if");
}else if(mediaPlayer.getCurrentPosition()/1000 > 20 && mediaPlayer.getCurrentPosition()/1000 < 54){
listView.smoothScrollToPosition(7);
System.out.println("5 if");
}
return convertView;
}
}