[list=1]
package com.photo.adapter; import java.util.ArrayList; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.photo.activity.R; /** * @description 工程名称 带下拉选择的可编辑输入框,PopupWindow的数据适配器 * * @author Li Junchao * @date 2014年5月9日 */ public class PopupAdapter extends BaseAdapter { private Context context; private ArrayListlist = new ArrayList (); @SuppressWarnings("unused") private Handler handler = null; private LayoutInflater layoutInflater; public PopupAdapter() { } public PopupAdapter(Context context,ArrayList list) { this.context = context; this.list = list; } public PopupAdapter(Context context,ArrayList list,Handler handler) { this.context = context; this.list = list; this.handler = handler; } /* (non-Javadoc) * @see android.widget.Adapter#getCount() */ @Override public int getCount() { return this.list != null ? list.size() : 0; } /* (non-Javadoc) * @see android.widget.Adapter#getItem(int) */ @Override public Object getItem(int position) { return list.get(position); } /* (non-Javadoc) * @see android.widget.Adapter#getItemId(int) */ @Override public long getItemId(int position) { return position; } /* (non-Javadoc) * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHoler holder = null; if(null == convertView) { holder = new ViewHoler(); layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(R.layout.proname_spinner_item,null); holder.imgBtn = (ImageView)convertView.findViewById(R.id.item_icon); holder.txtView = (TextView)convertView.findViewById(R.id.item_val); //绑定每一列的数据对象在列表项上 convertView.setTag(holder); } else { holder = (ViewHoler)convertView.getTag(); } if(holder != null) { //给每个列表项指定一个Id标识符,这个标识符是一个确切的数字 convertView.setId(position); //同时设置列表项中每个元素的标识符,都是本列表项在列表集合中的索引 holder.setId(position); //从数据集合中取值显示列表文本 holder.txtView.setText(list.get(position)); //设置触摸事件 这里最好不要为每一个列表项绑定touch事件,可以为ListView绑定OnItemClickListener // holder.txtView.setOnTouchListener(new SelectedItemTouchListener(list,position,handler)); } return convertView; } /** * 列表项触摸事件 */ public class SelectedItemTouchListener implements View.OnTouchListener { private ArrayList list = new ArrayList (); private int position = 0; private Handler handler = null; public SelectedItemTouchListener() { } public SelectedItemTouchListener(ArrayList list,int position,Handler handler) { this.list = list; this.position = position; this.handler = handler; } @Override public boolean onTouch(View v, MotionEvent event) { Message message = new Message(); Bundle bundle = new Bundle(); // if(event.getAction() == MotionEvent.ACTION_OUTSIDE) // { // message.what = 0; // } // else // { Log.d("listview item ontouch",list.get(position)); bundle.putString("value",list.get(position)); bundle.putInt("selectedIndex",position); message.setData(bundle); message.what = 1; // } //发送一个消息,在主界面里设置输入文本框的值 this.handler.sendMessage(message); return true; } } /** * @description 记录列表项数据,绑定数据对象到每一个列表项 * * @author Admin * @date 2014年5月9日 */ public class ViewHoler { private TextView txtView; private ImageView imgBtn; /** * @description 列表项元素 * * @param position */ public void setId(int position) { this.txtView.setId(position); this.imgBtn.setId(position); } public TextView getTxtView() { return txtView; } public void setTxtView(TextView txtView) { this.txtView = txtView; } public ImageView getImgBtn() { return imgBtn; } public void setImgBtn(ImageView imgBtn) { this.imgBtn = imgBtn; } } }
PopupWindow ppwin = null; //工程名下拉列表的Popupwindow ListView listView = null; ArrayListproNames = new ArrayList (); PopupAdapter adapter = null; ImageButton arrowBtn = null; boolean isInit = false; @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(hasFocus) { //每次当前activity获得当前焦点都重新读取sd下已经创建的项目目录 Log.d("onWindowFocusChanged","重新获取工程目录"); getProjectNamesfromSD(); if(adapter != null && !adapter.isEmpty()) { adapter.notifyDataSetChanged(); } } while(!isInit) { Log.d("EnrolmentActivity.onWindowFocusChanged","初始化popupwindow"); initPopupWindow(); isInit = true; } } /** * @description 从sd卡上读取项目名称,返回项目名称数据列表集合 * * @return */ private void getProjectNamesfromSD() { proNames.clear(); //如果SD卡当前是已安装和准备好读写的状态 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File projectDir = new File(Environment.getExternalStorageDirectory(),"photepro"); //如果工程文件目录存在 if(projectDir.exists() && projectDir.isDirectory()) { String[] _filesName = projectDir.list(); for(String name : _filesName) { if(!"tmp".equals(name)) { proNames.add(name); } } } } } /** * @description 初始化PopupWindow * */ private void initPopupWindow() { // handler = new PopupHandler(this); //项目名称下拉列表控件 下拉按钮 arrowBtn = (ImageButton)findViewById(R.id.open_arrow_btn); arrowBtn.setOnClickListener(arrowBtnClick); View view = (View)this.getLayoutInflater().inflate(R.layout.proname_spinner,null); this.listView = (ListView)view.findViewById(R.id.proname_list); this.adapter = new PopupAdapter(EnrolmentActivity.this,proNames); listView.setAdapter(adapter); listView.setOnItemClickListener(pronameItemClick); ppwin = new PopupWindow(view,project.getWidth(),LayoutParams.WRAP_CONTENT); ColorDrawable cd = new ColorDrawable(-0000); ppwin.setBackgroundDrawable(cd); ppwin.setOutsideTouchable(true); ppwin.setTouchable(true); ppwin.setFocusable(true); ppwin.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //如果点击了popwindow之外的地方 if(event.getAction() == MotionEvent.ACTION_OUTSIDE) { ppwin.dismiss(); return true; } return false; } }); } //工程名列表视图点选事件 private AdapterView.OnItemClickListener pronameItemClick = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { PopupAdapter.ViewHoler holder = (PopupAdapter.ViewHoler)view.getTag(); project.setText(holder.getTxtView().getText()); popupWinDismiss(); } }; /** * @description 关闭popupwindow * */ public void popupWinDismiss() { ppwin.dismiss(); } /** * @description 显示项目名称的popupwindow * */ private void popwindowShowing() { ppwin.showAsDropDown(project); }
[/list]