Spinner - 如何将用户的选择放在顶部
Spinner - How to put user's selection at top position
我想知道是否有人可以帮助我编写代码。我有一个微调器,其中包含一个数组中的六个项目,第三个项目设置为默认值。当用户选择一个项目时,我希望用户的选择移动到微调器的顶部位置。澄清一下,如果我有项目 A、B、C、D、E 和 F:
默认:
C
--
A
B
D
E
F
如果用户选择D:
D
--
A
B
C
E
F
如 "diagram," 所示,我也在尝试弄清楚如何仅在顶部选择上放置分隔线。
这是我目前的代码。我刚刚开始学习 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) {
}
这最适合自定义适配器。我会做以下事情:
- 保留可能值的主列表。
- 将 onSelectedItemAdapter 设置为将设置所选项目的索引的函数。
- 对于getItem()函数,如果position=0,则使用选中的item。如果它 == 1,则使用除法器。如果为 2 或更高,则测试位置 2 是否小于所选项目。如果是,则 return 位置 p-2 的项目,否则 return p-1.
- 将微调器项目的数量设置为比列表中的数字大 1(对于分隔线)
我相信这个逻辑应该可行,用一些简单的东西测试它并在实际示例中使用它之前验证它是否有效。
首先要实现你想要的布局,你得有一个自定义适配器。
所以需要定义一个class如下:
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>
然后在你的activity中:
MyAdapter adapter= new MyAdapter(this, spinnerValues);
mySpinner.setAdapter(adapter);
关于从下拉列表中删除所选项目:
您必须在 activity 中包含以下内容:
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) {
}
我想知道是否有人可以帮助我编写代码。我有一个微调器,其中包含一个数组中的六个项目,第三个项目设置为默认值。当用户选择一个项目时,我希望用户的选择移动到微调器的顶部位置。澄清一下,如果我有项目 A、B、C、D、E 和 F:
默认:
C
--
A
B
D
E
F
如果用户选择D:
D
--
A
B
C
E
F
如 "diagram," 所示,我也在尝试弄清楚如何仅在顶部选择上放置分隔线。
这是我目前的代码。我刚刚开始学习 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) {
}
这最适合自定义适配器。我会做以下事情:
- 保留可能值的主列表。
- 将 onSelectedItemAdapter 设置为将设置所选项目的索引的函数。
- 对于getItem()函数,如果position=0,则使用选中的item。如果它 == 1,则使用除法器。如果为 2 或更高,则测试位置 2 是否小于所选项目。如果是,则 return 位置 p-2 的项目,否则 return p-1.
- 将微调器项目的数量设置为比列表中的数字大 1(对于分隔线)
我相信这个逻辑应该可行,用一些简单的东西测试它并在实际示例中使用它之前验证它是否有效。
首先要实现你想要的布局,你得有一个自定义适配器。
所以需要定义一个class如下:
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>
然后在你的activity中:
MyAdapter adapter= new MyAdapter(this, spinnerValues);
mySpinner.setAdapter(adapter);
关于从下拉列表中删除所选项目:
您必须在 activity 中包含以下内容:
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) {
}