自定义对话框,进度条,下拉刷新等

本博客集成了自定义对话框、进度条、下拉刷新以及popup弹出框等功能,提供了易于学习和扩展使用的代码示例。包括基础类、对话框、适配器、主活动类、自定义弹窗和下拉刷新列表视图的实现,详细介绍了各个组件的使用方法和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个demo集合了自定义对话框,进度条,下拉刷新以及popup弹出框等。是学习了网上开源项目后,抽取集合了常用对话框,进度条,下拉刷新以及popup弹出框等。现在结构目录简单,更易于学习和扩展使用。注释都卸载代码。下面进行简单的介绍以及部分代码展示。

1、整体实现的效果图


2、项目结构图


这上面项目结构图也是一目了然,有什么功能展示。大家也看到了,这上面类有点多,如果全部贴出来,不大可能,有兴趣下载本文源码。

3、看看基础类BaseActivity

我就贴一下基础类,还有实现类。因为这些一段段代码易于其他项目使用。

[java]  view plain copy
  1. package com.org.base;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import com.example.activity.R;  
  8. import com.example.activity.R.layout;  
  9. import com.org.dialog.LoadingDialog;  
  10. import com.org.view.HandyTextView;  
  11.   
  12. import android.os.AsyncTask;  
  13. import android.os.Bundle;  
  14. import android.util.DisplayMetrics;  
  15. import android.util.Log;  
  16. import android.view.Gravity;  
  17. import android.view.LayoutInflater;  
  18. import android.view.View;  
  19. import android.widget.Toast;  
  20. import android.app.Activity;  
  21. import android.content.Intent;  
  22.   
  23. public class BaseActivity extends Activity {  
  24.     protected LoadingDialog mLoadingDialog;  
  25.     /** 
  26.      * 屏幕的宽度、高度、密度 
  27.      */  
  28.     protected int mScreenWidth;  
  29.     protected int mScreenHeight;  
  30.     protected float mDensity;  
  31.     //异步加载任务的list  
  32.     protected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>();  
  33.       
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.           
  39.         mLoadingDialog = new LoadingDialog(this"请求提交中");  
  40.         DisplayMetrics metric = new DisplayMetrics();  
  41.         getWindowManager().getDefaultDisplay().getMetrics(metric);  
  42.         mScreenWidth = metric.widthPixels;  
  43.         mScreenHeight = metric.heightPixels;  
  44.         mDensity = metric.density;  
  45.     }  
  46.       
  47.     protected void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask) {  
  48.         mAsyncTasks.add(asyncTask.execute());  
  49.     }  
  50.       
  51.     protected void clearAsyncTask() {  
  52.         Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks  
  53.                 .iterator();  
  54.         while (iterator.hasNext()) {  
  55.             AsyncTask<Void, Void, Boolean> asyncTask = iterator.next();  
  56.             if (asyncTask != null && !asyncTask.isCancelled()) {  
  57.                 asyncTask.cancel(true);  
  58.             }  
  59.         }  
  60.         mAsyncTasks.clear();  
  61.     }  
  62.   
  63.     protected void showLoadingDialog(String text) {  
  64.         if (text != null) {  
  65.             mLoadingDialog.setText(text);  
  66.         }  
  67.         mLoadingDialog.show();  
  68.     }  
  69.   
  70.     protected void dismissLoadingDialog() {  
  71.         if (mLoadingDialog.isShowing()) {  
  72.             mLoadingDialog.dismiss();  
  73.         }  
  74.     }  
  75.       
  76.     /** 短暂显示Toast提示(来自res) **/  
  77.     protected void showShortToast(int resId) {  
  78.         Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show();  
  79.     }  
  80.   
  81.     /** 短暂显示Toast提示(来自String) **/  
  82.     protected void showShortToast(String text) {  
  83.         Toast.makeText(this, text, Toast.LENGTH_SHORT).show();  
  84.     }  
  85.   
  86.     /** 长时间显示Toast提示(来自res) **/  
  87.     protected void showLongToast(int resId) {  
  88.         Toast.makeText(this, getString(resId), Toast.LENGTH_LONG).show();  
  89.     }  
  90.   
  91.     /** 长时间显示Toast提示(来自String) **/  
  92.     protected void showLongToast(String text) {  
  93.         Toast.makeText(this, text, Toast.LENGTH_LONG).show();  
  94.     }  
  95.   
  96.     /** 显示自定义Toast提示(来自res) **/  
  97.     protected void showCustomToast(int resId) {  
  98.         View toastRoot = LayoutInflater.from(BaseActivity.this).inflate(  
  99.                 R.layout.common_toast, null);  
  100.         ((HandyTextView) toastRoot.findViewById(R.id.toast_text))  
  101.                 .setText(getString(resId));  
  102.         Toast toast = new Toast(BaseActivity.this);  
  103.         toast.setGravity(Gravity.CENTER, 00);  
  104.         toast.setDuration(Toast.LENGTH_SHORT);  
  105.         toast.setView(toastRoot);  
  106.         toast.show();  
  107.     }  
  108.   
  109.     /** 显示自定义Toast提示(来自String) **/  
  110.     protected void showCustomToast(String text) {  
  111.         View toastRoot = LayoutInflater.from(BaseActivity.this).inflate(  
  112.                 R.layout.common_toast, null);  
  113.         ((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(text);  
  114.         Toast toast = new Toast(BaseActivity.this);  
  115.         toast.setGravity(Gravity.CENTER, 00);  
  116.         toast.setDuration(Toast.LENGTH_SHORT);  
  117.         toast.setView(toastRoot);  
  118.         toast.show();  
  119.     }  
  120.   
  121.     /** Debug输出Log日志 **/  
  122.     protected void showLogDebug(String tag, String msg) {  
  123.         Log.d(tag, msg);  
  124.     }  
  125.   
  126.     /** Error输出Log日志 **/  
  127.     protected void showLogError(String tag, String msg) {  
  128.         Log.e(tag, msg);  
  129.     }  
  130.   
  131.     /** 通过Class跳转界面 **/  
  132.     protected void startActivity(Class<?> cls) {  
  133.         startActivity(cls, null);  
  134.     }  
  135.   
  136.     /** 含有Bundle通过Class跳转界面 **/  
  137.     protected void startActivity(Class<?> cls, Bundle bundle) {  
  138.         Intent intent = new Intent();  
  139.         intent.setClass(this, cls);  
  140.         if (bundle != null) {  
  141.             intent.putExtras(bundle);  
  142.         }  
  143.         startActivity(intent);  
  144.     }  
  145.       
  146.     /** 默认退出 **/  
  147.     protected void defaultFinish() {  
  148.         super.finish();  
  149.     }  
  150. }  

4、BasePopupWindow

[java]  view plain copy
  1. package com.org.base;  
  2.   
  3. import android.annotation.TargetApi;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.os.Build;  
  7. import android.util.AttributeSet;  
  8. import android.view.Gravity;  
  9. import android.view.View;  
  10. import android.widget.PopupWindow;  
  11.   
  12. @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  13. public abstract class BasePopupWindow extends PopupWindow {  
  14.   
  15.     protected View mContentView;  
  16.     protected onSubmitClickListener mOnSubmitClickListener;  
  17.   
  18.     public BasePopupWindow() {  
  19.         super();  
  20.     }  
  21.   
  22.     public BasePopupWindow(Context context, AttributeSet attrs,  
  23.             int defStyleAttr, int defStyleRes) {  
  24.         super(context, attrs, defStyleAttr, defStyleRes);  
  25.     }  
  26.   
  27.     public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.     }  
  30.   
  31.     public BasePopupWindow(Context context, AttributeSet attrs) {  
  32.         super(context, attrs);  
  33.     }  
  34.   
  35.     public BasePopupWindow(Context context) {  
  36.         super(context);  
  37.     }  
  38.   
  39.     public BasePopupWindow(int width, int height) {  
  40.         super(width, height);  
  41.     }  
  42.   
  43.     public BasePopupWindow(View contentView, int width, int height,  
  44.             boolean focusable) {  
  45.         super(contentView, width, height, focusable);  
  46.     }  
  47.   
  48.     public BasePopupWindow(View contentView) {  
  49.         super(contentView);  
  50.     }  
  51.   
  52.     @SuppressWarnings("deprecation")  
  53.     public BasePopupWindow(View contentView, int width, int height) {  
  54.         super(contentView, width, height, true);  
  55.         mContentView = contentView;  
  56.         setBackgroundDrawable(new BitmapDrawable());  
  57.         initViews();  
  58.         initEvents();  
  59.         init();  
  60.     }  
  61.   
  62.     public abstract void initViews();  
  63.   
  64.     public abstract void initEvents();  
  65.   
  66.     public abstract void init();  
  67.   
  68.     public View findViewById(int id) {  
  69.         return mContentView.findViewById(id);  
  70.     }  
  71.   
  72.     /** 
  73.      * 显示在parent的上部并水平居中 
  74.      *  
  75.      * @param parent 
  76.      */  
  77.     public void showViewTopCenter(View parent) {  
  78.         showAtLocation(parent, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 00);  
  79.     }  
  80.   
  81.     /** 
  82.      * 显示在parent的中心 
  83.      *  
  84.      * @param parent 
  85.      */  
  86.     public void showViewCenter(View parent) {  
  87.         showAtLocation(parent, Gravity.CENTER, 00);  
  88.     }  
  89.   
  90.     /** 
  91.      * 添加确认单击监听 
  92.      *  
  93.      * @param l 
  94.      */  
  95.     public void setOnSubmitClickListener(onSubmitClickListener l) {  
  96.         mOnSubmitClickListener = l;  
  97.     }  
  98.   
  99.     public interface onSubmitClickListener {  
  100.         void onClick();  
  101.     }  
  102.   
  103. }  

