【问题标题】:add fragment to popup window in android将片段添加到android中的弹出窗口
【发布时间】:2014-11-14 07:54:51
【问题描述】:

我的问题是我想在弹出窗口中添加片段以在单击列表时显示一些信息。我的列表包含自定义适配器。以下是我想做的:-

单击列表项时,我从适配器视图获取该模型并将其传递给 JobSearchModel。现在 JobSearchModel 包含我想在 JobDescription 片段上显示的信息。但我不知道如何在弹出窗口中添加片段。

searchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JobSearchModel jobs = (JobSearchModel) adapterView.getItemAtPosition(i);

                JobDescription jobDescription = new JobDescription();

                Bundle args = new Bundle();
                args.putSerializable("jobs", jobs);

                jobDescription.setArguments(args);

                popupWindow.showAtLocation(jobDescription, Gravity.CENTER, 0, 0);
            }
        });

【问题讨论】:

  • 嘿,即使我也遇到了这个问题。你有没有找到解决方案?

标签: android android-layout android-activity android-fragments


【解决方案1】:

Copied From HERE

以下内容应符合您的规范。从分配给视图的OnClickListeneronClick(View v) 内部调用此方法:

public void showPopup(View anchorView) {

    View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

    PopupWindow popupWindow = new PopupWindow(popupView, 
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // Example: If you have a TextView inside `popup_layout.xml`    
    TextView tv = (TextView) popupView.findViewById(R.id.tv);

    tv.setText(....);

    // Initialize more widgets from `popup_layout.xml`
    ....
    ....

    // If the PopupWindow should be focusable
    popupWindow.setFocusable(true);

    // If you need the PopupWindow to dismiss when when touched outside 
    popupWindow.setBackgroundDrawable(new ColorDrawable());

    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.NO_GRAVITY, 
                                     location[0], location[1] + anchorView.getHeight());

}

cmets 应该很好地解释了这一点。 anchorView 是来自onClick(View v)v

【讨论】:

  • 这没有回答关于在弹出窗口中添加片段的问题。
  • 来自帮助页面:“每当您遇到一个非常草率、不费吹灰之力的帖子,或者一个明显不正确且可能非常不正确的答案时,请使用您的反对票。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
相关资源
最近更新 更多