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

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

本文demo下载:点击

1、整体实现的效果图

2、项目结构图

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

3、看看基础类BaseActivity

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

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();
	}
}

4、BasePopupWindow

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();
	}

}

5、BaseArrayListAdapter

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;
	}
}

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

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");

	}

}

7、下拉刷新PullRefreshListViewActivity

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下载:点击

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 11:20:46

Android 自定义对话框,进度条,下拉刷新等的相关文章

Android UI--自定义ListView(实现下拉刷新+加载更多)

http://blog.csdn.net/wwj_748/article/details/12512885 Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就是不健全的.因为小巫近期要开发新浪微博客户端,需要实现ListView的下拉刷新,所以就想把这个UI整合到项目当中去,这里只是一个demo,可以根据项目的需要进行修改. 就不要太在乎界面了哈:

Android学习之listview的下拉刷新、上拉载入

本例是在上例的基础上完成的.本例实现的listview上拉载入.下拉刷新功能,是在开源网站上别人写好的listview,主要是对listview的控件进行重写,添加了footer和header. 1.listview_footer listview_footer是listview的底部. 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解 标签: Android下拉刷新pullToRefreshListViewGridView 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-r

wpf 进度条 下拉

<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height

android开发游记:SpringView 下拉刷新的高效解决方案,定制你自己风格的拖拽页面

关于下拉刷新/上拉加载更多的解决方案网上已经有很多了,浏览了目前主流的下拉控件比如PullToRefresh库等,第一:大多数实现库都难以进行动画和样式的自定义.第二:不能很好的兼容多种滚动控件,它们都对listView.RecyclerView等进行了不同程度的重新实现,你在项目中就得使用库提供的PullToRefreshListView.PullToRefreshRecyclerView等来代替源生的listView.RecyclerView等,这样的方式其实并不好,随着android版本的

Android之RecyclerView轻松实现下拉刷新和加载更多

今天研究了下RecyclerView的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的APP显示数据列表时都用到了.自定义RecyclerView下拉刷新和加载更多听上去很复杂,实际上并不难,只要是对滑动事件的监听和处理. 一.自定义RecyclerView实现下拉刷新和加载更多 1.如何判断RecyclerView是在上滑还是下滑 在RecyclerView的OnScrollListener滑动事件监听中有个好用的方法,就是onScrolled(RecyclerView recycle

android Fragment 实例 Listfragment listiew 下拉刷新 上拉加载 横向滑动事件监听

Android Fragment开发实例及他色功能说明 代码太长,请网盘下载附件源代码: http://yunpan.cn/cs24a2Z7C5kRk (提取码:a0cf)  有疑问的欢迎联系QQ972910164 一. CustomListView说明:可下拉刷新,上拉加载,横向滑动接口回调, /** * 功能类似ListView,天机以下特性: * 1. 可下拉刷新,上拉加载,实现CustomListView.OnPullListener接口 * 3. Slide切换,实现CustomLis

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解 (转载)

最近项目用到下拉刷新,上来加载更多,这里对PullToRefresh这控件进行了解和使用. 以下内容转载自:http://blog.csdn.net/lmj623565791/article/details/38238749 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,例子中的功能极其强大,支持很多控件.本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更多. 1.List

【转载】 Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

Android下拉刷新pullToRefreshListViewGridView 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,例子中的功能极其强大,支持很多控件.本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更

android自定义圆形进度条,实现动态画圆效果

自定义圆形进度条效果图如下:应用场景如动态显示分数等. view的自定义属性如下attr.xml <?xml version="1.0" encoding="UTF-8"?> <resources> <declare-styleable name="ArcProgressbar">         <!-- 圆环起始角度-->         <attr name="startAng