简体   繁体   English

如何创建虚拟视图以锚定弹出窗口Android

[英]How to create dummy view to anchor popup window android

I have the following situation: 我有以下情况:

private void startConnection() {
    HashMap<String,String> hm = new HashMap<String,String>();
    hm.put("email","email@example.com");
    String URL = "192.168.1.2/test.php";
    Db_connection db = new Db_connection(hm,URL,context);
    db.startTest();
}

public class Db_connection {

    private HashMap<String, String> params;
    private String URL;
    private ConnectionController CC;
    private ToolTip tt;
    private Context con;
    private ProgressDialog dialog;

    public Db_connection(HashMap<String,String> params,String URL,Context con) {
        this.params = params;
        this.URL = URL;
        this.con = con;
        this.dialog = new ProgressDialog(con);
        this.dialog.setCancelable(true);
        this.CC = ConnectionController.getInstance();
    }

    public void startTest() {
        JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
                new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (dialog.isShowing()) { dialog.dismiss(); }

                if (response != null && response.has("result")) {
                    try {
                        if (!response.getString("result").matches("error")) {
                            Toast.makeText(con, "Response: "+response.getString("result"), Toast.LENGTH_LONG).show();
                        }
                        else { Toast.makeText(con, response.getString("errmsg"), Toast.LENGTH_LONG).show(); return; }

                        VolleyLog.v("Response:%n %s", response.toString(4));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToolTip tt = new ToolTip(con, error.getMessage());
                tt.showNow();
                Log.e("Error: ", error.getMessage());
            }
        });
        req.setRetryPolicy(rp);
        CC.addToRequestQueue(req);
    }
}

public class ToolTip {

private Context con;
private PopupWindow pn;
private View layout;
private ImageView cancel;
private TextView mess;

public ToolTip(Context con,String htmlMsg) {
    this.con = con;
    LayoutInflater inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.tooltip,null);
    pn = new PopupWindow(layout,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,false);
    pn.setAnimationStyle(R.style.AnimationToolTip);
    cancel = (ImageView)layout.findViewById(R.id.tooltip_imageClose);
    mess = (TextView)layout.findViewById(R.id.tooltip_mainText);
        mess.setText(Html.fromHtml(htmlMsg));

        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pn.dismiss();
        }
        });
    }

    public void showNow() {
        pn.showAtLocation(/*HERE IS WHERE I DON'T KNOW WHAT TO PASS AS VIEW*/((Activity) con).getActionBar().getCustomView(), Gravity.NO_GRAVITY, 0, 180);
    }
}

Everything works but when the volley request throws an error and uses the ToolTip class to show the error: 一切正常,但是当截击请求引发错误并使用ToolTip类显示错误时:

ToolTip tt = new ToolTip(con, error.getMessage());
tt.showNow();

i get this exception: 我得到这个例外:

11-21 15:08:06.390: E/AndroidRuntime(22448): Process: com.tooltip.test, PID: 22448
11-21 15:08:06.390: E/AndroidRuntime(22448): java.lang.ClassCastException: com.tooltip.test.ConnectionController cannot be cast to android.app.Activity
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.tooltip.test.ToolTip.showNow(ToolTip.java:46)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.tooltip.test.Db_connection$2.onErrorResponse(Db_connection.java:100)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.android.volley.Request.deliverError(Request.java:577)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at android.os.Handler.handleCallback(Handler.java:733)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at android.os.Looper.loop(Looper.java:136)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at android.app.ActivityThread.main(ActivityThread.java:5139)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at java.lang.reflect.Method.invoke(Method.java:515)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-21 15:08:06.390: E/AndroidRuntime(22448):    at dalvik.system.NativeStart.main(Native Method)

Usually i pass this: 通常我通过这个:

((Activity) con).getActionBar().getCustomView()

as the view to which anchor the popup window and it works but in this particular case, the activity calling the tooltip class, doesn't have an actionbar (it's the only one, all others activity does have the custom actionbar). 作为锚定弹出窗口并对其起作用的视图,但是在这种特殊情况下,调用工具提示类的活动没有操作栏(这是唯一的,其他所有活动都具有自定义操作栏)。

I'm here to ask if there is a method to use a dummy, programmatically created view to use in this case. 我在这里问是否有一种方法可以在这种情况下使用虚拟的,以编程方式创建的视图。

Or maybe i'm doing it wrong and you have a nice suggestion :) 或者也许我做错了,你有一个很好的建议:)

Thanks in advance 提前致谢

After some try i finally came to a solution: i choose to use two different method, one for activity with action bar, and one for activity without action bar: 经过一番尝试,我终于找到了解决方案:我选择使用两种不同的方法,一种用于带操作栏的活动,一种用于不带操作栏的活动:

public void showNow() { //THIS IS FOR ACTIVITY WITH ACTION BAR
    pn.showAtLocation((Activity)con.getActionBar().getCustomView(), Gravity.NO_GRAVITY, 0, 180);
}

public void showNowNoActBar() { //THIS IS FOR ACTIVITY WITHOUT ACTION BAR
    pn.showAtLocation(con.findViewById(R.id.dummy_hidden_view), Gravity.NO_GRAVITY, 0, 180);
}

Obviously R.id.dummy_hidden_view is defined in the XML Layout of the activity without the custom action bar like this: 显然, R.id.dummy_hidden_view是在活动的XML布局中定义的,而没有这样的自定义操作栏:

<TextView
    android:id="@+id/dummy_hidden_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:visibility="gone" />

I think that both method could be solved with only one method but as of now, i can rely on my "double method" solution for what i need. 我认为这两种方法都只能用一种方法解决,但到目前为止,我可以依靠我的“双重方法”解决方案来满足需要。 Hope it helps someone else. 希望它可以帮助别人。 Still remain the fact that i will appreciate any better suggestion on how to handle this. 仍然存在这样的事实,我将对如何处理此问题提出任何更好的建议。

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

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