I want to implement OnClickListener
on my songs RecyclerView
. whenever i tap on any item, OnClick event is fired, but it gets fired on more than one item. but it should be fired for the item i have tapped. It also gets fired on random rows of RecyclerView
. for example when i click on first row of RecyclerView
, OnClick event is fired on random positions, say on 7th, 15th, 28th....112th many more. I think it gets recycled so this happens.
At first it was not getting fired on random positions. for example if click on 1st position onclick event got fired on 1st position and also on 12th, 21st, 31st, i.e. on every 12th position. it was periodic but now it just random.
i have tried many methods like:
http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
https://stackoverflow.com/a/24746411/4754718
I have tried many other methods also but sorry I cant provide link because I need more than 10 reputation for that, sorry.
Following is my viewholder class:
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.TextView;
/**
* Created by anunay on 7/11/2016.
*/
public class SongsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView songName;
public TextView artistName;
public TextView songDuration;
public ImageButton expandChild;
public Button playNow;
public Button playNext;
public Button addToQueue;
public Button addToPlayList;
public Button editMetadata;
public Button delete;
public static HorizontalScrollView horizontal_scroll;
public SongsViewHolder(final View itemView) {
super(itemView);
songName = (TextView) itemView.findViewById(R.id.songNameText);
artistName = (TextView) itemView.findViewById(R.id.artistNameText);
songDuration = (TextView) itemView.findViewById(R.id.songDuration);
expandChild = (ImageButton) itemView.findViewById(R.id.expandCollapseBtn);
playNow = (Button) itemView.findViewById(R.id.playNowBtn);
playNext = (Button) itemView.findViewById(R.id.playNextBtn);
addToQueue = (Button) itemView.findViewById(R.id.addToQueueBtn);
addToPlayList = (Button) itemView.findViewById(R.id.addToPlaylistBtn);
editMetadata = (Button) itemView.findViewById(R.id.editMetaDataBtn);
delete = (Button) itemView.findViewById(R.id.deleteBtn);
horizontal_scroll = (HorizontalScrollView) itemView.findViewById(R.id.childSection);
expandChild.setOnClickListener(this);
}
@Override
public void onClick(View view) {
horizontal_scroll.setVisibility(View.VISIBLE);
}
}
and if you like to see , following is my adapter class:
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.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by anunay on 7/11/2016.
*/
public class SongsAdapter extends RecyclerView.Adapter<SongsViewHolder> {
// private ArrayList<Song> mSongsArrayList;
private final List<Song> mSongsArrayList;
private Context mContext;
public SongsAdapter(Context context, ArrayList<Song> songsArrayList) {
mSongsArrayList = songsArrayList;
mContext = context;
}
private Context getContext() {
return mContext;
}
@Override
public SongsViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View songsView = inflater.inflate(R.layout.songs_item, parent, false);
SongsViewHolder songsViewHolder = new SongsViewHolder(songsView);
return songsViewHolder;
}
@Override
public void onBindViewHolder(final SongsViewHolder holder, int position) {
Song currSong = mSongsArrayList.get(position);
TextView songNameText = holder.songName;
songNameText.setText(currSong.getTitle());
TextView artistNameText = holder.artistName;
artistNameText.setText(currSong.getArtist());
TextView songDurationText = holder.songDuration;
songDurationText.setText(currSong.getDurationString());
}
@Override
public int getItemCount() {
return mSongsArrayList.size();
}
}
if i provide a toast in onclick method with position instead of expanding the recyclerview it works perfectly, its neither generated with random position nor periodically.
expandChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// horizontal_scroll.setVisibility(View.VISIBLE);
Toast.makeText(view.getContext(), "clicked at position : "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});