2014-02-22 92 views
1

我吹一個PopupWindow內的ListView,我想在彈出的行爲是這樣的:設置PopupWindow有一個最大高度

  • 包裹列表視圖時,其高度爲< X
  • 設置彈出窗口的高度= x當listiew的高度> x(列表視圖可滾動)

彈出窗口由連接到EditText的TextWatcher顯示,因爲它意在顯示搜索建議。 ListView底層的適配器由自定義加載程序管理,只要用戶在EditText中輸入了某些內容就會啓動。 我試圖在列表視圖上覆蓋onMeasure()並將測量的高度傳遞給一個偵聽器,該偵聽器調用PopupWindow.update(),但由於後者最終會調用第一個,因此會創建一個循環。 使用下面的代碼,彈出窗口將所包含的ListView視爲想要的,但高度不限於任何值。我需要一個解決方案來將高度限制在最大值,比如說300dp。

etFiltro.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      /... 
     } 
     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      Log.i("loader","text changed"); 
      filtro = String.valueOf(charSequence); 
      getSupportLoaderManager().restartLoader(0,null,loaderCallbacks); 
      if (popupWindow.isShowing()) popupWindow.dismiss(); 
     } 
     @Override 
     public void afterTextChanged(Editable editable) {    
      popupWindow.showAsDropDown(etFiltro); 
     } 
    }); 

private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() { 

    MyListView suggestionList; 
    SimpleCursorAdapter adapter; 
    int mHeight; 

    @Override 
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
     SuggestionLoader loader = new SuggestionLoader(MainActivity.this, databaseConnector, filtro); 
     loader.setUpdateThrottle(500); 
     View view = ((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_window,null); 
     suggestionList = (MyListView) view.findViewById(R.id.suggestionList); 
     adapter = new SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_list_item_1,null, 
       new String[]{"note"},new int[]{android.R.id.text1},0); 
     suggestionList.setAdapter(adapter); 
     suggestionList.setEmptyView(view.findViewById(R.id.tvNoSuggestions)); 
     //ensure previous popup is dismissed 
     if (popupWindow!=null) popupWindow.dismiss(); 
     popupWindow = new PopupWindow(view,etFiltro.getWidth(),0); 
     popupWindow.setWindowLayoutMode(0,WindowManager.LayoutParams.WRAP_CONTENT); 
     popupWindow.setAnimationStyle(0);//0 = no animation; -1 = default animation    
     Log.i("loader","onCreateLoader"); 
     return loader; 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { 
     adapter.changeCursor(cursor);   
     Log.i("loader","onLoadFinished"); 

    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> cursorLoader) { 
     Log.i("loader", "onLoaderReset"); 
     adapter.changeCursor(null); 
    } 
}; 

回答

2

我自己找到了解決方案。對於任何人誰會絆倒這個問題,答案是重寫ListView的方法onMeasure()像這樣:

public class MyListView extends ListView { 

public MyListView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public MyListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public MyListView(Context context) { 
    super(context); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    //set your custom height. AT_MOST means it can be as tall as needed, 
    //up to the specified size. 

    int height = MeasureSpec.makeMeasureSpec(300,MeasureSpec.AT_MOST); 

    super.onMeasure(widthMeasureSpec,height);    
} 
} 
相關問題