5、BaseArrayListAdapter

[java]  view plain copy
  1. package com.org.base;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.List;  
  6.   
  7. import android.content.Context;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.BaseAdapter;  
  12.   
  13. //基本的适配器  
  14. public class BaseArrayListAdapter extends BaseAdapter {  
  15.   
  16.     protected Context mContext;  
  17.     protected LayoutInflater mInflater;  
  18.     protected List<String> mDatas = new ArrayList<String>();  
  19.   
  20.     //String数组添加数据源  
  21.     public BaseArrayListAdapter(Context context, String... datas) {  
  22.         mContext = context;  
  23.         mInflater = LayoutInflater.from(context);  
  24.         if (datas != null && datas.length > 0) {  
  25.             mDatas = Arrays.asList(datas);  
  26.         }  
  27.     }  
  28.   
  29.     //用list方式添加数据源  
  30.     public BaseArrayListAdapter(Context context, List<String> datas) {  
  31.         mContext = context;  
  32.         mInflater = LayoutInflater.from(context);  
  33.         if (datas != null && datas.size() > 0) {  
  34.             mDatas = datas;  
  35.         }  
  36.     }  
  37.   
  38.     @Override  
  39.     public int getCount() {  
  40.         return mDatas.size();  
  41.     }  
  42.   
  43.     @Override  
  44.     public Object getItem(int position) {  
  45.         return mDatas.get(position);  
  46.     }  
  47.   
  48.     @Override  
  49.     public long getItemId(int position) {  
  50.         return position;  
  51.     }  
  52.   
  53.     @Override  
  54.     public View getView(int position, View convertView, ViewGroup parent) {  
  55.         return null;  
  56.     }  
  57. }  

