我不明白如何将这个界面OnItemClickListener
用于多个按钮
package com.mtechviral.musicplayer;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongHolder> {
private ArrayList<SongInfo> _songs = new ArrayList<SongInfo>();
private Context context;
private OnItemClickListener mOnItemClickListener;
public SongAdapter(Context context, ArrayList<SongInfo> songs) {
this.context = context;
this._songs = songs;
}
//getting a error
public interface OnItemClickListener {
void onItemClick(Button b ,View view, SongInfo obj, int position);
//add another button parameter
}
public interface OnItemClickListener {
void onItemClick(Button bt ,View view, SongInfo obj, int position);
//add another button parameter
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
@Override
public SongHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View myView = LayoutInflater.from(context).inflate(R.layout.row_songs,viewGroup,false);
return new SongHolder(myView);
}
@Override
public void onBindViewHolder(final SongHolder songHolder, final int i) {
final SongInfo s = _songs.get(i);
songHolder.tvSongName.setText(_songs.get(i).getSongname());
songHolder.tvSongArtist.setText(_songs.get(i).getArtistname());
songHolder.btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(songHolder.btnAction,v, s, i);
}
}
});
songHolder.btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(songHolder.btnStop,v, s, i);
}
}
});
//write a function for pause
}
@Override
public int getItemCount() {
return _songs.size();
}
public class SongHolder extends RecyclerView.ViewHolder {
TextView tvSongName,tvSongArtist;
Button btnAction,btnStop;
//declare a new button............................................
public SongHolder(View itemView) {
super(itemView);
tvSongName = (TextView) itemView.findViewById(R.id.tvSongName);
tvSongArtist = (TextView) itemView.findViewById(R.id.tvArtistName);
btnAction = (Button) itemView.findViewById(R.id.btnAction);
btnStop = (Button) itemView.findViewById(R.id.btnStop);
//declaraion details
//....................................................
}
}
}
答案 0 :(得分:1)
您无需定义多个接口来处理多个按钮。您只能使用一种方法,或者可以在侦听器界面中添加另一种方法来处理单击。在我看来,你应该添加另一种方法来分离处理。像这样:
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongHolder> {
...
public interface OnItemClickListener {
// Only use 2 parameters because you just need them.
void onActionClicked(SongInfo obj, int position);
void onStopClicked(SongInfo obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
...
@Override
public void onBindViewHolder(final SongHolder songHolder, final int i) {
...
songHolder.btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onActionClicked(s, i);
}
}
});
songHolder.btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onStopClicked(s, i);
}
}
});
//write a function for pause
}
...
}