【发布时间】:2016-07-19 08:20:28
【问题描述】:
我想在我的歌曲RecyclerView 上实现OnClickListener。每当我点击任何项目时,都会触发 OnClick 事件,但它会在多个项目上触发。但应该为我点击的项目解雇它。它还会在RecyclerView 的随机行上触发。例如,当我单击RecyclerView 的第一行时,OnClick 事件会在随机位置触发,例如在 7 日、15 日、28 日……112 日更多。我认为它会被回收,所以会发生这种情况。
起初它并没有在随机位置上被解雇。例如,如果单击第 1 个位置 onclick 事件在第 1 个位置以及第 12、21、31 个位置触发,即在每个第 12 个位置触发。它是周期性的,但现在它只是随机的。
我尝试了很多方法,例如:
http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
https://stackoverflow.com/a/24746411/4754718
我也尝试了许多其他方法,但抱歉我无法提供链接,因为我需要超过 10 个声誉,抱歉。
下面是我的viewholder类:
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);
}
}
如果你喜欢看,下面是我的适配器类:
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();
}
}
如果我在 onclick 方法中提供带有位置的 toast,而不是扩展 recyclerview,它可以完美运行,它既不是随机位置生成的,也不是周期性生成的。
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();
}
});
【问题讨论】:
标签: android android-recyclerview onclicklistener audio-player android-music-player