简体   繁体   English

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

[英]add fragment to popup window in android

My problem is that i want to add fragment to my pop up window to display some information when a list is clicked. 我的问题是我想在单击列表时将片段添加到弹出窗口中以显示一些信息。 My list contains custom adapter. 我的列表包含自定义适配器。 below is what i want to do :- 以下是我想做的事:-

When list item is clicked, i get that model from adapterview and pass it to JobSearchModel. 当单击列表项时,我从adapterview获得该模型并将其传递给JobSearchModel。 Now JobSearchModel contains the information that i want to display on JobDescription fragement. 现在JobSearchModel包含我要在JobDescription片段上显示的信息。 But i don't know how to add fragement to pop up window. 但我不知道如何添加片段以弹出窗口。

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

Copied From HERE 从这里复制

The following should work perfect in accordance with your specification. 以下内容应根据您的规范完美地工作。 Call this method from inside onClick(View v) of OnClickListener assigned to the View: 从分配给View的OnClickListener onClick(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());

}

The comments should explain this well enough. 评论应该足够好地解释这一点。 anchorView is the v from onClick(View v) . anchorViewvonClick(View v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM