How to implement multiple buttons on Onitemclicklistener function interface? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-24 19:05:01

问题


I am not understanding here how to use this interface OnItemClickListener for multiple buttons

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
            //....................................................
        }
    }
}

回答1:


You don't need to define multiple interfaces for handling the multiple buttons. You can use only one method or you can add another method inside the listener interface to handle both the click. In my perspective, you should add another method to separate the handling. Something like this:

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
  }

  ...

}


来源:https://stackoverflow.com/questions/46983161/how-to-implement-multiple-buttons-on-onitemclicklistener-function-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!