6、实现自定义对话框,进度条以及popup弹出框,主MainActivity

[java]  view plain copy
  1. package com.example.activity;  
  2.   
  3. import com.org.base.BaseActivity;  
  4. import com.org.dialog.EditTextDialog;  
  5. import com.org.dialog.SimpleListDialog;  
  6. import com.org.dialog.SimpleListDialog.onSimpleListItemClickListener;  
  7. import com.org.popupwindow.ChatPopupWindow;  
  8. import com.org.popupwindow.ChatPopupWindow.onChatPopupItemClickListener;  
  9. import com.viw.adapt.CheckListDialogAdapter;  
  10.   
  11. import android.R.layout;  
  12. import android.content.DialogInterface;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.util.TypedValue;  
  16. import android.view.Gravity;  
  17. import android.view.View;  
  18. import android.view.View.OnClickListener;  
  19. import android.view.ViewGroup.LayoutParams;  
  20. import android.widget.Button;  
  21. import android.widget.RelativeLayout;  
  22.   
  23. public class MainActivity extends BaseActivity   
  24. implements   
  25. OnClickListener,  
  26. onChatPopupItemClickListener{  
  27.   
  28.     protected SimpleListDialog mDialog;  
  29.     private int mCheckId = 0;  
  30.     private ChatPopupWindow mChatPopupWindow;  
  31.     private int mWidth;  
  32.     private int mHeaderHeight;  
  33.       
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.   
  39.         findViewById(R.id.button1).setOnClickListener(this);  
  40.         findViewById(R.id.button2).setOnClickListener(this);  
  41.         findViewById(R.id.button3).setOnClickListener(this);  
  42.         findViewById(R.id.button4).setOnClickListener(this);  
  43.         findViewById(R.id.button5).setOnClickListener(this);          
  44.         initPopupWindow();  
  45.     }  
  46.   
  47.     public void onSimpleListItem() {  
  48.         String[] modes = new String[]{"测试一","测试二","测试三"};  
  49.         mDialog = new SimpleListDialog(this);  
  50.         mDialog.setTitle("单选框");  
  51.         mDialog.setTitleLineVisibility(View.GONE);  
  52.         mDialog.setAdapter(new CheckListDialogAdapter(mCheckId, this, modes));  
  53.         //用匿名内部类实现接口监听  
  54.         mDialog.setOnSimpleListItemClickListener(new DialogItemClickListener());  
  55.         mDialog.show();  
  56.     }  
  57.   
  58.     protected class DialogItemClickListener implements  
  59.             onSimpleListItemClickListener {  
  60.   
  61.         @Override  
  62.         public void onItemClick(int position) {  
  63.             mCheckId = position;  
  64.         }  
  65.     }  
  66.   
  67.     @Override  
  68.     public void onClick(View v) {  
  69.         switch (v.getId()) {  
  70.         case R.id.button1:  
  71.   
  72.             showLoadingDialog("正在加载。。。");  
  73.             new Handler().postDelayed(new Runnable() {  
  74.                 @Override  
  75.                 public void run() {  
  76.                     dismissLoadingDialog();  
  77.   
  78.                 }  
  79.             }, 10000);  
  80.             break;  
  81.         case R.id.button2:  
  82.             onSimpleListItem();  
  83.             break;  
  84.         case R.id.button3:  
  85.             onEditTextDialog();  
  86.             break;        
  87.         case R.id.button4:  
  88.             RelativeLayout layoutShow = (RelativeLayout)findViewById(R.id.layoutShow);  
  89.             mChatPopupWindow.showAtLocation(layoutShow, Gravity.RIGHT  
  90.                     | Gravity.TOP, -10, mHeaderHeight + 10);  
  91.             break;  
  92.         case R.id.button5:  
  93.             startActivity(PullRefreshListViewActivity.class);  
  94.             break;  
  95.         default:  
  96.             break;  
  97.         }  
  98.   
  99.     }  
  100.   
  101.     private void onEditTextDialog() {  
  102.         final EditTextDialog mEditTextDialog = new EditTextDialog(this);  
  103.         mEditTextDialog.setTitle("填写推荐人");  
  104.         mEditTextDialog.setButton("取消",  
  105.                 new DialogInterface.OnClickListener() {  
  106.   
  107.                     @Override  
  108.                     public void onClick(DialogInterface dialog, int which) {  
  109.                         mEditTextDialog.cancel();  
  110.                     }  
  111.                 }, "确认"new DialogInterface.OnClickListener() {  
  112.   
  113.                     @Override  
  114.                     public void onClick(DialogInterface dialog, int which) {  
  115.                         String text = mEditTextDialog.getText();  
  116.                         if (text == null) {  
  117.                             mEditTextDialog.requestFocus();  
  118.                         } else {  
  119.                             mEditTextDialog.dismiss();  
  120.                         }  
  121.                     }  
  122.                 });  
  123.         mEditTextDialog.show();  
  124.     }  
  125.       
  126.     //初始化PopupWindow  
  127.     protected void initPopupWindow() {  
  128.         mWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
  129.                 130, getResources().getDisplayMetrics());  
  130.         mHeaderHeight = (int) TypedValue.applyDimension(  
  131.                 TypedValue.COMPLEX_UNIT_DIP, 48, getResources()  
  132.                         .getDisplayMetrics());  
  133.         mChatPopupWindow = new ChatPopupWindow(this, mWidth,  
  134.                 LayoutParams.WRAP_CONTENT);  
  135.         mChatPopupWindow.setOnChatPopupItemClickListener(this);  
  136.     }  
  137.   
  138.       
  139.     //以下是popupwindow的选择监听事件  
  140.     @Override  
  141.     public void onVoiceModeClick() {  
  142.         showCustomToast("pop监听1");  
  143.           
  144.     }  
  145.   
  146.     @Override  
  147.     public void onCreateClick() {  
  148.         showCustomToast("pop监听2");  
  149.     }  
  150.   
  151.     @Override  
  152.     public void onSynchronousClick() {  
  153.         showCustomToast("pop监听3");  
  154.           
  155.     }  
  156.   
  157. }  

