0
我正在为我们的音乐学校开发一个android应用程序。我设计了一个包含6个项目的列表视图的布局,每个项目中的控件都来自不同的布局文件。如何在不影响其他列表视图项目中的控件的情况下单击列表视图项目中的单个控件?
包含列表视图布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8AE9F"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/aboutComposer"
android:layout_width="wrap_content"
android:layout_height="45sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal"
android:text="@string/msv_songs"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCartTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="@string/cart_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvCartTotal"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:src="@drawable/icon_pay_now" />
<ListView
android:id="@+id/lstMSVSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:focusable="false" >
</ListView>
</RelativeLayout>
布局文件是控制用于每个列表视图项的源
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvViewSample"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgFilmPoster"
android:layout_width="75sp"
android:layout_height="75sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/tvSongName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvFilmName"
android:layout_below="@+id/tvFilmName"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvYearReleased"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvSongName"
android:layout_below="@+id/tvSongName"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvYearReleased"
android:layout_below="@+id/tvYearReleased"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imgFilmPoster"
android:focusable="true"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imgAddCart"
android:layout_width="35sp"
android:layout_height="35sp"
android:contentDescription="@string/add_to_cart"
android:layout_alignTop="@+id/imgView"
android:layout_toRightOf="@+id/imgView"
android:src="@drawable/add_to_cart"
android:tag = "@string/add_to_cart" />
<ImageView
android:id="@+id/imgView"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_below="@+id/imgFilmPoster"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/imgPlay"
android:src="@drawable/view_icon" />
<ImageView
android:id="@+id/imgPlay"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_alignLeft="@+id/tvPrice"
android:layout_alignTop="@+id/imgView"
android:contentDescription="@string/play_mp3"
android:src="@drawable/play" />
</RelativeLayout>
为个别列表项和处理图像视图加载数据
Java代码点击事件
private List<Song> msvSongs = new ArrayList<Song>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msv_music_sheets);
populateSongsList();
populateSongsListView();
}
private void populateSongsList() {
msvSongs.add(new Song(R.drawable.msv_ayirathil_oruvan,"Ayirathil Oruvan","Adho Andha Paravai Pola","1965",150,R.raw.msv_adho_andha_paravai_pola_ao));
msvSongs.add(new Song(R.drawable.msv_karuppu_panam,"Karuppu Panam","Aadavaralaam","1964",150,R.raw.msv_aadavaralaam_kp));
msvSongs.add(new Song(R.drawable.msv_paalum_pazhamum,"Paalum Paazhamum","Aalayamaniyin Oosai","1961",150,R.raw.msv_aalayamaniyin_oosai_pp));
msvSongs.add(new Song(R.drawable.msv_paava_mannippu,"Paava Mannipu","Athaan Ennathan","1961",150,R.raw.msv_athaan_ennathaan_pm));
msvSongs.add(new Song(R.drawable.msv_periya_idhuthu_pen,"Periya Idhuthu Pen","Andru Vandhadhadhum Adhey Nila","1963",175,R.raw.msv_andru_vandhadhum_adhey_nila_pip));
msvSongs.add(new Song(R.drawable.msv_puthiya_paravai,"Pudhiya Paravai","Enge Nimmadhi","1964",250,R.raw.msv_enge_nimmadhi_pparavai));
//msvSongs.add(new Song(R.drawable.msv_server_sundaram,"Server Sundaram","Avalukku Enna Azhagiya Mugam","1964",150,R.raw.msv_avalukkenna_azhagiamugham_ss));
}
private void populateSongsListView() {
ArrayAdapter<Song> msvSongs = new MSVSongsAdapter();
ListView songsList = (ListView) findViewById(R.id.lstMSVSongs);
songsList.setAdapter(msvSongs);
}
private class MSVSongsAdapter extends ArrayAdapter<Song>{
public MSVSongsAdapter(){
super(MsvSongs.this, R.layout.songs_view, msvSongs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.songs_view, parent, false);
}
for(int i=0;i<msvSongs.size();i++){
//Find a song to display
final Song currentSong = msvSongs.get(position);
//Fill the FilmPoster View
ImageView filmPoster = (ImageView)itemView.findViewById(R.id.imgFilmPoster);
filmPoster.setImageResource(currentSong.getFilmImageID());
//Fill the FilmName View
TextView filmName = (TextView)itemView.findViewById(R.id.tvFilmName);
filmName.setText(currentSong.getMovieName());
//Fill the SongName View
TextView songName = (TextView)itemView.findViewById(R.id.tvSongName);
songName.setText(currentSong.getSongName());
//Fill the YearReleased View
TextView yearReleased = (TextView)itemView.findViewById(R.id.tvYearReleased);
yearReleased.setText(currentSong.getYearReleased());
//Fill the Price View
TextView songPrice = (TextView)itemView.findViewById(R.id.tvPrice);
songPrice.setText("Rs. "+currentSong.getSongPrice());
//Fill the Play MP3 View
final ImageView viewMP3 = (ImageView)itemView.findViewById(R.id.imgPlay);
viewMP3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String contentDesc = (String) viewMP3.getContentDescription();
if(contentDesc.contains("Play")){
if (mp3Player != null && mp3Player.isPlaying()){
Toast.makeText(MsvSongs.this, "Stop the previous song", Toast.LENGTH_SHORT).show();
}else{
mp3Player=MediaPlayer.create(MsvSongs.this, currentSong.getmp3File());
mp3Player.start();
viewMP3.setImageResource(drawable.stop);
viewMP3.setContentDescription("Stop MP3");
}
}else{
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player=null;
viewMP3.setImageResource(drawable.play);
viewMP3.setContentDescription("Play MP3");
}
}
});
当我点击某个歌曲的播放按钮时, ist视图,播放歌曲并将播放图像更改为停止图像。但它也会将播放图像更改为停止其他列表视图项目的图像。
请问您能否建议上述代码中出现什么问题?