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

常見(jiàn)的Dialog與下拉刷新組件

對(duì)話框效果圖

Loading提示框
進(jìn)度提示框
圖片選擇對(duì)話框
時(shí)間選擇對(duì)話框
普通確認(rèn)對(duì)話框

刷新

SwipeRefreshLayout刷新
SwipeRefreshLayout加載
常見(jiàn)下拉刷新
常見(jiàn)加載數(shù)據(jù)

說(shuō)明

代碼 使用了 AndroidAutoLayout butterknife 為基礎(chǔ)庫(kù)
butterknife https://github.com/JakeWharton/butterknife
AndroidAutoLayout
博客原文 http://blog.csdn.net/lmj623565791/article/details/49990941
https://github.com/hongyangAndroid/AndroidAutoLayout

對(duì)話框說(shuō)明
使用 DialogFragment 實(shí)現(xiàn),創(chuàng)建 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 李挺哲
 * 創(chuàng)建人:litingzhe
 * 郵箱:453971498@qq.com
 * Created by litingzhe on 2017/4/11 下午3:23.
 * Info Dialog工具類
 */

public class AbDialogUtil {

    /**
     * dialog 標(biāo)記
     */
    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;

    /**
     * 顯示一個(gè)全屏對(duì)話框.
     *
     * @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();
        // 指定一個(gè)系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫(huà) 
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);
        return newFragment;
    }

    /**
     * 顯示一個(gè)居中的對(duì)話框.
     *
     * @param view
     */
    public static AbSampleDialogFragment showDialog(View view) {
        return showDialog(view, Gravity.CENTER);
    }

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


    /**
     * 顯示一個(gè)指定位置對(duì)話框.
     *
     * @param view
     * @param gravity 位置
     * @return
     */
    public static AbSampleDialogFragment showDialog(View view, int gravity) {

        return showDialogOrPanel(view, gravity, ThemeHoloLightDialog);
    }

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

    /**
     * 自定義的對(duì)話框面板.
     *
     * @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();
        // 指定一個(gè)系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)   
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);

        return newFragment;
    }

    /**
     * 顯示一個(gè)普通對(duì)話框.
     *
     * @param view 對(duì)話框View
     */
    public static AbAlertDialogFragment showAlertDialog(View view) {
        FragmentActivity activity = (FragmentActivity) view.getContext();
        AbAlertDialogFragment alertDialogFragment = new AbAlertDialogFragment();
        alertDialogFragment.setContentView(view);
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 指定一個(gè)系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        alertDialogFragment.show(ft, dialogTag);
        return alertDialogFragment;
    }

    /**
     * 顯示進(jìn)度框.
     *
     * @param context               the context
     * @param indeterminateDrawable 用默認(rèn)請(qǐng)寫(xiě)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();
        // 指定一個(gè)系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)   
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        newFragment.show(ft, dialogTag);
        return newFragment;
    }


    /**
     * 顯示一個(gè)隱藏的的對(duì)話框.
     *
     * @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();
            // 指定一個(gè)系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
            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已經(jīng)被銷毀的異常
            e.printStackTrace();
        }
    }

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


}


普通對(duì)話框

 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("溫馨提醒");
        builder.setMessage("您已經(jīng)打開(kāi) 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();

橫向進(jìn)度對(duì)話框

  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>

設(shè)置 下拉刷新功能


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


            }
        });

加載數(shù)據(jù)結(jié)束
pulltofreshView.onFooterLoadFinish();
刷新數(shù)據(jù)結(jié)束
pulltofreshView.onHeaderRefreshFinish();

自定義SwiperefreshLayout 支持加載

使用方法

 // 設(shè)置下拉進(jìn)度的背景顏色,默認(rèn)就是白色的
        swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 設(shè)置下拉進(jìn)度的主題顏色
        swipeRefreshView.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);

        // 下拉時(shí)觸發(fā)SwipeRefreshLayout的下拉動(dòng)畫(huà),動(dòng)畫(huà)完畢之后就會(huì)回調(diào)這個(gè)方法
        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

              
                // TODO 獲取數(shù)據(jù)
                final Random random = new Random();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mList.add(0, "測(cè)試數(shù)據(jù)" + random.nextInt(100) + "號(hào)");
                        mAdapter.notifyDataSetChanged();

                        Toast.makeText(SwipeRefreshLayoutActivity.this, "刷新了一條數(shù)據(jù)", Toast.LENGTH_SHORT).show();

                        // 加載完數(shù)據(jù)設(shè)置為不刷新?tīng)顟B(tài),將下拉進(jìn)度收起來(lái)
                        swipeRefreshView.setRefreshing(false);
                    }
                }, 1200);


            }
        });


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

                        // 添加數(shù)據(jù)
                        for (int i = 30; i < 35; i++) {
                            mList.add("測(cè)試數(shù)據(jù)" + i + "號(hào)");
                            // 這里要放在里面刷新,放在外面會(huì)導(dǎo)致刷新的進(jìn)度條卡住
                            mAdapter.notifyDataSetChanged();
                        }

                        Toast.makeText(SwipeRefreshLayoutActivity.this, "加載了" + 5 + "條數(shù)據(jù)", Toast.LENGTH_SHORT).show();

                        // 加載完數(shù)據(jù)設(shè)置為不加載狀態(tài),將加載進(jìn)度收起來(lái)
                        swipeRefreshView.setLoading(false);
                    }
                }, 1200);
            }
        });

說(shuō)明: SwiperefreshLayout與 ViewPager 等橫向滑動(dòng)混用 會(huì)有異常。需要對(duì)ViewPager 進(jìn)行一些修改 后續(xù)會(huì)提及

代碼不定時(shí)更新 https://github.com/chinaltz/JustAndroid

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容