7、下拉刷新PullRefreshListViewActivity

[java]  view plain copy
  1. package com.example.activity;  
  2.   
  3. import android.os.AsyncTask;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.AdapterView.OnItemClickListener;  
  8. import com.org.base.BaseActivity;  
  9. import com.org.view.PullRefreshListView;  
  10. import com.org.view.PullRefreshListView.OnCancelListener;  
  11. import com.org.view.PullRefreshListView.OnRefreshListener;  
  12. import com.viw.adapt.CheckListDialogAdapter;  
  13.   
  14. public class PullRefreshListViewActivity extends BaseActivity  
  15. implements  
  16. OnRefreshListener,  
  17. OnCancelListener,  
  18. OnItemClickListener{  
  19.     private PullRefreshListView mRefreshListView;  
  20.     String[] modes = new String[]{"测试一","测试二","测试三"};  
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_refresh);  
  26.           
  27.         initViews();  
  28.     }  
  29.       
  30.       
  31.     protected void initViews() {  
  32.         mRefreshListView = (PullRefreshListView) findViewById(R.id.otherfeedlist_mmrlv_list);  
  33.         //适配器我随便用了一个  
  34.         mRefreshListView.setAdapter(new CheckListDialogAdapter(1this, modes));  
  35.         mRefreshListView.setOnItemClickListener(this);  
  36.         mRefreshListView.setOnRefreshListener(this);  
  37.         mRefreshListView.setOnCancelListener(this);  
  38.     }  
  39.   
  40.   
  41.     @Override  
  42.     public void onCancel() {  
  43.         //取消刷新  
  44.         mRefreshListView.onRefreshComplete();  
  45.     }  
  46.   
  47.   
  48.     @Override  
  49.     public void onRefresh() {  
  50.           
  51.         //异步加载线程  
  52.         putAsyncTask(new AsyncTask<Void, Void, Boolean>() {  
  53.             @Override  
  54.             protected Boolean doInBackground(Void... params) {  
  55.                 try {  
  56.                     Thread.sleep(2000);  
  57.                 } catch (InterruptedException e) {  
  58.   
  59.                 }  
  60.                 return null;  
  61.             }  
  62.   
  63.             @Override  
  64.             protected void onPostExecute(Boolean result) {  
  65.                 super.onPostExecute(result);  
  66.                 //这里写刷新adapt数据就行可以了  
  67.                   
  68.                   
  69.                 mRefreshListView.onRefreshComplete();  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74.   
  75.     @Override  
  76.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  77.             long id) {  
  78.         // listview的单击事件就不写了  
  79.           
  80.     }  
  81. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值