如何使用 CustomListAdapter 为不同的选项卡使用媒体播放器数组
How to use array for Media player using CustomListAdapter for different tabs
我有三个选项卡式活动,每个活动都有自己的图像和声音数组。图像和声音被传递到 CustomListAdapter 并作为列表返回:
选项卡活动:
public class Tab1Activity extends Fragment {
MediaPlayer mp = new MediaPlayer();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
String[] sound = {"path/to/media/file.mp3wl.gif",
"path/to/media/file.mp3",
"",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3"
};
String[] images = {"ImagePathwl.gif",
"ImagePath/image.jpg",
"",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg"
};
CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), sound, images);
list=(ListView) rootView.findViewById(R.id.list_view);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
try {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
mp.setDataSource(sound[+position]);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.e("GGGGGG", "prepare() failed");
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.reset();
}
});
return rootView;
}
自定义列表适配器:
public class CustomListAdapter extends ArrayAdapter<String> {
private final String[] sound;
private final String[] images;
public CustomListAdapter(Activity context, String[] sound, images) {
super(context, R.layout.animal_list, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.sound=sound;
this.image=image;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.animal_list, null,false);
imageView = (ImageView) rowView.findViewById(R.id.icon);
if (!imgid[position].isEmpty()) {
Picasso.with(context) // Context
.load(imgid[position]) // URL or file
.into(imageView); // An ImageView object to show the loaded image
}
}
}
如何使用 customAdapter 中的 imageView 为不同的 Tab 播放声音?
我将媒体播放器功能移至适配器并移至此处的 onclick 并将阵列声音传递至适配器。
我有三个选项卡式活动,每个活动都有自己的图像和声音数组。图像和声音被传递到 CustomListAdapter 并作为列表返回:
选项卡活动:
public class Tab1Activity extends Fragment {
MediaPlayer mp = new MediaPlayer();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
String[] sound = {"path/to/media/file.mp3wl.gif",
"path/to/media/file.mp3",
"",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3"
};
String[] images = {"ImagePathwl.gif",
"ImagePath/image.jpg",
"",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg"
};
CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), sound, images);
list=(ListView) rootView.findViewById(R.id.list_view);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
try {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
mp.setDataSource(sound[+position]);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.e("GGGGGG", "prepare() failed");
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.reset();
}
});
return rootView;
}
自定义列表适配器:
public class CustomListAdapter extends ArrayAdapter<String> {
private final String[] sound;
private final String[] images;
public CustomListAdapter(Activity context, String[] sound, images) {
super(context, R.layout.animal_list, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.sound=sound;
this.image=image;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.animal_list, null,false);
imageView = (ImageView) rowView.findViewById(R.id.icon);
if (!imgid[position].isEmpty()) {
Picasso.with(context) // Context
.load(imgid[position]) // URL or file
.into(imageView); // An ImageView object to show the loaded image
}
}
}
如何使用 customAdapter 中的 imageView 为不同的 Tab 播放声音?
我将媒体播放器功能移至适配器并移至此处的 onclick 并将阵列声音传递至适配器。