场景:自定义下拉框兑现(android)
自定义下拉框实现(android)
说道android下拉框spineer,框架中虽有现成的控件,但实际效果可能并不是我们所需要的那种,如下图:
其实我们更需要的是像WEB那种风格,如图所示:
其实实现也很简单,就是自定义个popwindow就可以了
下面贴上代码片段:
public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{
private Context mContext;
private ListView mListView;
private NormalSpinerAdapter mAdapter;
private IOnItemSelectListener mItemSelectListener;
public SpinerPopWindow(Context context)
{
super(context);
mContext = context;
init();
}
public void setItemListener(IOnItemSelectListener listener){
mItemSelectListener = listener;
}
private void init()
{
View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, null);
setContentView(view);
setWidth(LayoutParams.WRAP_CONTENT);
setHeight(LayoutParams.WRAP_CONTENT);
setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x00);
setBackgroundDrawable(dw);
mListView = (ListView) view.findViewById(R.id.listview);
mAdapter = new NormalSpinerAdapter(mContext);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
}
public void refreshData(List list, int selIndex)
{
if (list != null && selIndex != -1)
{
mAdapter.refreshData(list, selIndex);
}
}
@Override
public void onItemClick(AdapterView> arg0, View view, int pos, long arg3) {
dismiss();
if (mItemSelectListener != null){
mItemSelectListener.onItemClick(pos);
}
}
}主界面调用代码:
public class SpinerWindowDemoActivity extends Activity implements OnClickListener, AbstractSpinerAdapter.IOnItemSelectListener{
/** Called when the activity is first created. */
private View mRootView;
private TextView mTView;
private ImageButton mBtnDropDown;
private List nameList = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
mRootView = findViewById(R.id.rootView);
mTView = (TextView) findViewById(R.id.tv_value);
mBtnDropDown = (ImageButton) findViewById(R.id.bt_dropdown);
mBtnDropDown.setOnClickListener(this);
String[] names = getResources().getStringArray(R.array.hero_name);
for(int i = 0; i < names.length; i++){
nameList.add(names[i]);
}
mSpinerPopWindow = new SpinerPopWindow(this);
mSpinerPopWindow.refreshData(nameList, 0);
mSpinerPopWindow.setItemListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.bt_dropdown:
showSpinWindow();
break;
}
}
private void setHero(int pos){
if (pos >= 0 && pos <= nameList.size()){
String value = nameList.get(pos);
mTView.setText(value);
}
}
private SpinerPopWindow mSpinerPopWindow;
private void showSpinWindow(){
Log.e("", "showSpinWindow");
mSpinerPopWindow.setWidth(mTView.getWidth());
mSpinerPopWindow.showAsDropDown(mTView);
}
@Override
public void onItemClick(int pos) {
setHero(pos);
}
}
就这么简单,具体UI美化大家再修改下xml就可以了
下面附上工程链接:
http://download.csdn.net/detail/geniuseoe2012/5184664
more brilliant,Please pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012