gpt4 book ai didi

java - 显示后如何检测每个 RecyclerView item

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:09:12 24 4
gpt4 key购买 nike

我想检测我的 RecylerView 中的每个项目显示给用户后。

基本上,我试图在每个项目加载到屏幕上后播放声音。

但是我无法检测到每个项目何时加载到屏幕上!有没有我必须调用的方法来检测呈现的每个项目

E.g 1st RecyclerView item displayed -> play sound
2st RecyclerView item displayed -> play sound...

我的 Adapter 类看起来像这样 -

public class AdapterListAnimation extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Multiples> items = new ArrayList<>();

private Context ctx;
private OnItemClickListener mOnItemClickListener;
private int animation_type = 0;
.........
.........

我正在从 onCreated() 方法调用此 initComponent() 方法。您能否就我应该如何实现上述目标提出建议?

private void initComponent() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
items = DataGenerator.getPeopleData(this,of,value);
setAdapter();

/* MediaPlayer mp=MediaPlayer.create(this, R.raw.sword);
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(this, R.raw.sword);
} mp.start();*/
}

private void setAdapter() {
// Set data and list adapter
mAdapter = new AdapterListAnimation(this, items, animation_type);
recyclerView.setAdapter(mAdapter);

// on item list clicked
mAdapter.setOnItemClickListener(new AdapterListAnimation.OnItemClickListener() {
@Override
public void onItemClick(View view, com.math.multiplication.model.Multiples obj, int position) {
Snackbar.make(parent_view, "Item " + obj.first + " clicked", Snackbar.LENGTH_SHORT).show();
}
});
}

最佳答案

您需要覆盖 onViewAttachedToWindowonViewDetachedFromWindow。但是为了检测 holer 类型,你需要像这样的 getItemViewType():

public class PostAdapter extends RecyclerView.Adapter {

@Override
public int getItemViewType(int position) {
switch (types.get(position)){
case 1:
return 1;
case 2:
return 2;
default:
return position;


}
}
@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
if (holder.getItemViewType() == 1){
//play song

}

}

@Override
public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if (holder.getItemViewType() == 1){
//pause song

}
}

关于java - 显示后如何检测每个 RecyclerView item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443865/

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