我正在编写音频播放器应用程序,我必须在新歌开始时自动将焦点更改为下一个列表项目,我能够播放下一首歌但无法突出显示该歌曲。
<ListView
android:id="@+id/list_slidermenu"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="8"
android:layout_gravity="start"
android:scrollbars="none"
android:fastScrollEnabled="true"
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
/>
单击侦听器上的ListView:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
positionGlobal = position;
strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();
if(mediaPlayer!=null && mediaPlayer.isPlaying())
try {
mediaPlayer.stop();
} catch (Exception e) {
}
play();
textTrack.setText(strNTITLE);
}
});
}
用于自动切换到下一首歌曲的方法
public void next() {
if (positionGlobal == (audiosArrayList.size()-1)) {
positionGlobal = 0;
strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();
current_position = 0;
mediaPlayer.seekTo(current_position);
if(mediaPlayer!=null && mediaPlayer.isPlaying())
try {
mediaPlayer.stop();
} catch (Exception e) {
}
play();
textTrack.setText(strNTITLE);
}
else if(positionGlobal < audiosArrayList.size()) {
positionGlobal = positionGlobal+1;
strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();
current_position = 0;
mediaPlayer.seekTo(current_position);
if(mediaPlayer!=null && mediaPlayer.isPlaying())
try {
mediaPlayer.stop();
} catch (Exception e) {
}
play();
textTrack.setText(strNTITLE);
}
}
适配器类:
public class AudioAdapter extends BaseAdapter {
ArrayList<Audio> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context context;
public AudioAdapter(Context context, int resource, ArrayList<Audio> objects) {
this.context = context;
this.vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.Resource = resource;
this.actorList = objects;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return actorList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return actorList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.title);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.tvName.setText(actorList.get(position).getTitle());
return v;
}
static class ViewHolder {
public TextView tvName;
}
}
答案 0 :(得分:2)
您可以通过以下步骤列表实现此目的:
smoothScrollToPosition(int position)
代码段:
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// First set next position in positionGlobal
//highlight ListItem
audioAdapter.setSelectedItem(position);
// Then perform Click
listview.performItemClick(listview, positionGlobal, listview.getItemIdAtPosition(positionGlobal));
// Scroll to selected position
listview.smoothScrollToPosition(positionGlobal);
}
});
Adapter类看起来像:
public AudioAdapter(Context context, int resource, ArrayList<Audio> objects) {
public void setSelectedItem(int selectedItem) {
this.selectedItem = selectedItem;
notifyDataSetChange();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
// convert view = design
View v = convertView;
// Your code
// set selected Item
if(position == selectedItem) {
// you can define your own color of selected item here
v.setBackgroundColor(StringUtils.getColorFromResources(R.color.light_blue));
} else {
// you can define your own default selector here
v.setBackground(StringUtils.getDrawableFromResources(R.drawable.abs__list_selector_holo_light));
}
}
}
希望它有助于ツ
答案 1 :(得分:0)
为此,您必须向适配器发送值,该适配器将通知适配器列表视图的哪个位置将被设置为值。只有这样,您才能这样做。再次,您必须设置适配器。
答案 2 :(得分:0)
您的代码似乎很好,只需在适配器中调用notifyDataSetChange
即可通知它数组中的数据已更改并需要重新加载:
如果您需要在以前的位置获取物品,请执行以下操作:
if (position != 0) {
positionGlobal = position - 1;
strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();
}
答案 3 :(得分:0)
尝试setItemChecked,关注列表视图。没有电话nofifydatachanges
listview.setItemChecked(position, true);
你也试试
listview.setSelection(position);