Android研究院Android UIAndroid进阶之旅

Android从0到完整项目(3)常见的Dialog与下拉刷新组

2017-04-26  本文已影响459人  移动开发者_李挺哲

常见的Dialog与下拉刷新组件

对话框效果图

Loading提示框 进度提示框 图片选择对话框 时间选择对话框 普通确认对话框

刷新

SwipeRefreshLayout刷新 SwipeRefreshLayout加载 常见下拉刷新 常见加载数据

说明

代码 使用了 AndroidAutoLayout butterknife 为基础库
butterknife https://github.com/JakeWharton/butterknife
AndroidAutoLayout
博客原文 http://blog.csdn.net/lmj623565791/article/details/49990941
https://github.com/hongyangAndroid/AndroidAutoLayout

对话框说明
使用 DialogFragment 实现,创建 DialogUtils 工具类



package com.ningcui.mylibrary.utiils;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;

import com.ningcui.mylibrary.viewLib.dialog.AbAlertDialogFragment;
import com.ningcui.mylibrary.viewLib.dialog.AbProgressDialogFragment;
import com.ningcui.mylibrary.viewLib.dialog.AbProgressHorizontalDialogFragment;
import com.ningcui.mylibrary.viewLib.dialog.AbSampleDialogFragment;


/**
 * Copyright 李挺哲
 * 创建人:litingzhe
 * 邮箱:453971498@qq.com
 * Created by litingzhe on 2017/4/11 下午3:23.
 * Info Dialog工具类
 */

public class AbDialogUtil {

    /**
     * dialog 标记
     */
    public static String dialogTag = "dialog";

    public static int ThemeHoloLightDialog = android.R.style.Theme_Material_Light_Dialog;

    public static int ThemeLightPanel = android.R.style.Theme_Light_Panel;

    /**
     * 显示一个全屏对话框.
     *
     * @param view
     * @return
     */
    public static AbSampleDialogFragment showFullScreenDialog(View view) {
        FragmentActivity activity = (FragmentActivity) view.getContext();
        // Create and show the dialog.
        AbSampleDialogFragment newFragment = AbSampleDialogFragment.newInstance(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen, Gravity.CENTER);
        newFragment.setContentView(view);
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 指定一个系统转场动画 
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);
        return newFragment;
    }

    /**
     * 显示一个居中的对话框.
     *
     * @param view
     */
    public static AbSampleDialogFragment showDialog(View view) {
        return showDialog(view, Gravity.CENTER);
    }

    /**
     * 显示一个居中的面板.
     *
     * @param view
     */
    public static AbSampleDialogFragment showPanel(View view) {
        return showPanel(view, Gravity.CENTER);
    }


    /**
     * 显示一个指定位置对话框.
     *
     * @param view
     * @param gravity 位置
     * @return
     */
    public static AbSampleDialogFragment showDialog(View view, int gravity) {

        return showDialogOrPanel(view, gravity, ThemeHoloLightDialog);
    }

    /**
     * 显示一个指定位置的Panel.
     *
     * @param view
     * @param gravity 位置
     * @return
     */
    public static AbSampleDialogFragment showPanel(View view, int gravity) {
        return showDialogOrPanel(view, gravity, ThemeLightPanel);
    }

    /**
     * 自定义的对话框面板.
     *
     * @param view    View
     * @param gravity 位置
     * @param style   样式 ThemeHoloLightDialog  ThemeLightPanel
     * @return
     */
    private static AbSampleDialogFragment showDialogOrPanel(View view, int gravity, int style) {
        FragmentActivity activity = (FragmentActivity) view.getContext();
        // Create and show the dialog.
        AbSampleDialogFragment newFragment = AbSampleDialogFragment.newInstance(DialogFragment.STYLE_NO_TITLE, style, gravity);
        newFragment.setContentView(view);

        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 指定一个系统转场动画   
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);

        return newFragment;
    }

    /**
     * 显示一个普通对话框.
     *
     * @param view 对话框View
     */
    public static AbAlertDialogFragment showAlertDialog(View view) {
        FragmentActivity activity = (FragmentActivity) view.getContext();
        AbAlertDialogFragment alertDialogFragment = new AbAlertDialogFragment();
        alertDialogFragment.setContentView(view);
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 指定一个系统转场动画
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        alertDialogFragment.show(ft, dialogTag);
        return alertDialogFragment;
    }

    /**
     * 显示进度框.
     *
     * @param context               the context
     * @param indeterminateDrawable 用默认请写0
     * @param message               the message
     * @param isCancelable          是否可以取消
     */
    public static AbProgressDialogFragment showProgressDialog(Context context, int indeterminateDrawable,
                                                              String message, boolean isCancelable) {
        FragmentActivity activity = (FragmentActivity) context;
        AbProgressDialogFragment newFragment = AbProgressDialogFragment.newInstance(indeterminateDrawable, message, isCancelable);
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 指定一个系统转场动画   
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);
        return newFragment;
    }


    /**
     * 显示一个隐藏的的对话框.
     *
     * @param context
     * @param fragment
     */
    public static void showDialog(Context context, DialogFragment fragment) {
        FragmentActivity activity = (FragmentActivity) context;
        fragment.show(activity.getFragmentManager(), dialogTag);
    }


    /**
     * 移除Fragment.
     *
     * @param context the context
     */
    public static void removeDialog(final Context context) {
        try {
            FragmentActivity activity = (FragmentActivity) context;
            FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
            // 指定一个系统转场动画
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            Fragment prev = activity.getFragmentManager().findFragmentByTag(dialogTag);
            if (prev != null) {
                ft.remove(prev);
            }
            //不能加入到back栈
            //ft.addToBackStack(null);
            ft.commit();
        } catch (Exception e) {
            //可能有Activity已经被销毁的异常
            e.printStackTrace();
        }
    }

    /**
     * 移除Fragment和View
     *
     * @param view
     */
    public static void removeDialog(View view) {
        removeDialog(view.getContext());
        AbViewUtil.removeSelfFromParent(view);
    }


}


