将Gridview项目中的值传递给弹出窗口

时间:2019-02-20 16:19:13

标签: android gridview android-popupwindow

我在一个片段中有一个gridview,并在单击时打开了一个弹出窗口。如何将相同的数据(图像,标题,描述)传递到弹出窗口?

GridView和PopupWindow代码(片段)

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.achievement_details, null);
        ImageView close_window = popupView.findViewById(R.id.close_button);



        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("Badge Title");

        // 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);

    }

成就模型

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;
    }

我尝试了Intent(可能无法正常工作)。我尝试了多余的东西,但是由于我不太擅长,所以没有管理它。我认为的一种方法是使弹出窗口成为新活动,然后按预期传递数据,但是如果有一种方法(我希望有),我宁愿按原样传递片段中的值。

如果需要更多代码,请通知我。

0 个答案:

没有答案