2011-11-08 91 views

回答

0

我這樣做,有一個TextView上的onClick方法彈出包含一個ListView一個PopupWIndow,上ListView控件的onClick設置的TextView和解僱彈出。

public class SpinnerDropBox extends TextView{ 

    int mHeight; 
    ListView mListView; 
    List<String> mListChoice; 
    Context mContext; 
    int mWidth = 0; 
    PopupWindow popSpin; 
    TextView pThis; 
    int mLastPos; 

    public SpinnerDropBox(Context context, List<String> listChoice) { 
     super(context); 
     mContext = context; 
     mListChoice = listChoice; 
     mWidth = getMaxWidthOfList(); 
     this.setWidth(mWidth); 
     this.setText(mListChoice.get(0)); 
     pThis = this; 
     this.setOnClickListener(new OnClickListener() {  
      public void onClick(View v) { 
       showDropBox();    
       } 
       }); 
    } 

    private int getMaxWidthOfList(){   
     int width = 0; 
     Paint paint = new Paint(); 
     for(String s : mListChoice){ 
      if((int)this.getPaint().measureText(s) > width){ 
       width = (int)paint.measureText(s); 
      }   
     } 

     width *=3; 
     int screenWidth = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();  
     if(width > screenWidth){ 
      width = screenWidth; 
     } 

     return width; 
    } 

    public void setChoice(int pos){ 
     this.setText(mListChoice.get(pos)); 
    } 

    void showDropBox(){ 

     int xPos, yPos, arrowPos; 
     int[] location  = new int[2];  
     this.getLocationOnScreen(location); 
     Rect rect = new Rect(location[0], location[1], location[0] + this.getWidth(), location[1] + this.getHeight());    
     this.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
     int rootHeight  = this.getMeasuredHeight(); 
     int rootWidth = 0; 
     if (rootWidth == 0) { 
      rootWidth  = this.getMeasuredWidth(); 
     } 

     int screenWidth  = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();    
     int screenHeight = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();   
     if ((rect.left + rootWidth) > screenWidth) { 
      xPos  = rect.left - (rootWidth-this.getWidth());   
      xPos  = (xPos < 0) ? 0 : xPos;    
      arrowPos = rect.centerX()-xPos;   
     } else { 
      if (this.getWidth() > rootWidth) { 
       xPos = rect.centerX() - (rootWidth/2); 
      } else { 
       xPos = rect.left; 
      }   
      arrowPos = rect.centerX()-xPos; 
     } 

     popSpin = new PopupWindow(mContext); 
     popSpin.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
     popSpin.setWidth(mWidth); 

     popSpin.setTouchable(true); 
     popSpin.setFocusable(true); 

     LinearLayout laySpin = new LinearLayout(mContext); 
     laySpin.setLayoutParams(new LayoutParams(mWidth, LayoutParams.WRAP_CONTENT)); 

     ListView lv = new ListView(mContext); 
     lv.setLayoutParams(new LayoutParams(mWidth, LayoutParams.WRAP_CONTENT)); 

     lv.setItemsCanFocus(true); 
     lv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
     lv.setFocusable(false); 
     lv.setOnItemClickListener(listListener);   

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext.getApplicationContext(), R.layout.simple, mListChoice); 
     lv.setAdapter(adapter); 
     laySpin.addView(lv); 

     popSpin.setContentView(laySpin);   

     if((screenHeight-rect.bottom) > (rootHeight*mListChoice.size())){ 
      yPos = rect.bottom; 
     } 
     else{ 
      yPos = rect.top - (rootHeight*mListChoice.size()+mListChoice.size()); 
     } 

     popSpin.showAtLocation(laySpin, Gravity.NO_GRAVITY, xPos, yPos); 

    } 

    private OnItemClickListener listListener = new OnItemClickListener() {  
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      mLastPos = arg2; 
      pThis.setText(((TextView)arg1).getText().toString());   
      popSpin.dismiss(); 
     } 
    }; 

    public int getLastPosition(){ 
     return mLastPos; 
    }