2016-12-01 46 views
0

我想顯示一個ListView的內容在PopupWindow中隨着列表填充而增長。它工作正常,除了元素隨着列表的增長而移動,直到它停止添加新元素。有沒有辦法讓窗口的位置保持固定,因爲它在屏幕上向下增長?我是Android編程的新手,所以任何輸入值得讚賞。謝謝! 。PopupWindow當它是孩子ListView增長時移動

private void initView(View v) { 

    LayoutInflater inflater = (LayoutInflater) 
    getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

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

    DisplayMetrics displaymetrics = new DisplayMetrics(); 

    getActivity().getWindowManager().getDefaultDisplay(). 
    getMetrics(displaymetrics); 

    int width = (int) ((int)displaymetrics.widthPixels * 0.9); 

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

    m = (MainActivity) getActivity(); 

    m.layout_main.getForeground().setAlpha(220); 

    btnScan = (Button) popupView.findViewById(R.id.scan); 

    btncancel = (Button) popupView.findViewById(R.id.close); 

    popupWindow.setOutsideTouchable(false); 

    popupWindow.setFocusable(true); 


    int loc_int[] = new int[2]; 
    if (v == null) 

    try 
    { 
     v.getLocationOnScreen(loc_int); 
    } catch (NullPointerException npe) 
    { 
     //when the view doesn't exist on screen anymore. 

    } 

    Rect location = new Rect(); 
    location.left = loc_int[0]; 
    location.top = loc_int[1] ; 
    location.right = location.left + v.getWidth()/2; 
    location.bottom = location.top - view2.getHeight()/2 ; 


    popupWindow.showAtLocation(view2, Gravity.CENTER, 
           view2.getWidth()/2, location.top); 

    popupWindow.showAsDropDown(view2); 

    listDevicesFound = (ListView) popupView.findViewById(R.id.devicelist); 

    btAdapter = BluetoothAdapter.getDefaultAdapter(); 

    adtDevice = new ArrayAdapter<String>(getActivity(),R.layout.list_text, 
       lsDevice);   

    listDevicesFound.setAdapter(adtDevice); 

    listDevicesFound.setOnItemClickListener(new onIntemClick()); 



} 

回答

0

我最終使用使用獲得的寬度和高度的一小部分固定窗口的寬度和高度: DisplayMetrics度量= this.getResources()getDisplayMetrics(); MyApplication.width = metrics.widthPixels; MyApplication.height = metrics.heightPixels;

隨着固定高度的PopupWindow沒有更多的轉移與列表的新增加。

相關問題