如何在 RecyclerView 的选中项上实现迷你均衡器?
How to implement mini equalizer on selected item of RecyclerView?
我有一个recyclerview,它的listitem设计如下
- 线性布局(水平)
- ImageView
- TextView
- 图像按钮(播放按钮)
- 均衡器视图(不可见)
现在当我点击播放按钮时,它应该隐藏并显示均衡器显示,如果我滚动列表,均衡器应该只停留在选定的项目上。
但是,我遇到了以 10 个项目为间隔显示 EqualizerView 的问题。也就是说,如果我点击第 3 项,那么均衡器将显示在第 3、13 等处..
我正在使用这个 https://github.com/claucookie/mini-equalizer-library-android 库来实现迷你均衡器。
public class StationViewAdapter extends RecyclerView.Adapter<StationViewAdapter.StationViewHolder> {
List<Station> statons;
final private ListItemClickListener mOnClickListener;
public StationViewAdapter (List<Station> s, ListItemClickListener l)
{
statons = s;
mOnClickListener = l;
}
@NonNull
@Override
public StationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new StationViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final StationViewHolder holder, int position) {
Station station = statons.get(position);
if(!station.getArtwork().isEmpty() && station.getArtwork() != null)
{
try{
Picasso.get().load(Uri.parse(station.getArtwork()))
// .placeholder(R.mipmap.ic_launcher)
.into(holder.stationArtwork);
} catch (Exception e) {
e.printStackTrace();
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
}
}
else
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
holder.stationName.setText(station.getName());
}
@Override
public int getItemCount() {
return statons.size();
}
public class StationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView stationArtwork;
TextView stationName;
ImageButton stationPlayButton;
EqualizerView equalizerView;
int selectedItem;
public StationViewHolder(@NonNull View itemView) {
super(itemView);
stationArtwork = itemView.findViewById(R.id.favicon);
stationName = itemView.findViewById(R.id.text);
stationPlayButton = itemView.findViewById(R.id.play_button);
equalizerView = itemView.findViewById(R.id.equalizer_view);
itemView.setOnClickListener(this);
stationPlayButton.setOnClickListener(this);
selectedItem = getAdapterPosition();
}
@Override
public void onClick(View v) {
int clickPosition = getAdapterPosition();
Log.d(TAG, "onClick: "+getAdapterPosition());
Log.d(TAG, "onClick: "+getLayoutPosition());
Log.d(TAG, "onClick: "+getItemId());
if(v == itemView) {
mOnClickListener.onListItemClick(clickPosition);
}else if(v == stationPlayButton)
{
mOnClickListener.onListItemPlay(clickPosition,stationPlayButton,equalizerView);
}
}
}
public interface ListItemClickListener {
void onListItemClick(int clickedItemIndex);
void onListItemPlay(int clickItemIndex,ImageButton i,EqualizerView eq);
}
}
这些是 Main 中的 ClickListeners Activity
@Override
public void onListItemClick(int clickedItemIndex) {
if(mToast !=null)
{
mToast.cancel();
}
mToast = Toast.makeText(this,"Main Activity item clicked at position"+clickedItemIndex,Toast.LENGTH_SHORT);
mToast.show();
}
@Override
public void onListItemPlay(int clickItemIndex,ImageButton i , EqualizerView eq) {
i.setVisibility(View.GONE);
eq.setVisibility(View.VISIBLE);
eq.animateBars();
}
RecyclerView 重用 ViewHolder 实例。您需要使 onBindViewHolder 中的 ViewHolder 状态无效。在您的情况下,您至少应该隐藏 EqulizerView:
@Override
public void onBindViewHolder(@NonNull final StationViewHolder holder, int position) {
equalizerView.setVisibility(View.INVISIBLE);
....
}
此外,您应该保存正在播放的歌曲,以便稍后初始化 ViewHolder 状态。
更多信息位于:https://developer.android.com/guide/topics/ui/layout/recyclerview#structure
我有一个recyclerview,它的listitem设计如下
- 线性布局(水平)
- ImageView
- TextView
- 图像按钮(播放按钮)
- 均衡器视图(不可见)
现在当我点击播放按钮时,它应该隐藏并显示均衡器显示,如果我滚动列表,均衡器应该只停留在选定的项目上。
但是,我遇到了以 10 个项目为间隔显示 EqualizerView 的问题。也就是说,如果我点击第 3 项,那么均衡器将显示在第 3、13 等处..
我正在使用这个 https://github.com/claucookie/mini-equalizer-library-android 库来实现迷你均衡器。
public class StationViewAdapter extends RecyclerView.Adapter<StationViewAdapter.StationViewHolder> {
List<Station> statons;
final private ListItemClickListener mOnClickListener;
public StationViewAdapter (List<Station> s, ListItemClickListener l)
{
statons = s;
mOnClickListener = l;
}
@NonNull
@Override
public StationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new StationViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final StationViewHolder holder, int position) {
Station station = statons.get(position);
if(!station.getArtwork().isEmpty() && station.getArtwork() != null)
{
try{
Picasso.get().load(Uri.parse(station.getArtwork()))
// .placeholder(R.mipmap.ic_launcher)
.into(holder.stationArtwork);
} catch (Exception e) {
e.printStackTrace();
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
}
}
else
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
holder.stationName.setText(station.getName());
}
@Override
public int getItemCount() {
return statons.size();
}
public class StationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView stationArtwork;
TextView stationName;
ImageButton stationPlayButton;
EqualizerView equalizerView;
int selectedItem;
public StationViewHolder(@NonNull View itemView) {
super(itemView);
stationArtwork = itemView.findViewById(R.id.favicon);
stationName = itemView.findViewById(R.id.text);
stationPlayButton = itemView.findViewById(R.id.play_button);
equalizerView = itemView.findViewById(R.id.equalizer_view);
itemView.setOnClickListener(this);
stationPlayButton.setOnClickListener(this);
selectedItem = getAdapterPosition();
}
@Override
public void onClick(View v) {
int clickPosition = getAdapterPosition();
Log.d(TAG, "onClick: "+getAdapterPosition());
Log.d(TAG, "onClick: "+getLayoutPosition());
Log.d(TAG, "onClick: "+getItemId());
if(v == itemView) {
mOnClickListener.onListItemClick(clickPosition);
}else if(v == stationPlayButton)
{
mOnClickListener.onListItemPlay(clickPosition,stationPlayButton,equalizerView);
}
}
}
public interface ListItemClickListener {
void onListItemClick(int clickedItemIndex);
void onListItemPlay(int clickItemIndex,ImageButton i,EqualizerView eq);
}
}
这些是 Main 中的 ClickListeners Activity
@Override
public void onListItemClick(int clickedItemIndex) {
if(mToast !=null)
{
mToast.cancel();
}
mToast = Toast.makeText(this,"Main Activity item clicked at position"+clickedItemIndex,Toast.LENGTH_SHORT);
mToast.show();
}
@Override
public void onListItemPlay(int clickItemIndex,ImageButton i , EqualizerView eq) {
i.setVisibility(View.GONE);
eq.setVisibility(View.VISIBLE);
eq.animateBars();
}
RecyclerView 重用 ViewHolder 实例。您需要使 onBindViewHolder 中的 ViewHolder 状态无效。在您的情况下,您至少应该隐藏 EqulizerView:
@Override
public void onBindViewHolder(@NonNull final StationViewHolder holder, int position) {
equalizerView.setVisibility(View.INVISIBLE);
....
}
此外,您应该保存正在播放的歌曲,以便稍后初始化 ViewHolder 状态。
更多信息位于:https://developer.android.com/guide/topics/ui/layout/recyclerview#structure