gpt4 book ai didi

java - 如何在Onitemclicklistener函数接口(interface)上实现多个按钮?

转载 作者:行者123 更新时间:2023-12-02 12:04:42 24 4
gpt4 key购买 nike

我在这里不明白如何使用这个界面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
//....................................................
}
}
}

最佳答案

您不需要定义多个接口(interface)来处理多个按钮。您可以仅使用一种方法,也可以在监听器接口(interface)内添加另一种方法来处理这两种单击。在我看来,您应该添加另一种方法来分离处理。像这样的事情:

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
}

...

}

关于java - 如何在Onitemclicklistener函数接口(interface)上实现多个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46983161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com