如果点击每个项目右侧的“X”按钮,我想让用户可以删除微调器下拉列表中显示的项目,请参阅下面的示例。
如果用户点击该项目,Spinner应该像经典的Android默认Spinner一样。
我应该如何扩展和重新定义Spinner
以这种方式工作?
答案 0 :(得分:1)
我认为你正在寻找这个:
<强> MainActivity:强>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spnView3 = (Spinner) findViewById(R.id.spnView3);
ArrayList<String> aList = new ArrayList<String>();
aList.add("--Select--");
aList.add("Md. Shahadat Sarker");
aList.add("Developer");
aList.add("ErrrorPoint");
spnView3.setAdapter(new SpinnerAdapter(this, R.layout.spinner_row, aList, aList));
Toast.makeText(this, "Selected: " + spnView3.getSelectedItem(), 500).show();
}
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spnView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
</RelativeLayout>
spinner_row.xml(布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >
<TextView
android:id="@+id/spnItemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/spnItemDel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<强>适配器:强>
public View getCustomView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_row, parent, false);
spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);
spnItemName.setText(iName.get(position)+"");
spnItemDel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
//iName[position] = null;
iName.remove(position);
notifyDataSetChanged();
}
});
return rowView;
}
完整示例是here... download
已编辑的部分:
if( /*Your condition goes here*/ ){
spnItemName.setVisibility(View.GONE);
} else{
spnItemName.setVisibility(View.VISIBLE);
}