8

私は基本的なアンドロイドスピナーを持っています。それをクリックした後、最初に選択されたアイテムの1つが強調表示されたリストが必要です。

ここで行われているように: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png

しかし、ラジオボックスではなく、私自身のバックグラウンドを使用して、アイテムの独自のレイアウトを使用しています。

どうすればこれを達成できますか?セレクターに役立つものはありますか、それともプログラムで行う必要がありますか?

どんな助けでも大歓迎です。

4

4 に答える 4

5
public class MainActivity extends Activity {

Spinner mySpinner;
String[] myArray={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mySpinner = (Spinner)findViewById(R.id.spinner_test);

    mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
        LayoutInflater inflater = getLayoutInflater();
        View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);

        TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
        mytext.setText(myArray[position]);

        //int selected = Spinner.
        int selected = mySpinner.getSelectedItemPosition();
        if(position == selected){
            spinnerItem.setBackgroundColor(Color.BLUE);
        }
        return spinnerItem;

    }

}

}

これは役立つはずです。

于 2013-12-27T21:43:44.647 に答える
-2

私の質問に答えるには、次のようなものが必要です。

public class mySpinnerAdapter extends SimpleCursorAdapter implements SpinnerAdapter {

public OrderSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO whatever you need
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
    }

    // TODO set the background color of the convertView
    // depending on your wishes

    return convertView;
}
}

このようにドロップダウン リストの作成を制御できるようにします。別のセレクターが必要な場合は、XML ファイルで簡単に行うことができます。

次に、単純にアダプターを作成し、メソッド setAdapter を使用してスピナーにバインドします。

于 2012-09-18T12:57:32.310 に答える