如何设置自定义列表视图中的ImageView setOnClickListner和Imageview单击时的play/pause歌曲?
How to set ImageView setOnClickListner which is in custom listview and play/pause song when Imageview clicked?
我是 android 的新手,我想在我的项目 ImageView
clicked.According 时播放和暂停歌曲 我有一个自定义列表视图,在这个列表视图中我有两个 ImageView
。因此,当用户 select 播放时播放歌曲并 select 暂停时暂停歌曲。
注意:- 基本上我想在我的 activity class 中设置 onClickListner 而不是自定义列表视图 adapter.And 我正在添加歌曲从 res\raw
文件夹到数组列表。
这是我的适配器
public class SongAdapter extends BaseAdapter{
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat) {
this.context = activity;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind)
{
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getCount() {
return songsListDat.size();
}
@Override
public Object getItem(int position) {
return songsListDat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView)vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView)vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
return vi;
}
}
这是 activity class :- 在这个 activity 中,我想设置 onclicklistner 用于播放和暂停功能。
public class MainActivity extends ListActivity {
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this, songsList);
setListAdapter(songAdapter);
ListView lv = getListView();
}
}
这是我的歌曲模型class
public class SongModel {
private String songPath, songTitle;
public SongModel(String songPath, String songTitle) {
this.songPath = songPath;
this.songTitle = songTitle;
}
public SongModel() {
}
public void setSongPath(String songPath){
this.songPath = songPath;
}
public String getSongPath() {
return songPath;
}
public void setSongTitle(String songTitle){
this.songTitle = songTitle;
}
public String getSongTitle() {
return songTitle;
}
}
有知道的请帮帮我...
如果您想在点击 ListView 项目时将 ListView 项目的图像 src 设置为其他内容,请尝试此操作。
使用 onItemClick
方法的 view
参数更改所选行的 ImageView
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
ImageView selectedImage = (ImageView) view.findViewById(R.id.buckysImage);
selectedImage.setImageResource(R.drawable.selected);
}
首先在你的适配器中添加一个接口 class 喜欢并像这样使用它 ::
public class SongAdapter extends BaseAdapter {
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
public OnMusicListener mListener;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat, OnMusicListener mListener) {
this.context = activity;
this.mListener = mListener;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind) {
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getCount() {
return songsListDat.size();
}
@Override
public Object getItem(int position) {
return songsListDat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView) vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView) vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onPlayClicked(position);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onPauseClicked(position);
}
});
return vi;
}
public interface OnMusicListener {
void onPauseClicked(int position);
void onPlayClicked(int position);
}
}
然后从你的activityclass
songAdapter = new SongAdapter(this, songsList,this);
setListAdapter(songAdapter);
添加 "this "
后,您现在可以从接口
实现方法
从中你可以得到列表中每个 play/pause 的点击事件..
如果需要更多帮助请告知..
所以这将是你的 activity 应该喜欢这个 ::
public class MainActivity extends ListActivity implements SongAdapter.OnMusicListener{
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this,songsList,this);
setListAdapter(songAdapter);
ListView lv = getListView();
}
@Override
public void onPauseClicked(int position){
//Todo-- Add code you want to perform on Pause clicked with specific position
}
@Override
public onPlayClicked(int position){
//Todo-- Add code you want to perform on Play clicked with specific position
}
}
您只需将 onclick 侦听器传递给您的适配器并将点击侦听器设置为您的图像视图。
当您从 activity 设置适配器时,只需在将参数传递给方法时创建新的点击侦听器即可。
我是 android 的新手,我想在我的项目 ImageView
clicked.According 时播放和暂停歌曲 我有一个自定义列表视图,在这个列表视图中我有两个 ImageView
。因此,当用户 select 播放时播放歌曲并 select 暂停时暂停歌曲。
注意:- 基本上我想在我的 activity class 中设置 onClickListner 而不是自定义列表视图 adapter.And 我正在添加歌曲从 res\raw
文件夹到数组列表。
这是我的适配器
public class SongAdapter extends BaseAdapter{
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat) {
this.context = activity;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind)
{
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getCount() {
return songsListDat.size();
}
@Override
public Object getItem(int position) {
return songsListDat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView)vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView)vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
return vi;
}
}
这是 activity class :- 在这个 activity 中,我想设置 onclicklistner 用于播放和暂停功能。
public class MainActivity extends ListActivity {
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this, songsList);
setListAdapter(songAdapter);
ListView lv = getListView();
}
}
这是我的歌曲模型class
public class SongModel {
private String songPath, songTitle;
public SongModel(String songPath, String songTitle) {
this.songPath = songPath;
this.songTitle = songTitle;
}
public SongModel() {
}
public void setSongPath(String songPath){
this.songPath = songPath;
}
public String getSongPath() {
return songPath;
}
public void setSongTitle(String songTitle){
this.songTitle = songTitle;
}
public String getSongTitle() {
return songTitle;
}
}
有知道的请帮帮我...
如果您想在点击 ListView 项目时将 ListView 项目的图像 src 设置为其他内容,请尝试此操作。
使用 onItemClick
方法的 view
参数更改所选行的 ImageView
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
ImageView selectedImage = (ImageView) view.findViewById(R.id.buckysImage);
selectedImage.setImageResource(R.drawable.selected);
}
首先在你的适配器中添加一个接口 class 喜欢并像这样使用它 ::
public class SongAdapter extends BaseAdapter {
Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
public OnMusicListener mListener;
private static LayoutInflater inflater;
public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat, OnMusicListener mListener) {
this.context = activity;
this.mListener = mListener;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind) {
notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getCount() {
return songsListDat.size();
}
@Override
public Object getItem(int position) {
return songsListDat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ringtone_row_layout, null);
TextView title = (TextView) vi.findViewById(R.id.name); // title
ImageView play = (ImageView) vi.findViewById(R.id.imgPlayPause);
ImageView pause = (ImageView) vi.findViewById(R.id.imgPause);
SongModel song = new SongModel();
song = songsListDat.get(position);
title.setText(song.getSongTitle());
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onPlayClicked(position);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onPauseClicked(position);
}
});
return vi;
}
public interface OnMusicListener {
void onPauseClicked(int position);
void onPlayClicked(int position);
}
}
然后从你的activityclass
songAdapter = new SongAdapter(this, songsList,this);
setListAdapter(songAdapter);
添加 "this "
后,您现在可以从接口
从中你可以得到列表中每个 play/pause 的点击事件..
如果需要更多帮助请告知..
所以这将是你的 activity 应该喜欢这个 ::
public class MainActivity extends ListActivity implements SongAdapter.OnMusicListener{
public static final String TAG = "[MainActivity]";
Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
mp = new MediaPlayer();
SongsManager plm = new SongsManager(activity);
final ArrayList<SongModel> songsListData = songsManager.songList;
this.songsList = plm.getAllSongs();
for (int i = 0; i < songsListData.size(); i++) {
SongModel song = songsListData.get(i);
songsListData.add(song);
}
songAdapter = new SongAdapter(this,songsList,this);
setListAdapter(songAdapter);
ListView lv = getListView();
}
@Override
public void onPauseClicked(int position){
//Todo-- Add code you want to perform on Pause clicked with specific position
}
@Override
public onPlayClicked(int position){
//Todo-- Add code you want to perform on Play clicked with specific position
}
}
您只需将 onclick 侦听器传递给您的适配器并将点击侦听器设置为您的图像视图。
当您从 activity 设置适配器时,只需在将参数传递给方法时创建新的点击侦听器即可。