發一下自己寫的幾個Adapter基類
阿新 • • 發佈:2019-01-02
自己今年整理出來的Adapter類,已經實現:分組列表->圖片列表節點、時間節點、連結節點->列表專案的編輯:文字、選擇項、地理位置選擇(高德地圖)、圖片列表選擇。現在在我的Android專案中正常在用。今天只發對應的Adapter類,其中使用的其他類,忙完這幾天,再抽時間整理一下,發個Demo出來。。
第二個類:package com.cssg.android; import java.util.ArrayList; import com.cssg.android.utils.Utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /** * 分組列表顯示介面卡 * 支援載入更多按鈕 * 2015-10-22 14:57 何洪滔 增加未載入資料時的提示 * 2016-02-25 15:36 何洪滔 新增設定分組間隔背景方法,包括設定背景圖片、資源或者顏色 * 2016-06-28 11:56 何洪滔 增加檢視快取方法 * @author cubar * */ public class GroupAdapter extends BaseAdapter { private final String TAG = "GroupAdapter"; /** * 資料列表 */ private ArrayList<Object> data; /** * 佈局管理器 */ protected LayoutInflater inflater = null; /** * 是否顯示載入更多資料按鈕 */ protected boolean _Bln_HasLoadMoreButton = false; /** * 更多按鈕文字 * 預設:載入更多 */ protected String _Str_MoreButton = "載入更多"; /** * 控制元件上下文 */ private Context context; /** * 獲取控制元件的上下文 * @return */ protected Context getContext(){ return context; } /** * 未載入資料前的提示資訊 */ private String _Str_MessageBeforLoad = "暫未載入資料"; /** * 沒有任何資料的提示資訊 */ private String _Str_MessageNoData = "沒有任何資料"; /** * 設定未載入資料前的提示資訊 * @param message * 提示資訊 */ public void setMessageBeforLoad(String message){ _Str_MessageBeforLoad = message; if(message == null || message == ""){ _Str_MessageBeforLoad = "暫未載入資料"; } } /** * 設定沒有資料時的提示資訊 * @param message */ public void setMessageNoData(String message){ _Str_MessageNoData = message; if(message == null || message == ""){ _Str_MessageNoData = "沒有任何資料"; } } /** * 當前是否已經載入資料 */ private boolean _Bln_IsNowLoadData = false; /** * 設定當前是否已經載入資料 * 用於控制顯示列表資料為空時的提示資訊 * @param isload */ public void setIsNowLoadData(boolean isload){ _Bln_IsNowLoadData = isload; } public GroupAdapter(Context context, ArrayList<Object> items){ this.context = context; data = items; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(data == null){ data = new ArrayList<Object>(); } } /** * 快取檢視,2016-07-28 11:56 何洪滔 新增 * @param obj 檢視對應的資料物件 * @param view 要快取的檢視 */ protected void cacheView(Object obj, View view){ if(obj == null || view == null){ return; } } //TODO 2016-02-25 何洪滔 新增設定分組間隔背景方法 private final int GROUP_BACKGROUND_TYPE_NONE = 102; private final int GROUP_BACKGROUND_TYPE_COLOR = 103; private final int GROUP_BACKGROUND_TYPE_DRAWABLE = 104; private final int GROUP_BACKGROUND_TYPE_RESURCE = 105; /** * 分組背景型別 */ private int GROUP_BACKGROUND_TYPE = GROUP_BACKGROUND_TYPE_NONE; private int groupBackgroundColor = 0; private Drawable groupBackgroundDrawable = null; private int groupBackgroundResurceId = 0; public void setGroupSplitBackgroundColor(int color){ GROUP_BACKGROUND_TYPE = GROUP_BACKGROUND_TYPE_COLOR; groupBackgroundColor = color; } public void setGroupSplitBackgroundDrawable(Drawable drawable){ GROUP_BACKGROUND_TYPE = GROUP_BACKGROUND_TYPE_DRAWABLE; groupBackgroundDrawable = drawable; } public void setGroupSplitBackgroundResurce(int resurceId){ GROUP_BACKGROUND_TYPE = GROUP_BACKGROUND_TYPE_RESURCE; groupBackgroundResurceId = resurceId; } /** * 生成一個分組間隔物件 * @return */ public static Object createGroupSplit(String title){ return new GroupListViewSplit(title); } /** * 獲取獲取更多按鈕按下事件 * @return */ public View.OnClickListener getMoreOnClickListener(){ return null; } /** * 獲取指定位置的資料物件 * @param index * @return */ public Object getItemAt(int index){ if(index >=0 && index < data.size()){ return data.get(index); } return null; } /** * 更新指定位置的資料物件 * @param index * @param obj */ public void setItemAt(int index, Object obj){ if(index >=0 && index < data.size()){ data.remove(index); data.add(index, obj); } } /** * 當前位置是否是分組間隔 * @param position * 要判斷的位置索引 * @return */ public boolean isGroupPosition(int position){ Object obj = this.getItemAt(position); if(obj == null){ return false; } if(obj.getClass().equals(GroupListViewSplit.class)){ return true; } return false; } /** * 當前是不是最後一頁 * @return */ protected boolean isEndPage(){ return _Int_NowPage >= _Int_TotalPage; } protected int _Int_NowPage = 1; /** * 獲取當前的頁數 * @return */ public int getNowPage(){ return _Int_NowPage; } /** * 設定當前的頁數 * @param nowPage */ public void setNowPage(int nowPage){ _Int_NowPage = nowPage; } protected int _Int_TotalPage = 1; /** * 獲取當前總頁數 * @return */ public int getTotalPage(){ return _Int_TotalPage; } /** * 設定當前總頁數 * @param totalPage */ public void setTotalPage(int totalPage){ _Int_TotalPage = totalPage; } @Override public int getCount() { // TODO Auto-generated method stub if(data.size() == 0){ //資料列表為空時,要顯示相關提示資訊 return 1; } if(_Bln_HasLoadMoreButton){ return data.size() + 1; } return data.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View vi = convertView; //如果列表資料為空,則顯示提示資訊 if(data.size() == 0){ if(vi == null || vi.findViewById(R.id.tv_comp_listview_no_data_info) == null){ vi = inflater.inflate(R.layout.comp_listview_no_data_info, null); } TextView tvInfo = (TextView)vi.findViewById(R.id.tv_comp_listview_no_data_info); int height = Utils.getDisplayHeight(getContext()); if(parent != null){ height = parent.getMeasuredHeight(); } tvInfo.setHeight(height); //tvInfo.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.MATCH_PARENT)); if(_Bln_IsNowLoadData){//如果已經載入了資料,則顯示沒有資料的提示資訊 tvInfo.setText(_Str_MessageNoData); }else{ tvInfo.setText(_Str_MessageBeforLoad); } return vi; } //顯示最後的載入更多按鈕 if(_Bln_HasLoadMoreButton){ if(position == data.size()){ //最後的顯示為載入更多按鈕 if(vi == null || convertView.findViewById(com.cssg.android.R.id.comp_listview_more_button) == null){ vi = inflater.inflate(com.cssg.android.R.layout.comp_listview_more, null); } Button btn = (Button) vi.findViewById(com.cssg.android.R.id.comp_listview_more_button); if(_Str_MoreButton != ""){ btn.setText(_Str_MoreButton); } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO 載入更多按鈕按下 if(getMoreOnClickListener() != null){ _Int_NowPage ++; if(isEndPage()){ _Int_NowPage --; Toast.makeText(getContext(), "已經達到最後一頁", Toast.LENGTH_SHORT).show(); return; } Button btn = (Button)view; btn.setEnabled(false); btn.setText("正在載入資料…"); getMoreOnClickListener().onClick(view); } } }); if(isEndPage()){ //btn.setEnabled(false); }else{ btn.setEnabled(true); } return vi; } } Object obj = data.get(position); if(obj == null){ return null; } if(obj.getClass().equals(GroupListViewSplit.class)){//當前節點是分組間隔 vi = this.fillViewWithData(obj, convertView); }else{ vi = null; } return vi; } /** * 填充資料到顯示 * @param obj * @param convertView */ protected View fillViewWithData(Object obj, View convertView){ View vi = null; try{ if (vi == null || convertView.findViewById(R.id.comp_listview_group_split_title) == null){ vi = inflater.inflate(R.layout.comp_listview_group_split, null); } GroupListViewSplit s = (GroupListViewSplit)obj; TextView tvTitle = (TextView)vi.findViewById(R.id.comp_listview_group_split_title); if(s.getTitle() != null && s.getTitle() != ""){ tvTitle.setText(s.getTitle()); tvTitle.setVisibility(View.VISIBLE); //修改高度為22dp vi.setLayoutParams(new android.widget.AbsListView.LayoutParams(android.widget.AbsListView.LayoutParams.MATCH_PARENT, (int) (22 * Utils.getSystemDensity(getContext())))); } switch(GROUP_BACKGROUND_TYPE){ case GROUP_BACKGROUND_TYPE_COLOR: tvTitle.setBackgroundColor(groupBackgroundColor); vi.setBackgroundColor(groupBackgroundColor); break; case GROUP_BACKGROUND_TYPE_DRAWABLE: tvTitle.setBackground(groupBackgroundDrawable); vi.setBackground(groupBackgroundDrawable); break; case GROUP_BACKGROUND_TYPE_RESURCE: tvTitle.setBackgroundResource(groupBackgroundResurceId); vi.setBackgroundResource(groupBackgroundResurceId); break; } }catch(Exception e){ Log.e(TAG, "填充資料失敗"); vi = null; } return vi; } }
第三個類package com.cssg.android; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import com.cssg.android.utils.DateTimePickDialogUtil; import com.cssg.android.utils.ImageLoader; import com.cssg.android.utils.Utils; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; /** * 各種節點型別集合介面卡 * 包括:日期節點,圖片列表節點,連結節點 * 2015-10-03 15:42 何洪滔 增加圖片節點 * 2015-10-05 10:23 何洪滔 增加圖片列表節點 * 2015-10-07 15:58 何洪滔 增加連結節點 * 2016-02-24 22:37 何洪滔 增加連結節點的生成方法 * 2016-06-02 15:02 何洪滔 增加連結節點的生成方法 * @author cubar * */ public class UtilsAdapter extends GroupAdapter { protected Context _context; /** * 圖片載入器 */ private ImageLoader imageLoader; public UtilsAdapter(Context context, ArrayList<Object> items) { super(context, items); // TODO Auto-generated constructor stub _context = context; imageLoader = new ImageLoader(context); } /** * 獲取當前設定的日期選擇回撥介面 * @return */ protected DateTimePickDialogUtil.OnDateTimeSelected getOnDateTimeSelectedInterface(){ return null; } /** * 獲取日期節點資料物件 * @param date * 日期節點資料介面 * @return */ public Object createDateViewData(DateView date){ if(date == null){ return null; } return new DateViewClass(date); } /** * 建立圖片節點資料物件 * @param imageview * 圖片節點資料介面 * @return */ public Object createImageViewData(ImageView imageview){ if(imageview == null){ return null; } return new ImageViewClass(imageview); } /** * 建立連結節點資料物件 * @param linkView * 連結節點資料介面 * @return */ public Object createLinkViewData(LinkView linkView){ if(linkView == null){ return null; } return new LinkViewClass(linkView); } /** * 建立連結節點資料物件 * @param title 顯示的標題 * @param summary 顯示的說明文字 * @param args 引數 * @param listener 點選事件 * @return */ public Object createLinkViewData(String title, String summary, String args, View.OnClickListener listener){ return new LinkViewClass(title, summary, args, listener); } /** * 建立連結節點資料物件 * @param title 顯示的標題 * @param summary 顯示的說明文字 * @param args 引數 * @param activity 呼叫的activity * @param pkg 要啟動的activity所在的包 * @param activityFullName 要啟動的activity的名稱,包含包名稱 * @param requestCode 需要回調的引數,0則不需要回調 * @return */ public Object createLinkViewData(String title, String summary, String args, final Activity activity,final String pkg, final String activityFullName, final int requestCode){ View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setComponent(new ComponentName(pkg, activityFullName)); intent.setAction(Intent.ACTION_VIEW); if(requestCode < 0){ activity.startActivity(intent); }else{ activity.startActivityForResult(intent, requestCode); } } }; return new LinkViewClass(title, summary, args, listener); } /** * 建立連結節點資料物件 * @param title 顯示的標題 * @param summary 顯示的說明文字 * @param args 引數 * @param context 呼叫的context * @param pkg 要啟動的activity所在的包 * @param activityFullName 要啟動的activity的名稱,包含包名稱 * @return */ public Object createLinkViewData(String title, String summary, String args, final Context context,final String pkg, final String activityFullName){ View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setComponent(new ComponentName(pkg, activityFullName)); intent.setAction(Intent.ACTION_VIEW); context.startActivity(intent); } }; return new LinkViewClass(title, summary, args, listener); } /** * 建立連結節點資料物件 * @param title 顯示的標題 * @param summary 顯示的說明文字 * @param args 引數 * @param context 呼叫的context * @param cls 要連結的啟動Activity * @return */ public Object createLinkViewData(String title, String summary, String args, final Context context, final Class<?> cls){ return createLinkViewData(title, summary, args, context, cls.getPackage().getName(), cls.getName()); } /** * 建立連結節點資料物件 * @param title 顯示的標題 * @param summary 顯示的說明文字 * @param args 引數 * @param activity 呼叫的activity * @param cls 要連結的啟動Activity * @param requestCode 需要回調的引數,0則不需要回調 * @return */ public Object createLinkViewData(String title, String summary, String args, Activity activity, Class<?> cls, int requestCode){ return createLinkViewData(title, summary, args, activity, cls.getPackage().getName(), cls.getName(), requestCode); } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View vi = convertView; vi = super.getView(position, convertView, parent); if(vi == null){ Object obj = getItemAt(position); if(obj == null){ return null; } if(obj.getClass().equals(DateViewClass.class)){//當前節點是時間資料節點 final DateViewClass d = (DateViewClass)obj; vi = inflater.inflate(R.layout.comp_listview_item_data_view, null); setDateView(vi, d.getSelectedDate()); //點選選擇日期事件 vi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { // TODO Auto-generated method stub Date dtm = d.getSelectedDate(); if(dtm == null){ dtm = new Date(); } DateTimePickDialogUtil util = new DateTimePickDialogUtil(getContext(), inflater, dtm); util.dateTimePicKDialog(new DateTimePickDialogUtil.OnDateTimeSelected() { @Override public void OnSelected(final Date date) { // TODO Auto-generated method stub if(getOnDateTimeSelectedInterface() != null){ getOnDateTimeSelectedInterface().OnSelected(date); } setDateView(view, date); setItemAt(position, new DateViewClass(new DateView(){ @Override public Date getSelectedDate() { // TODO Auto-generated method stub return date; }})); } }); } }); }else if(obj.getClass().equals(ImageViewClass.class)){ //當前節點是圖片節點 int w, h;//圖片顯示的寬度 //計算圖片大小 int sw = Utils.getDisplayWidth(getContext()); ImageViewClass ivc = (ImageViewClass)obj; w = (int) ((sw - (2 + ivc.getImageCount() - 1) * 10 * Utils.getSystemDensity(getContext())) / ivc.getImageCount()); h = w; vi = inflater.inflate(R.layout.comp_listview_item_images_view, null); LinearLayout ll = (LinearLayout)vi.findViewById(R.id.ll_comp_listview_item_images_view); for(int i = 0 ; i < ivc.getImageUrl().length; i ++){ if(i > 0){//增加圖片間隔 android.view.View sv = new android.view.View(getContext()); sv.setLayoutParams(new LayoutParams((int) (10 * Utils.getSystemDensity(getContext())),LayoutParams.MATCH_PARENT)); ll.addView(sv); } android.widget.ImageView iv = new android.widget.ImageView(getContext()); iv.setLayoutParams(new LayoutParams(w, h)); ll.addView(iv); imageLoader.DisplayImage(ivc.getImageUrl()[i], iv); } }else if(obj.getClass().equals(LinkViewClass.class)){ //當前節點是連結節點 vi = inflater.inflate(R.layout.comp_listview_item_link_view, null); LinkViewClass lvc = (LinkViewClass)obj; TextView tvTitle = (TextView)vi.findViewById(R.id.tv_comp_listview_item_link_view_title); TextView tvSummary = (TextView)vi.findViewById(R.id.tv_comp_listview_item_link_view_summary); tvTitle.setText(lvc.getText()); tvSummary.setText(lvc.getTextSummary()); if(lvc.getOnClickListener() != null){ vi.setTag(lvc.getArgs()); vi.setOnClickListener(lvc.getOnClickListener()); } }else { vi = null; } cacheView(obj, vi); } return vi; } private void setDateView(View view, Date date){ if(date == null){ date = new Date(); } Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); c.setTime(date); //mYear = String.valueOf(c.get(Calendar.YEAR)); // 獲取當前年份 //mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份 //mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當前月份的日期號碼 //return mYear + "年" + mMonth + "月" + mDay+"日"+"/星期"+mWay; TextView tvY = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_year); tvY.setText(String.valueOf(c.get(Calendar.YEAR))); TextView tvM = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_month); tvM.setText(String.valueOf(c.get(Calendar.MONTH) + 1)); TextView tvD = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_day); tvD.setText(String.valueOf(c.get(Calendar.DAY_OF_MONTH))); TextView tvH = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_hour); tvH.setText(String.valueOf(c.get(Calendar.HOUR_OF_DAY))); TextView tvMi = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_minite); tvMi.setText(String.valueOf(c.get(Calendar.MINUTE))); //獲取星期 String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); if("1".equals(mWay)){ mWay ="天"; }else if("2".equals(mWay)){ mWay ="一"; }else if("3".equals(mWay)){ mWay ="二"; }else if("4".equals(mWay)){ mWay ="三"; }else if("5".equals(mWay)){ mWay ="四"; }else if("6".equals(mWay)){ mWay ="五"; }else if("7".equals(mWay)){ mWay ="六"; } TextView tvWD = (TextView)view.findViewById(R.id.tv_comp_listview_item_date_view_weekday); tvWD.setText("星期" + mWay); } //TODO 相關列表節點介面定義 //TODO 日期節點段介面定義 /** * 日期節點資料介面 * @author cubar * */ public interface DateView{ /** * 獲取當前選擇的時間 * @return */ Date getSelectedDate(); } /** * 日期節點內部類 * @author cubar * */ protected class DateViewClass{ private DateView i; /** * 獲取當前選擇的時間 * @return */ Date getSelectedDate(){ return i.getSelectedDate(); } public DateViewClass(DateView dv){ i = dv; } } //TODO 圖片節點介面定義 /** * 圖片節點資料介面 * 僅顯示橫向列表 * @author cubar * */ public interface ImageView{ /** * 圖片地址列表分隔符 */ public final static String IMAGE_SPLIT = ";"; /** * 獲取當前圖片的地址列表 * @return */ String[] getImageUrls(); /** * 獲取當前要顯示的圖片數量 * @return */ int getImageCount(); } /** * 圖片節點資料內部類 * @author cubar * */ protected class ImageViewClass{ private ImageView v; /** * 獲取當前圖片的地址 * @return */ public String[] getImageUrl(){ if(v == null){ return null; } return v.getImageUrls(); } /** * 獲取當前要顯示的圖片數量 * @return */ int getImageCount(){ if(v == null){ return 0; } return v.getImageCount(); } public ImageViewClass(ImageView iv){ v = iv; } } //TODO 連結節點資料介面 /** * 連結節點資料介面 * @author cubar * */ public interface LinkView{ /** * 獲取連結主文字 * @return */ String getText(); /** * 獲取說明文字 * @return */ String getTextSummary(); /** * 獲取引數 * @return */ String getArgs(); /** * 獲取點選事件 * @return */ View.OnClickListener getOnClickListener(); } /** * 連結節點資料內部類 * @author cubar * */ protected class LinkViewClass{ private LinkView lv; private String text; private String summary; private String args; private View.OnClickListener listener; /** * 獲取連結主文字 * @return */ String getText(){ if(lv != null){ return lv.getText(); } return text; } /** * 獲取說明文字 * @return */ String getTextSummary(){ if(lv != null){ return lv.getTextSummary(); } return summary; } /** * 獲取引數 * @return */ String getArgs(){ if(lv != null){ return lv.getArgs(); } return args; } /** * 獲取點選事件 * @return */ View.OnClickListener getOnClickListener(){ if(lv != null){ return lv.getOnClickListener(); } return listener; } public LinkViewClass(LinkView linkView){ lv = linkView; } public LinkViewClass(String txt, String smy, String arg, View.OnClickListener lstner){ text = txt; summary = smy; args = arg; listener = lstner; lv = null; } } }
package com.cssg.android; import java.util.ArrayList; import java.util.Date; import com.cssg.android.photo.ImageSelectViewActivity; import com.cssg.android.utils.DatePickDialogUtil; import com.cssg.android.utils.Utils; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Gallery; import android.widget.Toast; import android.widget.TextView; /** * 支援列表專案內容編輯的集合介面卡 * 2016-07-28 11:42 何洪滔 增加新版本的日期節點功能和相關方法 * 2016-07-29 20:23 何洪滔 增加圖片選擇節點功能和相關方法,此方法需要使用EditAdapter(Activity activity, ArrayList<Object> items)進行初始化 * 2016-08-30 19:23 何洪滔 增加跳轉Activity選擇節點方法 * * @author cubar * */ @SuppressWarnings("deprecation") public class EditAdapter extends UtilsAdapter { private Activity _activity; /** * 是否能編輯標識 */ private boolean _Bln_Editable = true; /** * 獲取是否能編輯 * @return */ protected boolean getEditable(){ return _Bln_Editable; } /** * 設定是否能編輯 * @param enable */ public void setEditable(boolean enable){ _Bln_Editable = enable; } public EditAdapter(Context context, ArrayList<Object> items) { super(context, items); // TODO Auto-generated constructor stub } public EditAdapter(Activity activity, ArrayList<Object> items){ super(activity, items); _activity = activity; } /** * 建立可編輯連結項 * * @param title * 標題 * @param content * 內容 * @param mask * 提示水印 * @return */ public Object createEditLinkView(String title, String content, String mask) { return createEditLinkView(title, content, mask, false); } /** * 建立可編輯連結項 * * @param title * 標題 * @param content * 內容 * @param mask * 提示水印 * @param newedit * 是否在新視窗中編輯 * @return */ public Object createEditLinkView(String title, String content, String mask, boolean newedit) { return new EditLinkView(title, content, mask, newedit); } /** * 建立可編輯連結項 * * @param title * 標題 * @param content * 內容 * @param mask * 提示水印 * @param newedit * 是否在新視窗中編輯 * @param editable * 是否可編輯 * @return */ public Object createEditLinkView(String title, String content, String mask, boolean newedit, boolean editable) { EditLinkView res = new EditLinkView(title, content, mask, newedit); res.setClickable(editable); return res; } /** * 建立可編輯連結項 * * @param title * 標題 * @param content * 內容 * @param mask * 提示水印 * @param newedit * 是否在新視窗中編輯 * @param editable * 是否可編輯 * @param set * 賦值介面 * @return */ public Object createEditLinkView(String title, String content, String mask, boolean newedit, boolean editable, OnSetDone set) { EditLinkView res = new EditLinkView(title, content, mask, newedit, set); res.setClickable(editable); return res; } /** * 建立按鈕項 * * @param text * 按鈕名稱 * @param listener * 點選事件 * @return */ public Object createButtonView(String text, View.OnClickListener listener) { return new ButtonView(text, listener); } /** * 建立列表選擇頂 * * @param title * 標題 * @param items * 選擇內容 * @param completed * 選擇完成回撥介面 * @return */ public Object createSelectView(View root, String title, ArrayList<? extends ISelectItem> items, OnSelectCompleted completed) { return createSelectView(root, title, items, "", completed); } /** * 建立列表選擇頂 * * @param title * 標題 * @param items * 選擇內容 * @param selectId * 預設選擇的值 * @param completed * 選擇完成回撥介面 * @return */ public Object createSelectView(View root, String title, ArrayList<? extends ISelectItem> items, String selectId, OnSelectCompleted completed) { SelectView view = new SelectView(title, items, selectId, completed); view.setRootView(root); return view; } /** * 建立列表選擇頂 * @param activity 上下文檢視 * @param pkg 要啟動的檢視所屬包名 * @param compname 元件名 * @param resultCode 返回標識 * @param title 標題 * @param vlaue 要顯示的內容 * @param mask 提示資訊 * @param argName 跳轉引數名稱 * @param args 引數 * @return */ public Object createSelectView(Activity activity, String pkg, String compname, int resultCode, String title, String value, String mask, String argName, String args){ SelectView view = new SelectView(title, null, "", null); view.setActivity(activity); view.setPakageName(pkg); view.setComponentName(compname); view.setResultCode(resultCode); view.setArgName(argName); view.setArgs(args); view.setMask(mask); view.setValue(value); return view; } /** * 獲取日期節點資料物件 * * @param title * 標題 * @param date * 日期值 * @param completed * 選擇完成回撥介面 * @return */ public Object createDateViewData(String title, Date date, OnDateSelectCompleted completed) { return new DateViewV2(title, date, completed); } /** * 獲取圖片選擇節點資料物件,使用此方法,需要Activity實現On * * @param title * 標題 * @param images * 已經選擇的圖片列表,使用“;”間隔 * @param completed * 選擇完成回撥介面 * @return */ public Object createImageSelectView(String title, String images, ImageGalleryAdapter adapter, OnImageSelectCompleted completed) { return new ImageSelectView(title, images, adapter, completed); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = super.getView(position, convertView, parent); if (view != null) { return view; } Object obj = this.getItemAt(position); if (obj != null) { if (obj.getClass().equals(EditLinkView.class)) { final EditLinkView cls = (EditLinkView) obj; TextWatcher watcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub // Log.i("EditAdapter", "編輯改變:" + s); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub // Log.i("EditAdapter", "編輯改變開始:" + s); } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub // Log.i("EditAdapter", "編輯改變結束:" + s.toString()); if (cls.getOnSetDone() != null) { cls.getOnSetDone().Set(s.toString()); } cls.setContent(s.toString()); } }; view = this.inflater.inflate(R.layout.comp_listview_editlink, null); TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); tvTitle.setText(cls.getTitle()); TextView tvContent = (TextView) view .findViewById(R.id.tv_content); if(cls.getContent() != "" && cls.getContent() != null){ tvContent.setText(cls.getContent()); }else{ tvContent.setText(cls.getMask()); } EditText etContent = (EditText) view .findViewById(R.id.et_content); etContent.setText(cls.getContent()); etContent.setHint(cls.getMask()); etContent.addTextChangedListener(watcher); android.widget.ImageView iv = (android.widget.ImageView) view .findViewById(R.id.iv_linksign); if(!getEditable()){ etContent.setVisibility(View.INVISIBLE); tvContent.setVisibility(View.VISIBLE); iv.setVisibility(View.INVISIBLE); }else{ if (!cls.getClickable()) { // 不支援點選事件,只顯示TextView etContent.setVisibility(View.INVISIBLE); tvContent.setVisibility(View.VISIBLE); iv.setVisibility(View.INVISIBLE); } else { if (cls.getIsNewEditAct()) { iv.setVisibility(View.VISIBLE); // 需要跳轉到編輯介面 etContent.setVisibility(View.INVISIBLE); tvContent.setVisibility(View.VISIBLE); // 新增跳轉事件 } else { iv.setVisibility(View.INVISIBLE); // 直接在本介面編輯 etContent.setVisibility(View.VISIBLE); tvContent.setVisibility(View.INVISIBLE); } } } } else if (obj.getClass().equals(ButtonView.class)) { // 按鈕項 ButtonView bv = (ButtonView) obj; view = inflater.inflate(R.layout.comp_listview_button, null); Button btn = (Button) view.findViewById(R.id.btn_Button); btn.setText(bv.getButtonText()); btn.setOnClickListener(bv.getListener()); if(!getEditable()){ btn.setEnabled(getEditable()); } } else if (obj.getClass().equals(SelectView.class)) { // 選擇項 final SelectView sv = (SelectView) obj; view = this.inflater.inflate(R.layout.comp_listview_select, null); TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); tvTitle.setText(sv.getTitle()); final TextView tvContent = (TextView) view .findViewById(R.id.tv_content); tvContent.setText(sv.getContent()); // android.widget.ImageView iv = // (android.widget.ImageView)view.findViewById(R.id.iv_linksign); if(getEditable()){ // 新增點選事件 tvContent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(sv.getComponentName() == "" || sv.getComponentName() == null){ // 例項化SelectPicPopupWindow SelectItemPopupWindow menuWindow = new SelectItemPopupWindow( getContext(), sv.getSelects(), new OnSelectCompleted() { @Override public void OnSelect(ISelectItem si) { // TODO Auto-generated method stub tvContent.setText(si.getName()); if (sv.getSelectCompleted() != null) { sv.getSelectCompleted().OnSelect(si); } } }); // 顯示視窗 // menuWindow.showAtLocation(getContext().findViewById(R.id.ll_content), // Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); // 設定layout在PopupWindow中顯示的位置 menuWindow.showAtLocation(sv.getRootView(), Gravity.CENTER | Gravity.BOTTOM, 0, 0); }else{ if(sv.getActivity() != null){ //跳轉至指定的Activity Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setComponent(new ComponentName(sv.getPakageName(), sv.getComponentName())); Bundle bund = new Bundle(); bund.putString(sv.getArgName(), sv.getArgs()); intent.putExtras(bund); sv.getActivity().startActivityForResult(intent, sv.getResultCode()); } } } }); } } else if (obj.getClass().equals(DateViewV2.class)) {// 當前介面是日期節點 final DateViewV2 d = (DateViewV2) obj; view = inflater.inflate(R.layout.comp_listview_date, null); // setDateView(view, d.getDate()); TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); tvTitle.setText(d.getTitle()); /* * Calendar c = Calendar.getInstance(); * c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); * c.setTime(d.getDate()); //mYear = * String.valueOf(c.get(Calendar.YEAR)); // 獲取當前年份 //mMonth = * String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份 //mDay = * String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當前月份的日期號碼 * //return mYear + "年" + mMonth + "月" + mDay+"日"+"/星期"+mWay; * String str = String.valueOf(c.get(Calendar.YEAR)) + "年" + * String.valueOf(c.get(Calendar.MONTH) + 1) + "月" + * String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "日"; */ final TextView tvContent = (TextView) view .findViewById(R.id.tv_content); tvContent.setText(Utils.stringFromDate("yyyy年MM月dd日", d.getDate())); if(getEditable()){ // 點選選擇日期事件 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { // TODO Auto-generated method stub Date dtm = d.getDate(); if (dtm == null) { dtm = new Date(); } DatePickDialogUtil util = new DatePickDialogUtil( getContext(), inflater, dtm); util.dateTimePicKDialog(new DatePickDialogUtil.OnDateTimeSelected() { @Override public void OnSelected(final Date date) { // TODO Auto-generated method stub if (d.getSelectCompleted() != null) { d.getSelectCompleted().OnDateSelect(date); } // setDateView(view, date); tvContent.setText(Utils.stringFromDate( "yyyy年MM月dd日", date)); d.setDate(date); } }); } }); } } else if (obj.getClass().equals(ImageSelectView.class)) { // 為圖片選擇項 final ImageSelectView cls = (ImageSelectView) obj; view = inflater.inflate(R.layout.comp_listview_imageselect, null); TextView tvTitle = (TextView) view.findViewById(R.id.tv_title); tvTitle.setText(cls.getTitle()); TextView tvContent = (TextView) view .findViewById(R.id.tv_content); tvContent.setText("點選選擇圖片"); Gallery gl = (Gallery) view.findViewById(R.id.gly_images); gl.setAdapter(cls.getAdapter()); if (cls.getImages() == null || cls.getImages().length() < 1) { tvContent.setVisibility(View.VISIBLE); gl.setVisibility(View.INVISIBLE); } else { tvContent.setVisibility(View.INVISIBLE); gl.setVisibility(View.VISIBLE); } _listImageSelectHoler.add(new ImageSelectHolder(tvContent, gl, cls)); if(getEditable()){ // 專案的點選事件 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(_activity != null){ Intent intent = new Intent(); //intent.setComponent(new ComponentName("com.cssg.android", ImageSelectViewActivity.class.getName())); intent.setClass(_context, ImageSelectViewActivity.class); Bundle bund = new Bundle(); bund.putString(ImageSelectViewActivity.STR_ARG_IMAGES, cls.getImages()); intent.putExtras(bund); _activity.startActivityForResult(intent, ImageSelectViewActivity.RESULT_BACK_SIGN); }else{ Toast.makeText(_context, "沒有使用Activity進行初始化控制元件!", Toast.LENGTH_SHORT).show(); } } }); } } } cacheView(obj, view); return view; } private ArrayList<ImageSelectHolder> _listImageSelectHoler = new ArrayList<ImageSelectHolder>(); /** * 圖片選擇顯示控制元件容器 * @author cubar * */ protected class ImageSelectHolder{ private TextView _sum; private Gallery _gal; private ImageSelectView _isv; protected TextView getTextView(){ return _sum; } protected Gallery getGallery(){ return _gal; } protected ImageSelectView getView(){ return _isv; } public ImageSelectHolder(TextView tv, Gallery gal, ImageSelectView view){ _sum = tv; _gal = gal; _isv = view; } } /** * 通知圖片已經選擇 */ public void notifImageSelected(String images){ for(int i = 0 ; i < _listImageSelectHoler.size(); i ++){ if(images.length() > 0){ _listImageSelectHoler.get(i).getTextView().setVisibility(View.INVISIBLE); _listImageSelectHoler.get(i).getGallery().setVisibility(View.VISIBLE); }else{ _listImageSelectHoler.get(i).getTextView().setVisibility(View.VISIBLE); _listImageSelectHoler.get(i).getGallery().setVisibility(View.INVISIBLE); } try{ _listImageSelectHoler.get(i).getView().setImages(images); }catch(Exception e){ Log.e("EditAdapter", "異常:" + e.getMessage()); } } } /** * 可編輯的連結專案 內部類 可設定直接編輯還是連結到一個文字編輯頁編輯 * * @author cubar * */ protected class EditLinkView { private String _title; private String _content; private String _mask; private boolean _Bln_newEditAct = false; // private View.OnClickListener _listener = null; private boolean _Bln_Clickable = true; private OnSetDone _onSetDone; public EditLinkView() { } public EditLinkView(String title, String content, String mask, boolean newedit) { _title = title; _content = content; _mask = mask; _Bln_newEditAct = newedit; } public EditLinkView(String title, String content, String mask, boolean newedit, OnSetDone set) { this(title, content, mask, newedit); /* * _title = title; _content = content; _mask = mask; _Bln_newEditAct * = newedit; */ _onSetDone = set; } /** * 獲取連結顯示標題 * * @return */ protected String getTitle() { return _title; } /** * 設定連結顯示標題 * * @param title */ protected void setTitle(String title) { _title = title; } /** * 獲取連結顯示內容 * * @return */ protected String getContent() { return _content; } /** * 設定連結顯示內容 * * @param content */ protected void setContent(String content) { _content = content; } /** * 獲取水印提示資訊 * * @return */ protected String getMask() { return _mask; } /** * 設定水印提示資訊 * * @param mask */ protected void setMask(String mask) { _mask = mask; } /** * 獲取是否在新編輯介面編輯 * * @return */ protected boolean getIsNewEditAct() { return _Bln_newEditAct; } /** * 設定是否在新編輯介面編輯 * * @param isneweditact */ protected void setIsNewEditAct(boolean isneweditact) { _Bln_newEditAct = isneweditact; } /** * 獲取點選事件 * * @return */ /* * protected View.OnClickListener getListener(){ return _listener; } */ /** * 設定點選事件 * * @param listener */ /* * protected void setClickListener(View.OnClickListener listener){ * _listener = listener; } */ /** * 獲取是否可點選 * * @return */ protected boolean getClickable() { return _Bln_Clickable; } /** * 設定是否可點選 * * @param enable */ protected void setClickable(boolean enable) { _Bln_Clickable = enable; } /** * 獲取賦值介面 * * @return */ protected OnSetDone getOnSetDone() { return _onSetDone; } /** * 設定賦值介面 * * @param set */ protected void setOnSetDone(OnSetDone set) { _onSetDone = set; } } /** * 設定完成賦值介面 * * @author cubar * */ public interface OnSetDone { /** * 設定值 * * @param value * 值,字串 */ void Set(String value); } /** * 按鈕列表項 * * @author cubar * */ public class ButtonView { private String _Str_text; private View.OnClickListener _listener; /** * 獲取按鈕字元 * * @return */ protected String getButtonText() { return _Str_text; } /** * 設定按鈕字元 * * @param text */ protected void setButtonText(String text) { _Str_text = text; } /** * 獲取按鈕事件 * * @return */ protected View.OnClickListener getListener() { return _listener; } /** * 設定按鈕事件 * * @param listener */ protected void setListener(View.OnClickListener listener) { _listener = listener; } public ButtonView() { } public ButtonView(String text, View.OnClickListener listener) { _Str_text = text; _listener = listener; } } //TODO 選擇列表項 /** * 選擇列表項 * 2016-08-30 19:23 何洪滔 增加跳轉Activity選擇列表的方法 * * @author cubar * */ public class SelectView { private View _rootView; private String _title; private ArrayList<? extends ISelectItem> _lst_select; private String _selectId = ""; private String pkgname = ""; private String componentname = ""; private Activity _activity = null; private int _resultCode = -1; private String _mask = ""; private String _value = ""; private String _argName = ""; private String _args = ""; /** * 獲取包名 * @return */ protected String getPakageName(){ return pkgname; } /** * 設定包名 * @param name */ protected void setPakageName(String name){ pkgname = name; } /** * 獲取元件名稱 * @return */ protected String getComponentName(){ return componentname; } /** * 設定元件名稱 * @param name */ protected void setComponentName(String name){ componentname = name; } /** * 獲取當前的Activity * @return */ protected Activity getActivity(){ return _activity; } /** * 設定當前的Activity * @param activity */ protected void setActivity(Activity activity){ _activity = activity; } /** * 獲取返回標識 * @return */ protected int getResultCode(){ return _resultCode; } /** * 設定返回標識 * @param code */ protected void setResultCode(int code){ _resultCode = code; } /** * 獲取Mask字串 * @return */ protected String getMask(){ return _mask; } /** * 設定Mask字串 * @param mask */ protected void setMask(String mask){ _mask = mask; } /** * 獲取顯示的內容 * @return */ protected String getValue(){ return _value; } /** * 設定顯示的內容 * @param value */ protected void setValue(String value){ _value = value; } /** * 獲取跳轉Activity接受的引數名稱 * @return */ protected String getArgName(){ return _argName; } /** * 設定跳轉Activity接受的引數名稱 * @param name */ protected void setArgName(String name){ _argName = name; } /** * 獲取傳送至跳轉Activity的引數 * @return */ protected String getArgs(){ return _args; } /** * 設定傳送至跳轉Activity的引數 * @param args */ protected void setArgs(String args){ _args = args; } /** * 獲取根檢視供彈出視窗定位 * * @return */ protected View getRootView() { return _rootView; } /** * 設定根檢視供彈出視窗定位 * * @param root */ public void setRootView(View root) { _rootView = root; } /** * 獲取標題 * * @return */ protected String getTitle() { return _title; } /** * 設定標題 * * @param title */ public void setTitle(String title) { _title = title; } /** * 獲取顯示在選擇器中的內容 * * @return */ protected String getContent() { if (_lst_select != null && _lst_select.size() > 0) { try { ISelectViewItem svi; for (int i = 0; i < _lst_select.size(); i++) { svi = (ISelectViewItem) _lst_select.get(i); if (svi.getID().equals(_selectId) || svi.getName().equals(_selectId)) { return svi.getName(); } } svi = (ISelectViewItem) _lst_select.get(0); //設定成預設 if(getSelectCompleted() != null){ getSelectCompleted().OnSelect(svi); } _selectId = svi.getID(); return svi.getName(); } catch (Exception e) { Log.e("EditAdapter", "選擇項介面化異常:" + e.getMessage()); } }else if(_value != "" && _value != null){ return _value; }else if(_mask != ""){ return _mask; } return "請選擇"; } /** * 設定選擇項內容列表 * * @param items */ public void setSelects(ArrayList<? extends ISelectItem> items) { _lst_select = items; } /** * 獲取選擇內容列表 * * @return */ protected ArrayList<? extends ISelectItem> getSelects() { return _lst_select; } protected String getSelectedID() { return _selectId; } /** * 設定當前已經選擇的項 * * @param id */ public void setSelectedID(String id) { _selectId = id; } private OnSelectCompleted _completed; /** * 獲取選擇完成回撥介面 * * @return */ protected OnSelectCompleted getSelectCompleted() { return _completed; } /** * 設定選擇完成回撥介面 * * @param completed */ public void setSelectCompleted(OnSelectCompleted completed) { _completed = completed; } public SelectView() { } /** * 初始化選擇項列表 * * @param title * 標題 * @param items * 選擇的列表,對應的類要實現ISelectViewItem介面 */ public SelectView(String title, ArrayList<? extends ISelectItem> items) { _title = title; _lst_select = items; } /** * 初始化選擇項列表 * * @param title * 標題 * @param items * 選擇的列表,對應的類要實現ISelectViewItem介面 * @param selectedId * 預設選擇的項的值 */ public SelectView(String title, ArrayList<? extends ISelectItem> items, String selectedId) { _title = title; _lst_select = items; _selectId = selectedId; } /** * 初始化選擇項列表 * * @param title * 標題 * @param items * 選擇的列表,對應的類要實現ISelectViewItem介面 * @param selectedId * 預設選擇的項的值 * @param completed * 選擇完成回撥介面 */ public SelectView(String title, ArrayList<? extends ISelectItem> items, String selectedId, OnSelectCompleted completed) { _title = title; _lst_select = items; _selectId = selectedId; _completed = completed; } } /** * 選擇專案資料介面 * * @author cubar * */ public interface ISelectViewItem extends ISelectItem { /** * 獲取選擇項的值 * * @return */ String getID(); /** * 獲取選擇項顯示的名稱 * * @return */ String getName(); } /** * 選擇完成事件介面 * * @author cubar * */ public interface OnSelectCompleted extends OnItemSelectCompleted { } // TODO 2016-07-28 11:42 何洪滔 增加新版本的日期節點類及介面定義 /** * 日期節點版本2 * * @author cubar * */ protected class DateViewV2 { private String _title; private Date _date; protected String getTitle() { return _title; } /** * 獲取日期 * * @return */ public Date getDate() { return _date; } /** * 設定日期 * * @param date */ protected void setDate(Date date) { _date = date; } private OnDateSelectCompleted _completed; /** * 獲取選擇完成呼叫介面 * * @return */ protected OnDateSelectCompleted getSelectCompleted() { return _completed; } public DateViewV2(String title, Date date, OnDateSelectCompleted completed) { _title = title; _date = date; _completed = completed; } } /** * 日期節點選擇完成介面 * * @author cubar * */ public interface OnDateSelectCompleted { /** * 日期選擇 * * @param date */ void OnDateSelect(Date date); } // TODO 2016-07-29 20:23 何洪滔 新增圖片選擇節點類及介面 protected class ImageSelectView { private String _title; private String _images; private OnImageSelectCompleted _completed; private ImageGalleryAdapter _adt; /** * 獲取標題 * * @return */ protected String getTitle() { return _title; } /** * 獲取圖片列表 * * @return */ protected String getImages() { return _images; } /** * 設定圖片列表 * @param images */ protected void setImages(String images){ _images = images; } /** * 獲取圖片資料介面卡 * @return */ protected ImageGalleryAdapter getAdapter(){ return _adt; } /** * 獲取選擇回撥介面 * * @return */ protected OnImageSelectCompleted getSelectCompleted() { return _completed; } public ImageSelectView(String title, String images, ImageGalleryAdapter adapter, OnImageSelectCompleted completed) { _title = title; _images = images; _adt = adapter; _completed = completed; } } /** * 圖片選擇完成回撥介面 * * @author cubar * */ public interface OnImageSelectCompleted { /** * 選擇到的所有圖片列表,圖片檔案地址,使用";"間隔 * * @param images */ void OnImageSelect(String images); } }