最近在项目中使用android原生的Spinner发现其显示效果没有想象中的好,于是就动手写了一个popuwindow的弹出框下拉列表,在popuwindow中主要使用了ListView自带的单选和多选模式。
listview中使用自带的选择框有两种方式,第一种方式:
ListView lv = (ListView) findViewById(R.id.list_view);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
第二中方式:就是在listview布局中直接加,建议使用第二种
android:choiceMode="singleChoice"
先书写一个带listview的布局,popuwindow_spinner布局:
其实在popuwindow中使用listview和在Activity中使用相同,具体实现代码如下(具体使用看注释):
public class SpinnerPopuwindow extends PopupWindow{
private View conentView;
private ListView listView;
private SpinnerPopAdapter adapter;
private Activity context;
private TextView pop_title;
private TextView pop_cancel;
/**
* @param context 上下文
* @param string 获取到未打开列表时显示的值
* @param list 需要显示的列表的集合
* @param itemsOnClick listview在activity中的点击监听事件
*/
@SuppressLint("InflateParams")
public SpinnerPopuwindow(final Activity context, final String string, final List list, AdapterView.OnItemClickListener itemsOnClick) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context =context;
conentView = inflater.inflate(R.layout.popuwindow_spinner, null);
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
// this.setWidth(view.getWidth());
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 刷新状态
this.update();
this.setOutsideTouchable(false);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(dw);
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
darkenBackground(1f);
}
});
//解决软键盘挡住弹窗问题
this.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
// 设置SelectPicPopupWindow弹出窗体动画效果
// this.setAnimationStyle(R.style.AnimationPreview);
adapter = new SpinnerPopAdapter(context,list);
listView = (ListView) conentView.findViewById(R.id.listView);
listView.setOnItemClickListener(itemsOnClick);
listView.setAdapter(adapter);
// setAdapter是异步进行的,为了使弹窗能即时刷新,所以使用post+Runnable
listView.post(new Runnable() {
@Override
public void run() {
//主要是为了比对未打开列表时显示的值和列表中的值进行默认选中
for(int j = 0;j
SpinnerPopAdapter书写方式是最基本的适配器写法:
public class SpinnerPopAdapter extends BaseAdapter {
private List content;
private Context context;
private LayoutInflater mInflater;
public SpinnerPopAdapter(Context context,List content){
this.context = context;
this.content = content;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return content == null ? 0 : content.size();
}
@Override
public Object getItem(int position) {
return content.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView = mInflater.inflate(R.layout.popuwindow_spinner_item,null);
viewHolder = new ViewHolder();
viewHolder.tv_spinner = (TextView) convertView.findViewById(R.id.tv_spinner);
viewHolder.check = (CheckableLayout) convertView.findViewById(R.id.check);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
String spinnerText = content.get(position);
viewHolder.tv_spinner.setText(spinnerText);
return convertView;
}
public static class ViewHolder{
TextView tv_spinner;
public CheckableLayout check;
}
}
adapter中布局,其中要记得自定义选中布局CheckableLayout继承自RelativeLayout并实现Checkable,网上例子很多我就不在这儿写了,popuwindow_spinner_item布局:
最主要的是在Activity中的使用
public class NewTaskActivity extends Activity implements View.OnClickListener{
public static final String TAG = "NewTaskActivity";
private ImageView goback;
private LinearLayout task_type;
private TextView tv_type;
private SpinnerPopuwindow mSpinnerPopuwindow;
/** 模拟的假数据 */
private List testData;
private String type;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newtask);
initView();
}
public void initView(){
goback = (ImageView) findViewById(R.id.goback);
goback.setOnClickListener(this);
task_type = (LinearLayout) findViewById(R.id.task_type);
task_type.setOnClickListener(this);
tv_type = (TextView) findViewById(R.id.tv_type);
TestData();
}
/**
* 模拟假数据
*/
private void TestData() {
testData = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String str = new String("数据" + i);
testData.add(str);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.goback:
this.finish();
break;
case R.id.task_type:
type = tv_type.getText().toString();
mSpinnerPopuwindow = new SpinnerPopuwindow(this,type,testData,itemsOnClick);
mSpinnerPopuwindow.showPopupWindow(task_type);
mSpinnerPopuwindow.setTitleText("类型");//给下拉列表设置标题
break;
}
}
//SchedulePopuwindow为弹出窗口实现监听类
private AdapterView.OnItemClickListener itemsOnClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
String value = testData.get(mSpinnerPopuwindow.getText());
tv_type.setText(value);
mSpinnerPopuwindow.dismissPopupWindow();
}
};
}
Activity中的布局,activity_newtask布局: