【发布时间】:2019-07-14 14:31:15
【问题描述】:
我已经寻找了三天的解决方案。 如果大家有idea请帮帮我,我什至觉得实现起来其实没有那么难,但如果你能这样说,这不是我的分支。
在 RecyclerView 中包含一个包含多个项目的列表,每个项目都有一个标题(TextView)和一个封面图像(ImageView)。
这些数据是在适配器中设置的,在 ViewHolder 中,更具体地说是在 OnBind 函数中。
那么我的问题在哪里? 我创建了一个弹出窗口,除了几个按钮之外,它还包含一个占位符 ImageView 和一个占位符 TextView。 我似乎找不到将单击项目的数据放在占位符内的列表中的方法。 我认为它类似于 OnBind 方法,但它不起作用。
这是适配器(如果需要 GameItem 的代码,我很乐意发布):
import java.util.ArrayList;
public class GameViewAdapter extends RecyclerView.Adapter<GameViewAdapter.GameViewHolder> {
private ArrayList<GameItem> mGameList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onGameClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener =listener;
}
public static class GameViewHolder extends RecyclerView.ViewHolder{
public ImageView Cover;
public TextView Title;
public TextView Description;
public PopupWindow popupWindow;
public ImageView popUpImage;
public TextView PopUpTitle;
public EditText customAmount;
public Button add;
private Button addcustom;
private Button exit;
public GameViewHolder(final View itemView, final OnItemClickListener listener) {
super(itemView);
add = itemView.findViewById(R.id.addaverage);
addcustom = itemView.findViewById(R.id.addcustom);
popUpImage = itemView.findViewById(R.id.popupimg);
PopUpTitle = itemView.findViewById(R.id.popuptitle);
customAmount = itemView.findViewById(R.id.gameamount);
Cover = itemView.findViewById(R.id.GameCover);
Title = itemView.findViewById(R.id.GameTitle);
Description = itemView.findViewById(R.id.GameAmount);
exit = itemView.findViewById(R.id.exit);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopUp();
}
});
}
public void showPopUp() {
final View popupView = LayoutInflater.from(itemView.getContext()).inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
exit = popupView.findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}
public GameViewAdapter(ArrayList<GameItem> gameList){
mGameList = gameList;
}
@Override
public GameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Context context = viewGroup.getContext();
View v = LayoutInflater.from(context).inflate(R.layout.game_entry, viewGroup, false);
GameViewHolder GVH = new GameViewHolder(v, mListener);
return GVH;
}
@Override
public void onBindViewHolder(@NonNull GameViewHolder gameViewHolder, int position){
GameItem currentItem = mGameList.get(position);
Glide.with(gameViewHolder.Cover).load(currentItem.getCover()).into(gameViewHolder.Cover);
gameViewHolder.Title.setText(currentItem.getTitle());
gameViewHolder.Description.setText(currentItem.getDescription());
}
@Override
public int getItemCount() {
return mGameList.size();
}
}
【问题讨论】:
-
我似乎无法理解您的问题到底是什么... 绑定后图片和文字不显示?你能解释一下吗?请
-
在
OnBind方法中,Image和Text通过Glide和gameViewHolder.Title.setText(currentItem.getTitle());正确关联到Items。现在,我创建了一个 PopUp,它应该从 RecyclerView 中单击的项目中获取图像和文本 -
我试图通过
getAdapterPosition或其他方式获取单个项目,但我失败了,并且找不到使用方法getCover的方法,该方法在我的GameItem类中的@987654328 中定义@
标签: java android arrays image popupwindow