这个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<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>(); @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(AsyncTask<Void, Void, Boolean> asyncTask) { mAsyncTasks.add(asyncTask.execute()); } protected void clearAsyncTask() { Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks .iterator(); while (iterator.hasNext()) { AsyncTask<Void, Void, Boolean> 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 List<String> mDatas = new ArrayList<String>(); //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<String> 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<Void, Void, Boolean>() { @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下载:点击
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_16064871/article/details/49716555