gpt4 book ai didi

android - 如何在 recyclerview 的项目上膨胀 xml 文件?

转载 作者:行者123 更新时间:2023-11-30 00:28:02 26 4
gpt4 key购买 nike

我正在使用一个库将鼠标悬停在 recylerview 的项目上,但是项目的监听器不工作,它以前工作过,而且问题是——在另一个膨胀的 xml 文件上膨胀新的 xml 文件的正确方法是什么?

提前致谢。

public class LessonVideoAdapter extends RecyclerView.Adapter<LessonVideoAdapter.LessonDataHolder> {

private List<VideoList> slipList;


public LessonVideoAdapter() {
slipList = new ArrayList<>();
slipList.add(new VideoList("Chapter 1", R.drawable.demo_one));
slipList.add(new VideoList("Chapter 2", R.drawable.demo_two));
slipList.add(new VideoList("Chapter 3", R.drawable.demo_one));
slipList.add(new VideoList("Chapter 4", R.drawable.demo_two));


}

@Override
public LessonDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {

Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.abc_card_view_video, parent, false);

return new LessonDataHolder(view);

}

@Override
public void onBindViewHolder(LessonDataHolder holder, int position) {

VideoList s = slipList.get(position);
holder.bindSlip(s);

}

@Override
public int getItemCount() {
return slipList.size();
}


public class LessonDataHolder extends RecyclerView.ViewHolder {

public BlurLayout samplelayout;
private TextView nameOfSlip;
private ImageView slipImage;
private Context context;


public LessonDataHolder(View itemView) {
super(itemView);

context = itemView.getContext();
nameOfSlip = (TextView) itemView.findViewById(R.id.demoText);
slipImage = (ImageView) itemView.findViewById(R.id.imageViewDemoVideo);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
{
Log.d("ashu", " clicked " + getAdapterPosition());

}
}
});
}

public void bindSlip(VideoList videoList) {
nameOfSlip.setText(videoList.stringSlipName);
slipImage.setImageResource(videoList.stringImage);
}
}


public class VideoList {
public String stringSlipName;
public int stringImage;

public VideoList(String stringSlipName, int stringImage) {
this.stringImage = stringImage;
this.stringSlipName = stringSlipName;
}

}
}

最佳答案

在没有看到你的 xml 的情况下,根据我的理解,我建议试试这个 -

onCreateViewHolder() 替换为这个

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.abc_card_view_video, parent, false);
return new LessonDataHolder(v);

在 itemView.setOnClickListener() 之前放这个 -

itemView.setClickable(true); 

我不确定膨胀 xml 的正确方法,但我使用下面的代码在我需要的地方膨胀 xml,除了 recyclerView 适配器 onCreateViewHolder() 方法回调 -

View view = getLayoutInflater(null).inflate(R.layout.scanner_manual_entry, null);

如果这解决了您的问题,请将此帖子标记为答案。
希望这有帮助

关于android - 如何在 recyclerview 的项目上膨胀 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007289/

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