¿Cómo crear una ruleta desplegable en Android <3.0 que parezca una ruleta desplegable para Android> 3.0?

Me gustaría crear una ruleta desplegable en Android> = 1.6 que parezca una ruleta desplegable en Android 3.0 donde la lista de opciones se adjunta al selector (el valor seleccionado).

Gracias

preguntado el 08 de noviembre de 11 a las 10:11

1 Respuestas

Hice eso con un TextView, en el método onClick, haga aparecer un PopupWIndow que contenga un ListView, en listView onClick configure el TextView y descarte la ventana emergente.

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

respondido 24 nov., 11:00

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.