如何单击列表视图项中的各个控件而不影响其他列表视图项中的控件?
Posted
技术标签:
【中文标题】如何单击列表视图项中的各个控件而不影响其他列表视图项中的控件?【英文标题】:How do I click individual controls within a list view item without affecting the controls in other list view item? 【发布时间】:2014-11-07 11:47:06 【问题描述】:我正在为我们的音乐学校开发一个安卓应用程序。我设计了一个包含 6 个项目的列表视图布局,每个项目中的控件来自不同的布局文件。
包含列表视图的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#F8AE9F"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/aboutComposer"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_ >
<ImageView
android:id="@+id/imgFilmPoster"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/tvSongName"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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");
);
当我在列表视图中单击歌曲的播放按钮时,它会播放歌曲并将播放图像更改为停止图像。但它也会将播放图像更改为其他列表视图项的停止图像。
您能否请教一下上面的代码有什么问题?
【问题讨论】:
【参考方案1】:试试这个方法,希望能帮助你解决问题。
private class MSVSongsAdapter extends BaseAdapter
private Context context;
private ArrayList<Song> songList;
private MediaPlayer mp3Player;
public MSVSongsAdapter(Context context, ArrayList<Song> songList)
this.context = context;
this.songList = songList;
@Override
public int getCount()
return songList.size();
@Override
public Object getItem(int position)
return songList.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
ViewHolder holder;
if(convertView==null)
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.songs_view,null);
holder.filmPoster = (ImageView) convertView.findViewById(R.id.imgFilmPoster);
holder.viewMP3 = (ImageView) convertView.findViewById(R.id.viewMP3);
holder.filmName = (TextView) convertView.findViewById(R.id.filmName);
holder.songName = (TextView) convertView.findViewById(R.id.songName);
holder.yearReleased = (TextView) convertView.findViewById(R.id.yearReleased);
holder.songPrice = (TextView) convertView.findViewById(R.id.songPrice);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
holder.filmPoster.setImageResource(songList.get(position).getFilmImageID());
holder.viewMP3.setImageResource(songList.get(position).getFilmImageID());
holder.filmName.setText(songList.get(position).getMovieName());
holder.songName.setText(songList.get(position).getSongName());
holder.yearReleased.setText(songList.get(position).getYearReleased());
holder.songPrice.setText("Rs. " + songList.get(position).getSongPrice());
holder.viewMP3.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String contentDesc = (String) songList.get(position).getContentDescription();
if (contentDesc.contains("Play"))
if (mp3Player != null && mp3Player.isPlaying())
Toast.makeText(context, "Stop the previous song", Toast.LENGTH_SHORT).show();
else
mp3Player = MediaPlayer.create(context, songList.get(position).getmp3File());
mp3Player.start();
((ImageView)v).setImageResource(R.drawable.stop);
((ImageView)v).setContentDescription("Stop MP3");
else
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player = null;
((ImageView)v).setImageResource(R.drawable.play);
((ImageView)v).setContentDescription("Play MP3");
);
return null;
static class ViewHolder
ImageView filmPoster;
ImageView viewMP3;
TextView filmName;
TextView songName;
TextView yearReleased;
TextView songPrice;
【讨论】:
以上是关于如何单击列表视图项中的各个控件而不影响其他列表视图项中的控件?的主要内容,如果未能解决你的问题,请参考以下文章