Android從0到完整項目(3)常見的Dialog與下拉刷新組件

常見的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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容