效果:点击开始按钮弹出开始选择框,点击巡检方式弹出可单选的菜单,点击巡检人员弹出可多选的菜单
1,单选多选下拉采用dialog自定义view的方式
部分代码截图:
(1)点击开始巡检添加点击事件
(2)dialog自定义view引入下拉菜单的布局
创建dialog
(3)巡检方式单选下拉采用android spinner自带下拉
(4)巡检人员多选下拉采用插件,按照参考引入部分文件即可
参考地址
部分代码:
dialog_patrol_start.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 选择巡检方式 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="20dip"
android:background="@color/white">
<TextView
android:layout_width="@dimen/link_tv_left"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_left"
android:gravity="fill_vertical"
android:text="巡检方式"
android:textColor="@color/c3"
android:textSize="@dimen/title" />
<Spinner
android:id="@+id/spWay"
android:layout_width="match_parent"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_h7"
android:textSize="@dimen/table_body"
android:gravity="fill_vertical"
android:textColor="@color/project_word_color"
android:background="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="right">
<ImageView
android:layout_width="12dip"
android:layout_height="16dip"
android:layout_marginTop="12dip"
android:layout_marginRight="15dip"
android:gravity="fill_vertical"
android:background="@drawable/sp_img_gray"
/>
</LinearLayout>
<View android:layout_alignBottom="@id/spWay"
android:layout_width="match_parent"
android:layout_height="@dimen/link_line_h"
android:background="@color/gray_white"/>
</RelativeLayout>
<!-- 选择巡检人员 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="20dip"
android:background="@color/white">
<TextView
android:layout_width="@dimen/link_tv_left"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_left"
android:gravity="fill_vertical"
android:text="巡检人员"
android:textColor="@color/c3"
android:textSize="@dimen/title" />
<!-- <Spinner
android:id="@+id/spPerson"
android:layout_width="match_parent"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_h7"
android:textSize="@dimen/table_body"
android:gravity="fill_vertical"
android:textColor="@color/project_word_color"
android:background="@color/white" />-->
<!-- 列表弹出框(对话框样式【多选】) -->
<com.andbase.util.spinner.SpinnerViewMultiDialog
android:id="@+id/spPerson"
android:layout_width="match_parent"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_h7"
android:textSize="@dimen/table_body"
android:gravity="fill_vertical"
android:textColor="@color/project_word_color"
android:background="@color/white" />
<TextView
android:id="@+id/tvShow"
android:layout_below="@id/spPerson"
android:layout_width="match_parent"
android:layout_height="@dimen/link_rl_tv_h"
android:layout_marginLeft="@dimen/link_rl_tv_h7"
android:layout_marginTop="-40dip"
android:gravity="fill_vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="right">
<ImageView
android:layout_width="12dip"
android:layout_height="16dip"
android:layout_marginTop="12dip"
android:layout_marginRight="15dip"
android:gravity="fill_vertical"
android:background="@drawable/sp_img_gray"/>
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/gray_white"/>
<LinearLayout
android:id="@+id/llBox"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="@null">
<Button
android:id="@+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:text="取消" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="@color/gray_white"/>
<Button
android:id="@+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:text="确定" />
</LinearLayout>
</LinearLayout>
dialogView() 部分:
/**
* 开始巡检弹出菜单
*/
private void dialogView() {
View view1 = LayoutInflater.from(mContext).inflate(R.layout.dialog_patrol_start,null);
//巡检方式
spWay = (Spinner) view1.findViewById(R.id.spWay);
adapterWay = new ArrayAdapter<>(mContext, android.R.layout.simple_dropdown_item_1line,way);
spWay.setAdapter(adapterWay);
spWay.setSelection(0, true);//让第一个数据项已经被选中
//巡检人员
// spPerson = (Spinner) view1.findViewById(R.id.spPerson);
spPerson = (SpinnerViewMultiDialog) view1.findViewById(R.id.spPerson);
tvShow = (TextView) view1.findViewById(R.id.tvShow);
mSpinnerPersonList = new ArrayList<SpinnearBean>();
//模拟获取数据集合
try{
mSpinnerPersonList = parseJsonArray("spinners4.txt");
}catch (Exception e) {
e.printStackTrace();
}
//设置下拉菜单显示的列表项文本
if (mSpinnerPersonList != null && mSpinnerPersonList.size() > 0){
spPerson.setData(mSpinnerPersonList);//设置下拉菜单列表集合源
spPerson.setHint("请选择巡检人员");
}
//下拉菜单列表的列表项的点击事件监听
// spPerson.setOnSpinnerConfirmClickListener(new OnSpinnerConfirmClickListener() {
// @Override
// public void OnConfirmed(ArrayList<Boolean> selecteIndexList) {
// StringBuffer str1 = new StringBuffer();
// for(int i=0;i<selecteIndexList.size();i++){
// if(selecteIndexList.get(i)){//如果为true,则执行下面的代码
//// str1.append(mSpinnerPersonList.get(i).getParaName() + ":" + mSpinnerPersonList.get(i).getParaValue());
// str1.append(mSpinnerPersonList.get(i).getParaName() + " " );
// }
// }
// tvShow.setVisibility(View.VISIBLE);
//// tvShow.setText(str1);
//
//// StringBuffer str = new StringBuffer();
//// for(int i=0;i<mSpinnerPersonList.size();i++){
//// str.append(mSpinnerPersonList.get(i).getParaName() + ":" + mSpinnerPersonList.get(i).isSelectedState() + "\n");
//// }
//// tvShow.setText(tvShow.getText() + "=====================\n" + str);
// }
// });
// SpinnerOption c = new SpinnerOption( "-1", "请选择巡检人员");
// personList.add(c);
// //设置Adapter
// ArrayAdapter<SpinnerOption> townAdapter = new ArrayAdapter<SpinnerOption>(mContext,android.R.layout.simple_spinner_item, personList);
// townAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// spPerson.setAdapter(townAdapter);
Button cancel = (Button) view1.findViewById(R.id.btnCancel);
Button ok = (Button) view1.findViewById(R.id.btnOk);
//创建Dialog
final AlertDialog dialog = new AlertDialog.Builder(mContext)
.create();
dialog.setTitle("开始巡检");
dialog.setCancelable(false);
//设置自定义view
dialog.setView(view1);
dialog.show();
//取消按钮监听点击事件
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
//确定按钮监听点击事件
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if (spPerson.getText().toString().equals("00000000"))
// {
// Toast.makeText(mContext,"密码正确!",Toast.LENGTH_SHORT).show();
// dialog.dismiss();
// }
// else
// {
// Toast.makeText(mContext,"密码错误,请重新输入!",Toast.LENGTH_SHORT).show();
// inputPwd.setText("");
// }
}
});
}
adapter.java
package com.andbase.search.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.ab.util.AbToastUtil;
import com.andbase.R;
import com.andbase.gettask.model.PatrolTask;
import com.andbase.history.dao.HistoryTaskDao;
import com.andbase.model.appModel.SpinnerOption;
import com.andbase.search.activity.PatrolEndReasonActivity;
import com.andbase.search.activity.SearchActivity;
import com.andbase.search.activity.SearchPointActivity;
import com.andbase.util.spinner.SpinnerViewMultiDialog;
import com.andbase.util.spinner.SpinnerViewPop;
import com.andbase.util.spinner.listener.OnSpinnerConfirmClickListener;
import com.google.gson.Gson;
import com.andbase.util.spinner.SpinnearBean;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* author:lmx
* date:2018/4/19
* description:巡检
*/
public class SearchAdapter extends BaseAdapter {
private SearchActivity mContext;
//单行的布局
private int mResource;
//列表展现的数据
private List<? extends Map<String, ?>> mData;
//Map中的key
private String[] mFrom;
//view的id
private int[] mTo;
//下拉菜单
private String way[] = new String[]{"在线巡检","离线巡检"};
private Spinner spWay;
private int wayindex;
private String person [][] = null;
private ArrayAdapter<String> adapterWay,adapterPerson;
private HistoryTaskDao historyTaskDao;
private List<SpinnerOption> personList = new ArrayList<SpinnerOption>();
private String hisTaskIds;//
//下拉多选
private SpinnerViewMultiDialog spPerson;
private TextView tvShow;
/**下拉菜单列表集合*/
private ArrayList<SpinnearBean> mSpinnerPersonList;
/**
* 构造方法
* @param context
* @param data 列表展现的数据
* @param resource 单行的布局
* @param from Map中的key
* @param to view的id
*/
public SearchAdapter(SearchActivity context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to){
mContext = context;
mData = data;
mResource = resource;
mFrom = from;
mTo = to;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder holder;
if(convertView == null){
//使用自定义的list_items作为Layout
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
//使用减少findView的次数
holder = new ViewHolder();
holder.itemNo = ((TextView) convertView.findViewById(mTo[0]));
holder.lineName = ((TextView) convertView.findViewById(mTo[1]));
holder.patrolTime = ((TextView) convertView.findViewById(mTo[2]));
holder.finish = ((Button) convertView.findViewById(mTo[3]));
holder.guid = ((TextView) convertView.findViewById(mTo[4]));
holder.status = ((ImageView) convertView.findViewById(mTo[5]));
holder.start = ((Button) convertView.findViewById(mTo[6]));
//设置标记
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//设置数据
final Map<String, ?> dataSet = mData.get(position);
if (dataSet == null) {
return null;
}
//获取该行数据
final Object itemNo = dataSet.get(mFrom[0]);
final Object lineName = dataSet.get(mFrom[1]);
final Object patrolTime = dataSet.get(mFrom[2]);
final Object finish = dataSet.get(mFrom[3]);
final Object guid = dataSet.get(mFrom[4]);
final Object status = dataSet.get(mFrom[5]);
final Object start = dataSet.get(mFrom[6]);
/*** 有用 ***/
final Object startTime = dataSet.get(mFrom[6]);
final Object endTime = dataSet.get(mFrom[7]);
final Object lineCode = dataSet.get(mFrom[8]);
final Object executeStatus = dataSet.get(mFrom[9]);
final Object isOrder = dataSet.get(mFrom[10]);
/*** 有用 ***/
holder.itemNo.setText(itemNo.toString());
holder.lineName.setText(lineName.toString());
// holder.patrolTime.setText(patrolTime.toString());
holder.finish.setText(finish.toString());
holder.guid.setText(guid.toString());
// holder.status.setText(status.toString());
holder.status.equals(status.toString());
/** 开始巡检 **/
holder.start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialogView();
}
});
return convertView;
}
/**
* ViewHolder类
*/
static class ViewHolder {
TextView itemNo;//编号
TextView lineName;//巡检线路
TextView patrolTime;//巡检时间
ImageView status;//任务状态
TextView guid;//id
TextView lineCode;//巡检线路
Button start;//开始巡检
Button finish;//结束巡检
}
/**
* 开始巡检弹出菜单
*/
private void dialogView() {
View view1 = LayoutInflater.from(mContext).inflate(R.layout.dialog_patrol_start,null);
//巡检方式
spWay = (Spinner) view1.findViewById(R.id.spWay);
adapterWay = new ArrayAdapter<>(mContext, android.R.layout.simple_dropdown_item_1line,way);
spWay.setAdapter(adapterWay);
spWay.setSelection(0, true);//让第一个数据项已经被选中
//巡检人员
// spPerson = (Spinner) view1.findViewById(R.id.spPerson);
spPerson = (SpinnerViewMultiDialog) view1.findViewById(R.id.spPerson);
tvShow = (TextView) view1.findViewById(R.id.tvShow);
mSpinnerPersonList = new ArrayList<SpinnearBean>();
//模拟获取数据集合
try{
mSpinnerPersonList = parseJsonArray("spinners4.txt");
}catch (Exception e) {
e.printStackTrace();
}
//设置下拉菜单显示的列表项文本
if (mSpinnerPersonList != null && mSpinnerPersonList.size() > 0){
spPerson.setData(mSpinnerPersonList);//设置下拉菜单列表集合源
spPerson.setHint("请选择巡检人员");
}
//下拉菜单列表的列表项的点击事件监听
// spPerson.setOnSpinnerConfirmClickListener(new OnSpinnerConfirmClickListener() {
// @Override
// public void OnConfirmed(ArrayList<Boolean> selecteIndexList) {
// StringBuffer str1 = new StringBuffer();
// for(int i=0;i<selecteIndexList.size();i++){
// if(selecteIndexList.get(i)){//如果为true,则执行下面的代码
//// str1.append(mSpinnerPersonList.get(i).getParaName() + ":" + mSpinnerPersonList.get(i).getParaValue());
// str1.append(mSpinnerPersonList.get(i).getParaName() + " " );
// }
// }
// tvShow.setVisibility(View.VISIBLE);
//// tvShow.setText(str1);
//
//// StringBuffer str = new StringBuffer();
//// for(int i=0;i<mSpinnerPersonList.size();i++){
//// str.append(mSpinnerPersonList.get(i).getParaName() + ":" + mSpinnerPersonList.get(i).isSelectedState() + "\n");
//// }
//// tvShow.setText(tvShow.getText() + "=====================\n" + str);
// }
// });
// SpinnerOption c = new SpinnerOption( "-1", "请选择巡检人员");
// personList.add(c);
// //设置Adapter
// ArrayAdapter<SpinnerOption> townAdapter = new ArrayAdapter<SpinnerOption>(mContext,android.R.layout.simple_spinner_item, personList);
// townAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// spPerson.setAdapter(townAdapter);
Button cancel = (Button) view1.findViewById(R.id.btnCancel);
Button ok = (Button) view1.findViewById(R.id.btnOk);
//创建Dialog
final AlertDialog dialog = new AlertDialog.Builder(mContext)
.create();
dialog.setTitle("开始巡检");
dialog.setCancelable(false);
//设置自定义view
dialog.setView(view1);
dialog.show();
//取消按钮监听点击事件
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
//确定按钮监听点击事件
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if (spPerson.getText().toString().equals("00000000"))
// {
// Toast.makeText(mContext,"密码正确!",Toast.LENGTH_SHORT).show();
// dialog.dismiss();
// }
// else
// {
// Toast.makeText(mContext,"密码错误,请重新输入!",Toast.LENGTH_SHORT).show();
// inputPwd.setText("");
// }
}
});
}
/**
* 解析JSON文件的简单数组
*/
private ArrayList<SpinnearBean> parseJsonArray(String fileName) throws Exception{
ArrayList<SpinnearBean> itemsList = new ArrayList<SpinnearBean>();
// String jsonStr = getStringFromAssert(mContext, fileName);
// if(jsonStr.equals("")){
// return null;
// }
// JSONObject allData = new JSONObject(jsonStr); //全部内容变为一个项
// JSONArray jsonArr = allData.getJSONArray(LISTROOTNODE); //取出数组
// for(int x = 0;x<jsonArr.length();x++){
// SpinnearBean model = new SpinnearBean();
// JSONObject jsonobj = jsonArr.getJSONObject(x);
// model.setParaName(jsonobj.getString(KEY_LISTITEM_NAME));
// model.setParaValue(jsonobj.getString(KEY_LISTITEM_VALUE));
// if(jsonobj.has(KEY_LISTITEM_CHECKCOLOR)){
// model.setCheckColor(jsonobj.getString(KEY_LISTITEM_CHECKCOLOR));
// }
// model.setSelectedState(false);
// itemsList.add(model);
// model = null;
// }
// JSONArray jsonArr = allData.getJSONArray(LISTROOTNODE); //取出数组
for(int x = 0;x<4;x++){
SpinnearBean model = new SpinnearBean();
// JSONObject jsonobj = jsonArr.getJSONObject(x);
model.setParaName("张三");
model.setParaValue("巡检一班");
// if(jsonobj.has(KEY_LISTITEM_CHECKCOLOR)){
// model.setCheckColor(jsonobj.getString(KEY_LISTITEM_CHECKCOLOR));
// }
model.setSelectedState(false);
itemsList.add(model);
model = null;
}
return itemsList;
}
}