如果您想创建自己的微调器,则快速(且效率低下)示例。(与列表视图适配器一起使用):
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<Button android:id="@+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="up"/>
<Button android:id="@+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down"/>
</LinearLayout>
</LinearLayout>
CustomSpinner 类:
public class CustomSpinner extends FrameLayout{
// ------------------------------ FIELDS ------------------------------
private FrameLayout mContainer;
private SpinnerAdapter mAdapter;
private int index = 0;
// --------------------------- CONSTRUCTORS ---------------------------
public CustomSpinner(Context context) {
super(context);
init(context);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false));
findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
index--;
refresh();
}
});
findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
index++;
refresh();
}
});
mContainer = (FrameLayout) findViewById(R.id.container);
}
// -------------------------- OTHER METHODS --------------------------
public void setAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
refresh();
}
private void refresh() {
//----needs recycling for better performance---
//---now, we'll just clear up--
mContainer.removeAllViews();
//---do we need to refresh ? -----
if(mAdapter == null || mAdapter.getCount() == 0){return;}
//--clamp index--
index = Math.max(0,index);
index = Math.min(mAdapter.getCount() - 1, index);
//--get view and show it-----
View currentView = mAdapter.getView(index,null,mContainer);
mContainer.addView(currentView);
}
}
用例:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.CustomSpinner android:id="@+id/custom_spinner"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
活动:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});
((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter);
}
}