我希望在gridview项中的onClick打开一个弹出窗口并传递相同的值。
我的模型代码:
public class Achievement {
private final int name;
private final int author;
private final int imageResource;
private boolean isFavorite = false;
private final String imageUrl;
public Achievement(int name, int author, int imageResource, String imageUrl) {
this.name = name;
this.author = author;
this.imageResource = imageResource;
this.imageUrl = imageUrl;
}
public int getName() {
return name;
}
public int getAuthor() {
return author;
}
public int getImageResource() {
return imageResource;
}
public boolean getIsFavorite() {
return isFavorite;
}
public void setIsFavorite(boolean isFavorite) {
this.isFavorite = isFavorite;
}
public void toggleFavorite() {
isFavorite = !isFavorite;
}
public String getImageUrl() {
return imageUrl;
}
}
是的,这是一个成就Gridview:)
我的AchievementAdapter代码:
public class AchievementsAdapter extends BaseAdapter {
private final Context mContext;
private final Achievement[] books;
public AchievementsAdapter(Context context, Achievement[] books) {
this.mContext = context;
this.books = books;
}
@Override
public int getCount() {
return books.length;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Achievement book = books[position];
// view holder pattern
if (convertView == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.achievement_item, null);
final ImageView imageViewCoverArt = convertView.findViewById(R.id.imageview_cover_art);
final TextView nameTextView = convertView.findViewById(R.id.textview_book_name);
final TextView authorTextView = convertView.findViewById(R.id.textview_book_author);
final ImageView imageViewFavorite = convertView.findViewById(R.id.imageview_favorite);
final ViewHolder viewHolder = new ViewHolder(nameTextView, authorTextView, imageViewCoverArt, imageViewFavorite);
convertView.setTag(viewHolder);
}
final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
// viewHolder.imageViewCoverArt.setImageResource(book.getImageResource());
viewHolder.nameTextView.setText(mContext.getString(book.getName()));
viewHolder.authorTextView.setText(mContext.getString(book.getAuthor()));
Picasso.with(mContext).load(book.getImageUrl()).into(viewHolder.imageViewCoverArt);
viewHolder.imageViewCoverArt.setAlpha(0.3f);
viewHolder.imageViewFavorite.setImageResource(book.getIsFavorite() ? R.drawable.lock_open : R.drawable.lock);
viewHolder.imageViewCoverArt.setAlpha(book.getIsFavorite() ? 1f : 0.3f);
viewHolder.authorTextView.setText(book.getIsFavorite()? "Unlocked": "Locked");
return convertView;
}
private class ViewHolder {
private final TextView nameTextView;
private final TextView authorTextView;
private final ImageView imageViewCoverArt;
private final ImageView imageViewFavorite;
public ViewHolder(TextView nameTextView, TextView authorTextView, ImageView imageViewCoverArt, ImageView imageViewFavorite) {
this.nameTextView = nameTextView;
this.authorTextView = authorTextView;
this.imageViewCoverArt = imageViewCoverArt;
this.imageViewFavorite = imageViewFavorite;
}
}
}
最后是 myFragment代码(我也有弹出窗口代码):
final GridView gridView = myFragment.findViewById(R.id.gridview);
final AchievementsAdapter achievementsAdapter = new AchievementsAdapter(getActivity(), books);
gridView.setAdapter(achievementsAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Achievement achievement = (Achievement) parent.getItemAtPosition(position);
showPopup(view);
}
});
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.achievements_details, null);
ImageView close_window = popupView.findViewById(R.id.close_button);
List<Integer> forshowup = new ArrayList<>();
final PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// Example: If you have a TextView inside `popup_layout.xml`
TextView tv = popupView.findViewById(R.id.title);
tv.setText("");
// If the PopupWindow should be focusable
popupWindow.setFocusable(true);
// If you need the PopupWindow to dismiss when when touched outside
int location[] = new int[2];
// Get the View's(the one that was clicked in the Fragment) location
anchorView.getLocationOnScreen(location);
// Using location, the PopupWindow will be displayed right under anchorView
popupWindow.showAtLocation(anchorView, Gravity.CENTER,
location[0], location[0] + anchorView.getHeight());
View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
public void onClick(View v) {
popupWindow.dismiss();
}
};
close_window.setOnClickListener(cancel_button_click_listener);
}
以防万一:
private Achievement[] books = {
new Achievement(R.string.wanderer, R.string.islocked, R.drawable.none,
"https://i.imgur.com/zNzIwNH.png"),
new Achievement(R.string.savant, R.string.islocked, R.drawable.none,
"https://i.imgur.com/W3BAHyD.png"),
其他信息:
所有代码都在工作。我唯一的问题是将点击的项目的照片,文本等发送到弹出窗口。我不是一个经验丰富的程序员。