編輯:關於Android編程
這個demo集合了自定義對話框,進度條,下拉刷新以及popup彈出框等。是學習了網上開源項目後,抽取集合了常用對話框,進度條,下拉刷新以及popup彈出框等。現在結構目錄簡單,更易於學習和擴展使用。注釋都卸載代碼。下面進行簡單的介紹以及部分代碼展示。
本文demo下載:點擊
這上面項目結構圖也是一目了然,有什麼功能展示。大家也看到了,這上面類有點多,如果全部貼出來,不大可能,有興趣下載本文源碼。
我就貼一下基礎類,還有實現類。因為這些一段段代碼易於其他項目使用。
package com.org.base; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.example.activity.R; import com.example.activity.R.layout; import com.org.dialog.LoadingDialog; import com.org.view.HandyTextView; import android.os.AsyncTask; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; import android.app.Activity; import android.content.Intent; public class BaseActivity extends Activity { protected LoadingDialog mLoadingDialog; /** * 屏幕的寬度、高度、密度 */ protected int mScreenWidth; protected int mScreenHeight; protected float mDensity; //異步加載任務的list protected List> mAsyncTasks = new ArrayList>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLoadingDialog = new LoadingDialog(this, 請求提交中); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); mScreenWidth = metric.widthPixels; mScreenHeight = metric.heightPixels; mDensity = metric.density; } protected void putAsyncTask(AsyncTaskasyncTask) { mAsyncTasks.add(asyncTask.execute()); } protected void clearAsyncTask() { Iterator> iterator = mAsyncTasks .iterator(); while (iterator.hasNext()) { AsyncTask asyncTask = iterator.next(); if (asyncTask != null && !asyncTask.isCancelled()) { asyncTask.cancel(true); } } mAsyncTasks.clear(); } protected void showLoadingDialog(String text) { if (text != null) { mLoadingDialog.setText(text); } mLoadingDialog.show(); } protected void dismissLoadingDialog() { if (mLoadingDialog.isShowing()) { mLoadingDialog.dismiss(); } } /** 短暫顯示Toast提示(來自res) **/ protected void showShortToast(int resId) { Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show(); } /** 短暫顯示Toast提示(來自String) **/ protected void showShortToast(String text) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } /** 長時間顯示Toast提示(來自res) **/ protected void showLongToast(int resId) { Toast.makeText(this, getString(resId), Toast.LENGTH_LONG).show(); } /** 長時間顯示Toast提示(來自String) **/ protected void showLongToast(String text) { Toast.makeText(this, text, Toast.LENGTH_LONG).show(); } /** 顯示自定義Toast提示(來自res) **/ protected void showCustomToast(int resId) { View toastRoot = LayoutInflater.from(BaseActivity.this).inflate( R.layout.common_toast, null); ((HandyTextView) toastRoot.findViewById(R.id.toast_text)) .setText(getString(resId)); Toast toast = new Toast(BaseActivity.this); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(toastRoot); toast.show(); } /** 顯示自定義Toast提示(來自String) **/ protected void showCustomToast(String text) { View toastRoot = LayoutInflater.from(BaseActivity.this).inflate( R.layout.common_toast, null); ((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(text); Toast toast = new Toast(BaseActivity.this); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(toastRoot); toast.show(); } /** Debug輸出Log日志 **/ protected void showLogDebug(String tag, String msg) { Log.d(tag, msg); } /** Error輸出Log日志 **/ protected void showLogError(String tag, String msg) { Log.e(tag, msg); } /** 通過Class跳轉界面 **/ protected void startActivity(Class cls) { startActivity(cls, null); } /** 含有Bundle通過Class跳轉界面 **/ protected void startActivity(Class cls, Bundle bundle) { Intent intent = new Intent(); intent.setClass(this, cls); if (bundle != null) { intent.putExtras(bundle); } startActivity(intent); } /** 默認退出 **/ protected void defaultFinish() { super.finish(); } }
package com.org.base; import android.annotation.TargetApi; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Build; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.widget.PopupWindow; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public abstract class BasePopupWindow extends PopupWindow { protected View mContentView; protected onSubmitClickListener mOnSubmitClickListener; public BasePopupWindow() { super(); } public BasePopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public BasePopupWindow(Context context, AttributeSet attrs) { super(context, attrs); } public BasePopupWindow(Context context) { super(context); } public BasePopupWindow(int width, int height) { super(width, height); } public BasePopupWindow(View contentView, int width, int height, boolean focusable) { super(contentView, width, height, focusable); } public BasePopupWindow(View contentView) { super(contentView); } @SuppressWarnings(deprecation) public BasePopupWindow(View contentView, int width, int height) { super(contentView, width, height, true); mContentView = contentView; setBackgroundDrawable(new BitmapDrawable()); initViews(); initEvents(); init(); } public abstract void initViews(); public abstract void initEvents(); public abstract void init(); public View findViewById(int id) { return mContentView.findViewById(id); } /** * 顯示在parent的上部並水平居中 * * @param parent */ public void showViewTopCenter(View parent) { showAtLocation(parent, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); } /** * 顯示在parent的中心 * * @param parent */ public void showViewCenter(View parent) { showAtLocation(parent, Gravity.CENTER, 0, 0); } /** * 添加確認單擊監聽 * * @param l */ public void setOnSubmitClickListener(onSubmitClickListener l) { mOnSubmitClickListener = l; } public interface onSubmitClickListener { void onClick(); } }
package com.org.base; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; //基本的適配器 public class BaseArrayListAdapter extends BaseAdapter { protected Context mContext; protected LayoutInflater mInflater; protected ListmDatas = new ArrayList (); //String數組添加數據源 public BaseArrayListAdapter(Context context, String... datas) { mContext = context; mInflater = LayoutInflater.from(context); if (datas != null && datas.length > 0) { mDatas = Arrays.asList(datas); } } //用list方式添加數據源 public BaseArrayListAdapter(Context context, List datas) { mContext = context; mInflater = LayoutInflater.from(context); if (datas != null && datas.size() > 0) { mDatas = datas; } } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { return null; } }
package com.example.activity; import com.org.base.BaseActivity; import com.org.dialog.EditTextDialog; import com.org.dialog.SimpleListDialog; import com.org.dialog.SimpleListDialog.onSimpleListItemClickListener; import com.org.popupwindow.ChatPopupWindow; import com.org.popupwindow.ChatPopupWindow.onChatPopupItemClickListener; import com.viw.adapt.CheckListDialogAdapter; import android.R.layout; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends BaseActivity implements OnClickListener, onChatPopupItemClickListener{ protected SimpleListDialog mDialog; private int mCheckId = 0; private ChatPopupWindow mChatPopupWindow; private int mWidth; private int mHeaderHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); findViewById(R.id.button5).setOnClickListener(this); initPopupWindow(); } public void onSimpleListItem() { String[] modes = new String[]{測試一,測試二,測試三}; mDialog = new SimpleListDialog(this); mDialog.setTitle(單選框); mDialog.setTitleLineVisibility(View.GONE); mDialog.setAdapter(new CheckListDialogAdapter(mCheckId, this, modes)); //用匿名內部類實現接口監聽 mDialog.setOnSimpleListItemClickListener(new DialogItemClickListener()); mDialog.show(); } protected class DialogItemClickListener implements onSimpleListItemClickListener { @Override public void onItemClick(int position) { mCheckId = position; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: showLoadingDialog(正在加載。。。); new Handler().postDelayed(new Runnable() { @Override public void run() { dismissLoadingDialog(); } }, 10000); break; case R.id.button2: onSimpleListItem(); break; case R.id.button3: onEditTextDialog(); break; case R.id.button4: RelativeLayout layoutShow = (RelativeLayout)findViewById(R.id.layoutShow); mChatPopupWindow.showAtLocation(layoutShow, Gravity.RIGHT | Gravity.TOP, -10, mHeaderHeight + 10); break; case R.id.button5: startActivity(PullRefreshListViewActivity.class); break; default: break; } } private void onEditTextDialog() { final EditTextDialog mEditTextDialog = new EditTextDialog(this); mEditTextDialog.setTitle(填寫推薦人); mEditTextDialog.setButton(取消, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mEditTextDialog.cancel(); } }, 確認, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String text = mEditTextDialog.getText(); if (text == null) { mEditTextDialog.requestFocus(); } else { mEditTextDialog.dismiss(); } } }); mEditTextDialog.show(); } //初始化PopupWindow protected void initPopupWindow() { mWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 130, getResources().getDisplayMetrics()); mHeaderHeight = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 48, getResources() .getDisplayMetrics()); mChatPopupWindow = new ChatPopupWindow(this, mWidth, LayoutParams.WRAP_CONTENT); mChatPopupWindow.setOnChatPopupItemClickListener(this); } //以下是popupwindow的選擇監聽事件 @Override public void onVoiceModeClick() { showCustomToast(pop監聽1); } @Override public void onCreateClick() { showCustomToast(pop監聽2); } @Override public void onSynchronousClick() { showCustomToast(pop監聽3); } }
package com.example.activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import com.org.base.BaseActivity; import com.org.view.PullRefreshListView; import com.org.view.PullRefreshListView.OnCancelListener; import com.org.view.PullRefreshListView.OnRefreshListener; import com.viw.adapt.CheckListDialogAdapter; public class PullRefreshListViewActivity extends BaseActivity implements OnRefreshListener, OnCancelListener, OnItemClickListener{ private PullRefreshListView mRefreshListView; String[] modes = new String[]{測試一,測試二,測試三}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_refresh); initViews(); } protected void initViews() { mRefreshListView = (PullRefreshListView) findViewById(R.id.otherfeedlist_mmrlv_list); //適配器我隨便用了一個 mRefreshListView.setAdapter(new CheckListDialogAdapter(1, this, modes)); mRefreshListView.setOnItemClickListener(this); mRefreshListView.setOnRefreshListener(this); mRefreshListView.setOnCancelListener(this); } @Override public void onCancel() { //取消刷新 mRefreshListView.onRefreshComplete(); } @Override public void onRefresh() { //異步加載線程 putAsyncTask(new AsyncTask() { @Override protected Boolean doInBackground(Void... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); //這裡寫刷新adapt數據就行可以了 mRefreshListView.onRefreshComplete(); } }); } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // listview的單擊事件就不寫了 } }
此demo實現了列表ListView和可擴展列表ExpandableListView。ListViewActivity:package fk.androiddemo_16
首先來看下我們實現的效果和360效果的對比:360手機助手效果演示本庫實現的效果(Icon來自360手機助手,侵刪)xml布局文件注:為了美觀,講每個Button的高度以
上一篇(Android 設計隨便說說之簡單實踐(模塊劃分))例舉了應用商店設計來說明怎麼做模塊劃分。模塊劃分主要依賴於第一是業務需求,具體是怎麼樣的業務。應用商店則包括兩
上篇介紹了導航欄下的滑動切換實現,這次要介紹的是導航欄上的切換,在很多應用中經常見到,先看效果圖。當點擊導航條上的分類的時候,會根據選中的分類顯示下面的數據。先看布局文件