普通对话框

 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("温馨提醒");
        builder.setMessage("您已经打开 android.support.v7.app.AlertDialog");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();


            }
        });
        builder.show();

横向进度对话框

  progressHortionalDialog = new ProgressDialog(mContext, android.support.v7.app.AlertDialog.BUTTON_NEUTRAL);
        if (title != null) {
            progressHortionalDialog.setTitle(title);
        }

        if (message != null) {
            progressHortionalDialog.setMessage(message);
        }
        progressHortionalDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressHortionalDialog.setIndeterminate(false);

        progressHortionalDialog.setMax(100);

        progressHortionalDialog.setCanceledOnTouchOutside(false);

        progressHortionalDialog.setCancelable(true);

        progressHortionalDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {

                dialog.dismiss();


            }
        });

常用下拉刷新

xml 中将 AbPullToRefreshView 套在 继承与ScrollView的组件最外层。例如 ScrollView ListView WebView GridView

 <com.ningcui.mylibrary.viewLib.refresh.AbPullToRefreshView
        android:id="@+id/pulltofreshView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

        <ListView

            android:id="@+id/pull_listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:divider="@color/gray_bg"
            android:dividerHeight="4px" />


    </com.ningcui.mylibrary.viewLib.refresh.AbPullToRefreshView>

设置 下拉刷新功能


        pulltofreshView.setLoadMoreEnable(true);
        pulltofreshView.setPullRefreshEnable(true);
        pulltofreshView.setOnHeaderRefreshListener(new AbPullToRefreshView.OnHeaderRefreshListener() {
            @Override
            public void onHeaderRefresh(AbPullToRefreshView abPullToRefreshView) {

                getData();


            }
        });

        pulltofreshView.setOnFooterLoadListener(new AbPullToRefreshView.OnFooterLoadListener() {
            @Override
            public void onFooterLoad(AbPullToRefreshView abPullToRefreshView) {


                loadData();


            }
        });

加载数据结束
pulltofreshView.onFooterLoadFinish();
刷新数据结束
pulltofreshView.onHeaderRefreshFinish();

自定义SwiperefreshLayout 支持加载

使用方法

 // 设置下拉进度的背景颜色,默认就是白色的
        swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshView.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);

        // 下拉时触发SwipeRefreshLayout的下拉动画,动画完毕之后就会回调这个方法
        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

              
                // TODO 获取数据
                final Random random = new Random();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mList.add(0, "测试数据" + random.nextInt(100) + "号");
                        mAdapter.notifyDataSetChanged();

                        Toast.makeText(SwipeRefreshLayoutActivity.this, "刷新了一条数据", Toast.LENGTH_SHORT).show();

                        // 加载完数据设置为不刷新状态,将下拉进度收起来
                        swipeRefreshView.setRefreshing(false);
                    }
                }, 1200);


            }
        });


        // 设置下拉加载更多
        swipeRefreshView.setOnLoadListener(new SwipeRefreshView.OnLoadListener() {
            @Override
            public void onLoad() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        // 添加数据
                        for (int i = 30; i < 35; i++) {
                            mList.add("测试数据" + i + "号");
                            // 这里要放在里面刷新,放在外面会导致刷新的进度条卡住
                            mAdapter.notifyDataSetChanged();
                        }

                        Toast.makeText(SwipeRefreshLayoutActivity.this, "加载了" + 5 + "条数据", Toast.LENGTH_SHORT).show();

                        // 加载完数据设置为不加载状态,将加载进度收起来
                        swipeRefreshView.setLoading(false);
                    }
                }, 1200);
            }
        });

说明: SwiperefreshLayout与 ViewPager 等横向滑动混用 会有异常。需要对ViewPager 进行一些修改 后续会提及

代码不定时更新 https://github.com/chinaltz/JustAndroid

上一篇下一篇

猜你喜欢

热点阅读