Android 中自定义下拉样式 Spinner
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
/**
自定义 SpinerPopWindow 类
@author gao_chun
*/
public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{
private Context mContext;
private ListView mListView;
private SpinerAdapter mAdapter;
private IOnItemSelectListener mItemSelectListener;
public SpinerPopWindow(Context context)
{
super(context);
mContext = context;
init();
}
public void setItemListener(IOnItemSelectListener listener){
mItemSelectListener = listener;
}
public void setAdatper(SpinerAdapter adapter){
mAdapter = adapter;
mListView.setAdapter(mAdapter);
}
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);
mListView.setOnItemClickListener(this);
}
public void refreshData(List<String> list, int selIndex)
{
if (list != null && selIndex != -1)
{
if (mAdapter != null){
mAdapter.refreshData(list, selIndex);
}
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
dismiss();
if (mItemSelectListener != null){
mItemSelectListener.onItemClick(pos);
}
}
}
三、定义 Adapter
package org.gaochun.adapter;
import java.util.List;
import org.gaochun.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SpinerAdapter extends BaseAdapter {
public static interface IOnItemSelectListener{
public void onItemClick(int pos);
};
private List<String> mObjects;
private LayoutInflater mInflater;
public SpinerAdapter(Context context,List<String> mObjects){
this.mObjects = mObjects;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void refreshData(List<String> objects, int selIndex){
mObjects = objects;
if (selIndex < 0){
selIndex = 0;
}
if (selIndex >= mObjects.size()){
selIndex = mObjects.size() - 1;
}
}
@Override
public int getCount() {
return mObjects.size();
}
@Override
public Object getItem(int pos) {
return mObjects.get(pos).toString();
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View convertView, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.spiner_item_layout, null);
viewHolder = new ViewHolder();
viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//Object item = getItem(pos);
viewHolder.mTextView.setText(mObjects.get(pos));
return convertView;
}
public static class ViewHolder
{
public TextView mTextView;
}
}
四、调用示例
public class MainActivity extends Activity implements OnClickListener,SpinerAdapter.IOnItemSelectListener{
private List<String> mListType = new ArrayList<String>(); //类型列表
private TextView mTView;
private SpinerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mTView = (TextView) findViewById(R.id.tv_value);
//初始化数据
String[] names = getResources().getStringArray(R.array.array_name);
for(int i = 0; i < names.length; i++){
mListType.add(names[i]);
}
mAdapter = new SpinerAdapter(this,mListType);
mAdapter.refreshData(mListType,0);
//显示第一条数据
//mTView.setText(names[0]);
//初始化 PopWindow
mSpinerPopWindow = new SpinerPopWindow(this);
mSpinerPopWindow.setAdatper(mAdapter);
mSpinerPopWindow.setItemListener(this
);
}
//设置 PopWindow
private SpinerPopWindow mSpinerPopWindow;
private void showSpinWindow(){
Log.e("", "showSpinWindow");
mSpinerPopWindow.setWidth(mTView.getWidth());
mSpinerPopWindow.showAsDropDown(mTView);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_dropdown:
showSpinWindow();
break;
}
}
/* (non-Javadoc)
@see org.gaochun.adapter.SpinerAdapter.IOnItemSelectListener#onItemClick(int)
*/
@Override
public void onItemClick(int pos) {
// TODO Auto-generated method stub
if (pos >= 0 && pos <= mListType.size()){
String value = mListType.get(pos);
mTView.setText(value.toString());
}
}
}
源码下载:http://download.csdn.net/download/gao_chun/8653069
五、自定义控件系列
Android 项目中使用自定义进度加载 Dialog:http://blog.csdn.net/gao_chun/article/details/45270031
ListView中按钮监听器 设置 及 优化:http://blog.csdn.net/gao_chun/article/details/41249131
Android 项目中自定义顶部标题栏:http://blog.csdn.net/gao_chun/article/details/45255929
Android自定义设置圆形图片控件:http://blog.csdn.net/gao_chun/article/details/39207557
评论