将片段添加到android中的弹出窗口

时间:2014-11-14 07:54:52

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

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

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

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

1 个答案:

答案 0 :(得分:-2)

Copied From HERE

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

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

}

评论应该很好地解释这一点。 anchorView是来自v的{​​{1}}。