2014-11-21 78 views
1

我有以下情況:如何創建虛擬視圖錨彈出窗口的Android

private void startConnection() { 
    HashMap<String,String> hm = new HashMap<String,String>(); 
    hm.put("email","[email protected]"); 
    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); 
    } 
} 

一切正常,但是當凌空請求拋出一個錯誤,並使用工具提示類顯示錯誤:

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

我得到這個異常:

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) 

通常我通過這樣的:

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

的圖,該錨彈出窗口和它的作品,但在這種特殊情況下,活動呼叫提示類,沒有一個動作條(它是唯一一個,所有其他的活動確實有自定義動作條)。

我在這裏問是否有一種方法使用虛擬,程序創建的視圖在這種情況下使用。

也許我做錯了,你有一個很好的建議:)

在此先感謝

回答

2

經過一番嘗試,我終於來到了一個解決方案:我選擇用兩種不同的方法,一個對於沒有操作欄與操作欄,以及一個用於活動活動:

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

顯然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" /> 

我認爲這兩種方法都可以用一種方法解決,但到目前爲止,我可以依靠我的「雙重方法」解決方案來滿足我的需求。希望它可以幫助別人。仍然保持這樣的事實,我將不勝感激關於如何處理這一問題的更好建議。