Spinner - 如何将用户的选择放在最高位置

时间:2015-08-07 14:47:48

标签: android spinner android-spinner android-adapter

我想知道是否有人可以帮我处理我的代码。我有一个微调器包含一个数组中的六个项目,第三项设置为默认值。当用户选择一个项目时,我希望用户的选择移动到微调器中的顶部位置。澄清一下,如果我有项目A,B,C,D,E和F:

默认值:

C

--

A

B

D

E

F

如果用户选择D:

D

--

A

B

C

E

F

如“图表”中所示,我也试图找出如何仅在顶部选择上放置分隔符。

这是我到目前为止的代码。我刚刚开始学习Android,所以我对所有内容都很满意,但感谢任何帮助,谢谢!

- 更新的代码 -

public class Test extends AppCompatActivity implements OnItemSelectedListener {

List<String> list;
String [] objects = {"A","B","C","D","E","F"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
        android.R.layout.simple_spinner_item, objects);

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    list = new ArrayList<>(Arrays.asList(objects));

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);
    spinner.setSelection(2);

    adapter.notifyDataSetChanged();
}
public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
    list.remove(pos);
    objects=list.toArray(new String[0]);
    adapter.notifyDataSetChanged();
}

public void onNothingSelected(AdapterView<?> parent) {

}

2 个答案:

答案 0 :(得分:0)

这适用于自定义适配器。我会做以下事情:

  1. 保留可能值的主列表。
  2. 将onSelectedItemAdapter设置为将设置所选项目索引的函数。
  3. 对于getItem()函数,如果position = 0,则使用所选项。如果== 1,请使用分隔符。如果为2或更高,则测试位置-2是否小于所选项目。如果是,则返回位置为p-2的项目,否则返回p-1。
  4. 将微调器项的数量设置为高于列表中的数字(对于分隔符)
  5. 我相信这个逻辑应该有效,用简单的东西测试它,并在真实例子中使用它之前验证它是否有效。

答案 1 :(得分:0)

首先要实现所需的布局,您必须拥有自定义适配器。

所以你需要按如下方式定义一个类:

public class MyAdapter extends ArrayAdapter<String>{
   Context c;
String [] objects;
   public MyAdapter(Context c, String [] objects){
          this.c=c;
this.objects=objects;
   }


    @Override 
   public View getDropDownView(int position, View cnvtView, ViewGroup prnt) { 
    return getCustomView(position, cnvtView, prnt);
 } 
    @Override 
     public View getView(int pos, View cnvtView, ViewGroup prnt) { 
return getCustomView(pos, cnvtView, prnt); 
           }
        }

public View getCustomView(int position, View convertView, ViewGroup parent) 

{ 
LayoutInflater inflater = getLayoutInflater(); 
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false); 
RelativeLayout layout= (RelativeLayout) mySpinner.findViewById(R.id.layout);
TextView letter= (TextView) mySpinner.findViewById(R.id.letter);
 letter.setText(objects[position]);
 if(position==0){
 layout.setVisibility(true);
 }
return mySpinner;
}

至于xml是custom_spinner.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
<TextView
android:id="@+id/letter"
android:height="wrap_content"
android:width="wrap_content"/>
<RelativeLayout
android:id="@+id/layout"
android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<View 
android:id="@+id/line"
android:height="1dp"
android:width="5dp"/>
<View
android:layout_toRightOf="@+id/line"
    android:height="1dp"
android:width="5dp"/>
        </RelativeLayout>
</RelativeLayout>

然后在你的活动中:

MyAdapter adapter= new MyAdapter(this, spinnerValues);
mySpinner.setAdapter(adapter);

至于从下拉列表中删除所选项目:

您必须在活动中拥有以下内容:

List<String> list;
String [] objects;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);

Spinner spinner = (Spinner) findViewById(R.id.spinnerTime);
spinner.setOnItemSelectedListener(this);
objects= {"A","B","C","D","E","F"};
list = new ArrayList<>(Arrays.asList(objects));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        objects, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);
spinner.setSelection(2);

adapter.notifyDataSetChanged();
 }
public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
  list = new ArrayList<>(Arrays.asList(R.array.time_period));
  list.remove(pos);
  objects=list.toArray(new String[0]);
adapter.notifyDataSetChanged();
 }

public void onNothingSelected(AdapterView<?> parent) {

}