├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── libraries │ ├── appcompat_v7_23_1_1.xml │ ├── recyclerview_v7_23_1_1.xml │ ├── support_annotations_23_1_1.xml │ └── support_v4_23_1_1.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml ├── vcs.xml └── workspace.xml ├── DialogFragmentUse.iml ├── DialogFragmentUse3.iml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── niu │ │ └── dialogfragment │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── niu │ │ └── dialogfragment │ │ ├── activity │ │ ├── BaseActivity.java │ │ └── FriendActivity.java │ │ ├── adapter │ │ └── FriendAdapter.java │ │ ├── data │ │ └── Friend.java │ │ ├── fragment │ │ ├── BaseFragment.java │ │ └── FriendFragment.java │ │ └── widget │ │ ├── BaseDialogFragment.java │ │ ├── ConfirmDialogFragment.java │ │ ├── DialogFactory.java │ │ ├── DialogListenerHolder.java │ │ ├── ListDialogFragment.java │ │ └── ProgressDialogFragment.java │ └── res │ ├── layout │ ├── activity_friend.xml │ ├── adapter_friend_item.xml │ ├── dialog_custom_progress.xml │ └── fragment_friend.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | DialogFragmentUse -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/recyclerview_v7_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_23_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Android 39 | 40 | 41 | Android Lint 42 | 43 | 44 | Java 45 | 46 | 47 | Java language level migration aidsJava 48 | 49 | 50 | 51 | 52 | Android 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 74 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 17 | 18 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 114 | 1208 | 1213 | 1525 | 1528 | 1529 | 1530 | 1537 | 1538 | 1539 | 1544 | 1545 | 1546 | 1547 | 1548 | 1549 | 1553 | 1554 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 1566 | 1567 | 1568 | 1569 | 1570 | 1571 | 1572 | 1573 | 1574 | 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 1584 | 1585 | 1586 | 1587 | 1590 | 1591 | 1594 | 1595 | 1596 | 1597 | 1600 | 1601 | 1604 | 1605 | 1608 | 1609 | 1610 | 1611 | 1614 | 1615 | 1618 | 1619 | 1622 | 1623 | 1626 | 1627 | 1630 | 1631 | 1632 | 1633 | 1636 | 1637 | 1640 | 1641 | 1644 | 1645 | 1648 | 1649 | 1652 | 1653 | 1654 | 1655 | 1658 | 1659 | 1662 | 1663 | 1666 | 1667 | 1670 | 1671 | 1674 | 1675 | 1676 | 1677 | 1680 | 1681 | 1684 | 1685 | 1688 | 1689 | 1692 | 1693 | 1694 | 1695 | 1696 | 1697 | 1698 | 1699 | 1700 | 1701 | 1702 | 1703 | 1704 | 1705 | 1706 | 1707 | 1708 | 1709 | 1710 | 1711 | 1712 | 1731 | 1732 | 1733 | 1751 | 1752 | 1753 | 1766 | 1767 | 1768 | 1769 | 1783 | 1784 | 1785 | 1786 | 1789 | 1790 | 1791 | 1792 | 1793 | 1794 | 1795 | 1796 | 1803 | 1804 | 1805 | 1806 | 1824 | 1831 | 1832 | 1833 | 1852 | 1853 | 1854 | 1855 | 1856 | 1864 | 1865 | 1867 | 1868 | 1869 | 1870 | 1871 | 1872 | 1460898029156 1873 | 1876 | 1877 | 1878 | 1879 | 1880 | 1881 | 1882 | 1883 | 1884 | 1885 | 1886 | 1887 | 1888 | 1889 | 1890 | 1891 | 1892 | 1893 | 1894 | 1895 | 1896 | 1897 | 1898 | 1899 | 1900 | 1901 | 1902 | 1903 | 1904 | 1905 | 1906 | 1907 | 1908 | 1909 | 1910 | 1911 | 1912 | 1913 | 1914 | 1915 | 1916 | 1917 | 1918 | 1919 | 1920 | 1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1932 | 1933 | 1934 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1947 | 1948 | 1949 | 1950 | 1951 | file://$PROJECT_DIR$/app/src/main/java/com/niu/dialogfragment/widget/DialogFactory.java 1952 | 58 1953 | 1954 | 1956 | 1957 | 1959 | 1960 | 1961 | 1962 | 1963 | 1964 | 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | 1971 | 1972 | 1973 | 1974 | 1975 | 1976 | 1977 | 1978 | 1979 | 1980 | 1981 | 1982 | 1983 | 1984 | 1985 | 1986 | 1987 | 1988 | 1989 | 1990 | 1991 | 1992 | 1993 | 1994 | 1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2031 | 2032 | 2033 | 2034 | 2035 | 2036 | 2037 | 2038 | 2039 | 2040 | 2041 | 2042 | 2043 | 2044 | 2045 | 2046 | 2047 | 2048 | 2049 | 2050 | 2051 | 2052 | 2053 | 2054 | 2055 | 2056 | 2057 | -------------------------------------------------------------------------------- /DialogFragmentUse.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /DialogFragmentUse3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DialogFragmentUse -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.niu.dialogfragment" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:23.1.1' 25 | compile 'com.android.support:recyclerview-v7:23.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\work\android studio\StudioIDE\android-sdk-windows-r24.2/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/niu/dialogfragment/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/activity/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.activity; 2 | 3 | import android.support.v4.app.FragmentActivity; 4 | import android.os.Bundle; 5 | 6 | import com.niu.dialogfragment.widget.BaseDialogFragment; 7 | import com.niu.dialogfragment.widget.DialogFactory; 8 | 9 | 10 | public abstract class BaseActivity extends FragmentActivity { 11 | 12 | 13 | 14 | protected DialogFactory mDialogFactory; 15 | 16 | 17 | 18 | 19 | public BaseDialogFragment.BaseDialogListener getDialogListener(){ 20 | return mDialogFactory.mListenerHolder.getDialogListener(); 21 | } 22 | 23 | /** 24 | * 清空DialogListenerHolder中的dialog listener 25 | */ 26 | public void clearDialogListener(){ 27 | mDialogFactory.mListenerHolder.setDialogListener(null); 28 | } 29 | 30 | 31 | @Override 32 | public void onSaveInstanceState(Bundle outState) { 33 | super.onSaveInstanceState(outState); 34 | mDialogFactory.mListenerHolder.saveDialogListenerKey(outState); 35 | } 36 | 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | mDialogFactory = new DialogFactory(getSupportFragmentManager(),savedInstanceState); 42 | mDialogFactory.restoreDialogListener(this); 43 | } 44 | 45 | 46 | @Override 47 | protected void onDestroy() { 48 | super.onDestroy(); 49 | 50 | } 51 | 52 | @Override 53 | protected void onResume() { 54 | super.onResume(); 55 | 56 | } 57 | 58 | @Override 59 | protected void onStop() { 60 | super.onStop(); 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/activity/FriendActivity.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.activity; 2 | 3 | import android.content.DialogInterface; 4 | import android.os.Bundle; 5 | import android.view.KeyEvent; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | 9 | import com.niu.dialogfragment.R; 10 | import com.niu.dialogfragment.widget.ConfirmDialogFragment; 11 | 12 | 13 | /** 14 | * Created by niuxiaowei on 2016/1/19. 15 | */ 16 | public class FriendActivity extends BaseActivity implements ConfirmDialogFragment.ConfirmDialogListener{ 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_friend); 22 | initViews(); 23 | } 24 | 25 | private void initViews(){ 26 | findViewById(R.id.show_pro).setOnClickListener(new View.OnClickListener() { 27 | @Override 28 | public void onClick(View view) { 29 | mDialogFactory.showProgressDialog("Activity调起的进度条...",true); 30 | } 31 | }); 32 | 33 | findViewById(R.id.show_confi).setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View view) { 36 | mDialogFactory.showConfirmDialog("activity调起确认框","我是Activity中的确认框",true,FriendActivity.this); 37 | } 38 | }); 39 | } 40 | 41 | @Override 42 | public void onBackPressed() { 43 | super.onBackPressed(); 44 | } 45 | 46 | @Override 47 | public void onClick(DialogInterface dialogInterface, int i) { 48 | Toast.makeText(this,"点击了Activity调起的确认对话框 i="+i,Toast.LENGTH_LONG).show(); 49 | } 50 | 51 | @Override 52 | public boolean onKeyDown(int keyCode, KeyEvent event) { 53 | return super.onKeyDown(keyCode, event); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/adapter/FriendAdapter.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.adapter; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.niu.dialogfragment.R; 13 | import com.niu.dialogfragment.data.Friend; 14 | import com.niu.dialogfragment.widget.DialogFactory; 15 | import com.niu.dialogfragment.widget.ListDialogFragment; 16 | 17 | import java.util.List; 18 | 19 | 20 | /** 21 | * Created by niuxiaowei on 2016/1/19. 22 | */ 23 | public class FriendAdapter extends RecyclerView.Adapter { 24 | 25 | private List mFriends; 26 | 27 | private DialogFactory mDialogFactory; 28 | 29 | private Context mContext; 30 | 31 | private FriendsListener mListener; 32 | 33 | public ListDialogFragment.ListDialogListener listDialogListener = new ListDialogFragment.ListDialogListener() { 34 | @Override 35 | public void onItemClick(int position) { 36 | 37 | Toast.makeText(mContext,"点击了 po="+position,Toast.LENGTH_LONG).show(); 38 | 39 | } 40 | }; 41 | 42 | public static interface FriendsListener{ 43 | void onFriendItemLongClick(Friend longClickFriend); 44 | } 45 | 46 | public void setData(List datas) { 47 | this.mFriends = datas; 48 | this.notifyDataSetChanged(); 49 | } 50 | 51 | 52 | public FriendAdapter(Context context, DialogFactory dialogFactory,FriendsListener listener) { 53 | this.mContext = context; 54 | this.mDialogFactory = dialogFactory; 55 | dialogFactory.restoreDialogListener(this); 56 | this.mListener = listener; 57 | } 58 | 59 | @Override 60 | public FriendViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 61 | FriendViewHolder re = new FriendViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_friend_item, viewGroup, false)); 62 | return re; 63 | } 64 | 65 | 66 | 67 | @Override 68 | public void onBindViewHolder(FriendViewHolder friendViewHolder, int i) { 69 | 70 | final Friend f = mFriends.get(i); 71 | friendViewHolder.nameTv.setText(f.mName); 72 | 73 | if (i % 2 == 0) { 74 | friendViewHolder.nameTv.setBackgroundColor(Color.RED); 75 | } else { 76 | friendViewHolder.nameTv.setBackgroundColor(Color.WHITE); 77 | } 78 | friendViewHolder.nameTv.setOnLongClickListener(new View.OnLongClickListener() { 79 | @Override 80 | public boolean onLongClick(View v) { 81 | 82 | if (mDialogFactory != null) { 83 | 84 | mDialogFactory.showListDialog(new String[]{"delete","copy"}, true, listDialogListener); 85 | } 86 | 87 | if (mListener != null) { 88 | mListener.onFriendItemLongClick(f); 89 | } 90 | return true; 91 | } 92 | }); 93 | } 94 | 95 | 96 | @Override 97 | public long getItemId(int position) { 98 | return position; 99 | } 100 | 101 | @Override 102 | public int getItemCount() { 103 | if (mFriends != null) { 104 | return mFriends.size(); 105 | } 106 | return 0; 107 | } 108 | 109 | 110 | public static class FriendViewHolder extends RecyclerView.ViewHolder { 111 | TextView nameTv; 112 | 113 | public FriendViewHolder(View itemView) { 114 | super(itemView); 115 | nameTv = (TextView) itemView.findViewById(R.id.name); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/data/Friend.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.data; 2 | 3 | /** 4 | * Created by niuxiaowei on 2015/11/16. 5 | */ 6 | public class Friend { 7 | public String mName; 8 | public long mId; 9 | public int mAge; 10 | public String mUserId; 11 | public String mLoginId; 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/fragment/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.fragment; 2 | import android.app.Activity; 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | 6 | import com.niu.dialogfragment.activity.BaseActivity; 7 | import com.niu.dialogfragment.widget.BaseDialogFragment; 8 | import com.niu.dialogfragment.widget.DialogFactory; 9 | 10 | 11 | /** 12 | 13 | */ 14 | public abstract class BaseFragment extends Fragment { 15 | 16 | protected BaseActivity mBaseActivity; 17 | 18 | protected DialogFactory mDialogFactory ; 19 | 20 | public BaseDialogFragment.BaseDialogListener getDialogListener(){ 21 | return mDialogFactory.mListenerHolder.getDialogListener(); 22 | } 23 | 24 | /** 25 | * 清空DialogListenerHolder中的dialog listener 26 | */ 27 | public void clearDialogListener(){ 28 | mDialogFactory.mListenerHolder.setDialogListener(null); 29 | } 30 | 31 | @Override 32 | public void onSaveInstanceState(Bundle outState) { 33 | super.onSaveInstanceState(outState); 34 | mDialogFactory.mListenerHolder.saveDialogListenerKey(outState); 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | @Override 45 | public void onActivityCreated(Bundle savedInstanceState) { 46 | super.onActivityCreated(savedInstanceState); 47 | mDialogFactory = new DialogFactory(getChildFragmentManager(),savedInstanceState); 48 | mDialogFactory.restoreDialogListener(this); 49 | 50 | 51 | } 52 | 53 | @Override 54 | public void onAttach(Activity activity) { 55 | super.onAttach(activity); 56 | if(activity instanceof BaseActivity){ 57 | mBaseActivity = (BaseActivity)activity; 58 | } 59 | 60 | } 61 | 62 | @Override 63 | public void onDestroy() { 64 | super.onDestroy(); 65 | 66 | } 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/fragment/FriendFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.fragment; 2 | 3 | import android.content.DialogInterface; 4 | import android.os.Bundle; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.Toast; 11 | 12 | import com.niu.dialogfragment.R; 13 | import com.niu.dialogfragment.adapter.FriendAdapter; 14 | import com.niu.dialogfragment.data.Friend; 15 | import com.niu.dialogfragment.widget.ConfirmDialogFragment; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | import java.util.Random; 20 | 21 | 22 | /** 23 | * Created by niuxiaowei on 2016/1/19. 24 | */ 25 | public class FriendFragment extends BaseFragment implements ConfirmDialogFragment.ConfirmDialogListener{ 26 | private RecyclerView mRecyclerView; 27 | private FriendAdapter mAdapter; 28 | private FriendAdapter.FriendsListener mListener = new FriendAdapter.FriendsListener(){ 29 | 30 | Friend longClickFriend ; 31 | @Override 32 | public void onFriendItemLongClick(Friend longClickFriend) { 33 | this.longClickFriend = longClickFriend; 34 | } 35 | }; 36 | 37 | @Override 38 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 39 | return inflater.inflate(R.layout.fragment_friend,container,false); 40 | } 41 | 42 | @Override 43 | public void onActivityCreated(Bundle savedInstanceState) { 44 | super.onActivityCreated(savedInstanceState); 45 | initView(); 46 | addFriends(); 47 | } 48 | 49 | private void addFriends(){ 50 | List fs = new ArrayList(5); 51 | Random r = new Random(1000); 52 | for (int i = 0; i < 20; i++) { 53 | Friend f = new Friend(); 54 | f.mLoginId = "11111"; 55 | f.mUserId = r.nextLong() + ""; 56 | f.mName = "张三"+i; 57 | f.mAge = 20; 58 | fs.add(f); 59 | } 60 | mAdapter.setData(fs); 61 | } 62 | 63 | public void initView() { 64 | mRecyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView); 65 | mRecyclerView.setLayoutManager(new LinearLayoutManager(mBaseActivity)); 66 | mAdapter = new FriendAdapter(mBaseActivity,mDialogFactory,mListener); 67 | mRecyclerView.setAdapter(mAdapter); 68 | 69 | 70 | 71 | getView().findViewById(R.id.show_confi).setOnClickListener(new View.OnClickListener() { 72 | @Override 73 | public void onClick(View v) { 74 | mDialogFactory.showConfirmDialog("fragment调起的对话框","我是fragment调起的确认对话框",true,FriendFragment.this); 75 | } 76 | }); 77 | getView().findViewById(R.id.show_pro).setOnClickListener(new View.OnClickListener() { 78 | @Override 79 | public void onClick(View v) { 80 | mDialogFactory.showProgressDialog("fragment中调起的的进度条 ...",true); 81 | } 82 | }); 83 | } 84 | 85 | 86 | @Override 87 | public void onClick(DialogInterface dialog, int which) { 88 | Toast.makeText(getActivity(),"点击了fragment调起的确认对话框 which="+which,Toast.LENGTH_LONG).show(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/BaseDialogFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.support.v4.app.DialogFragment; 8 | 9 | import com.niu.dialogfragment.activity.BaseActivity; 10 | import com.niu.dialogfragment.fragment.BaseFragment; 11 | 12 | /** 13 | * Created by niuxiaowei on 2015/10/15. 14 | * 自定义dialog,是所有自定义dialog的基类 15 | */ 16 | public class BaseDialogFragment extends DialogFragment { 17 | 18 | protected BaseActivity mBaseActivity; 19 | 20 | 21 | private static final String EXTRA_DIALOG_TITLE_KEY = "extra_dialog_title_key"; 22 | private static final String EXTRA_DIALOG_MESSAGE_KEY = "extra_dialog_message_key"; 23 | private static final String EXTRA_DIALOG_CANELABLE_KEY = "extra_dialog_cancelable"; 24 | private static final String EXTRA_DIALOG_IS_CUSTOM_KEY = "extra_dialog_is_custom"; 25 | private static final String EXTRA_DIALOG_ID_KEY = "extra_dialog_id"; 26 | 27 | //是否是自定义dialog 28 | protected boolean mIsCustomDialog = false; 29 | //每个对话框的ID 30 | protected int mDialogId; 31 | protected boolean mIsCancelable; 32 | protected String mTitle; 33 | 34 | 35 | protected boolean mIsParseDialogListener; 36 | /** 37 | * 基础的dialog listener,没有提供任何的方法,扩展的dialog,若该dialog有listener则必须继承本接口 38 | */ 39 | public static interface BaseDialogListener{ 40 | 41 | } 42 | 43 | @Override 44 | public void onResume() { 45 | super.onResume(); 46 | BaseDialogListener listener = null; 47 | 48 | 49 | if(!mIsParseDialogListener){ 50 | mIsParseDialogListener = true; 51 | 52 | 53 | /*/解析dialog listener,fragment的级别要大于activity,若 (getParentFragment() instanceof BaseFragment)为true 54 | * ,表明是一个fragment调起的dialog,否则是一个activity调起的diaolog 55 | * */ 56 | if (getParentFragment() instanceof BaseFragment) { 57 | listener = ((BaseFragment) getParentFragment()).getDialogListener(); 58 | }else if(mBaseActivity != null){ 59 | listener = mBaseActivity.getDialogListener(); 60 | } 61 | if (listener != null) { 62 | onReceiveDialogListener(listener); 63 | } 64 | } 65 | } 66 | 67 | @Override 68 | public void onAttach(Activity activity) { 69 | super.onAttach(activity); 70 | if (getActivity() instanceof BaseActivity) { 71 | mBaseActivity = (BaseActivity) getActivity(); 72 | } 73 | 74 | 75 | } 76 | 77 | /** 78 | * 接收dialog listener对象,具体由子类进行实现 79 | * @param listener 80 | */ 81 | protected void onReceiveDialogListener(BaseDialogListener listener){ 82 | 83 | } 84 | 85 | 86 | /** 87 | * 从bundle中解析参数,args有可能来自fragment被系统回收,然后界面又重新被启动系统保存的参数;也有可能是其他使用者带过来的 88 | * ,具体实现交给子类 89 | * 90 | * @param args 91 | */ 92 | protected void parseArgs(Bundle args) { 93 | mDialogId = args.getInt(EXTRA_DIALOG_ID_KEY); 94 | mTitle = args.getString(EXTRA_DIALOG_TITLE_KEY); 95 | mIsCancelable = args.getBoolean(EXTRA_DIALOG_CANELABLE_KEY); 96 | mIsCustomDialog = args.getBoolean(EXTRA_DIALOG_IS_CUSTOM_KEY); 97 | } 98 | 99 | @Override 100 | public void onCreate(Bundle savedInstanceState) { 101 | super.onCreate(savedInstanceState); 102 | parseArgs( getArguments()); 103 | setCancelable(mIsCancelable); 104 | 105 | } 106 | 107 | @Override 108 | public void onDestroy() { 109 | super.onDestroy(); 110 | /*销毁basefragment或BaseActivity中的dialog listener, 111 | * 同理BaseFragment级别要高于BaseActivity 112 | * */ 113 | if (getParentFragment() instanceof BaseFragment) { 114 | ((BaseFragment) getParentFragment()).clearDialogListener(); 115 | }else if(mBaseActivity != null){ 116 | mBaseActivity.clearDialogListener(); 117 | } 118 | 119 | } 120 | 121 | 122 | protected static void putIdParam(Bundle args, int dialogId) { 123 | if (args != null) { 124 | args.putInt(EXTRA_DIALOG_ID_KEY, dialogId); 125 | } 126 | } 127 | 128 | @NonNull 129 | protected static void putIsCustomParam(Bundle args, boolean isCustomDialog) { 130 | args.putBoolean(EXTRA_DIALOG_IS_CUSTOM_KEY, isCustomDialog); 131 | } 132 | 133 | @NonNull 134 | protected static void putTitleParam(Bundle bundler, String title) { 135 | 136 | bundler.putString(EXTRA_DIALOG_TITLE_KEY, title); 137 | } 138 | 139 | @NonNull 140 | protected static void putCancelableParam(Bundle bundle, boolean cancelable) { 141 | bundle.putBoolean(EXTRA_DIALOG_CANELABLE_KEY, cancelable); 142 | } 143 | 144 | @NonNull 145 | protected static void putMessageParam(Bundle bundler, String message) { 146 | 147 | bundler.putString(EXTRA_DIALOG_MESSAGE_KEY, message); 148 | } 149 | 150 | 151 | protected String parseMessageParam() { 152 | Bundle bundle = getArguments(); 153 | if (bundle == null) { 154 | return null; 155 | } 156 | return bundle.getString(EXTRA_DIALOG_MESSAGE_KEY); 157 | } 158 | 159 | 160 | } 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/ConfirmDialogFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.app.ProgressDialog; 7 | import android.content.DialogInterface; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.annotation.Nullable; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.view.Window; 15 | 16 | import com.niu.dialogfragment.R; 17 | 18 | 19 | /** 20 | * Created by niuxiaowei on 2015/10/16. 21 | */ 22 | public class ConfirmDialogFragment extends BaseDialogFragment { 23 | 24 | private String message ; 25 | private ConfirmDialogListener mListener; 26 | 27 | /** 28 | * 确认对话框的listener 29 | */ 30 | public interface ConfirmDialogListener extends BaseDialogListener,DialogInterface.OnClickListener{ 31 | 32 | } 33 | 34 | /** 35 | * @param title 36 | * @param message 37 | * @param cancelable 38 | * @return 39 | */ 40 | public static ConfirmDialogFragment newInstance(String title, String message,boolean cancelable){ 41 | ConfirmDialogFragment instance = new ConfirmDialogFragment(); 42 | Bundle args = new Bundle(); 43 | putTitleParam(args, title); 44 | putMessageParam(args, message); 45 | putCancelableParam(args, cancelable); 46 | instance.setArguments(args); 47 | return instance; 48 | } 49 | 50 | @Override 51 | protected void onReceiveDialogListener(BaseDialogListener listener) { 52 | if(listener instanceof ConfirmDialogListener){ 53 | mListener = (ConfirmDialogListener)listener; 54 | } 55 | } 56 | 57 | @NonNull 58 | @Override 59 | public Dialog onCreateDialog(Bundle savedInstanceState) { 60 | if(!mIsCustomDialog){ 61 | 62 | 63 | AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle(mTitle== null?getString(R.string.app_name):mTitle).setMessage(message== null?" ":message) 64 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 65 | @Override 66 | public void onClick(DialogInterface dialog, int which) { 67 | mListener.onClick(dialog,which); 68 | } 69 | }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 70 | @Override 71 | public void onClick(DialogInterface dialog, int which) { 72 | mListener.onClick(dialog,which); 73 | } 74 | }).create(); 75 | return dialog; 76 | }else{ 77 | return super.onCreateDialog(savedInstanceState); 78 | } 79 | } 80 | 81 | @Override 82 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 83 | super.onViewCreated(view, savedInstanceState); 84 | } 85 | 86 | @Nullable 87 | @Override 88 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 89 | if(mIsCustomDialog){ 90 | View view = inflater.inflate(R.layout.dialog_custom_progress,container,false); 91 | //启用窗体的扩展特性。 92 | getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 93 | return view; 94 | }else{ 95 | return super.onCreateView(inflater,container,savedInstanceState); 96 | } 97 | 98 | } 99 | 100 | @Override 101 | protected void parseArgs(Bundle args) { 102 | super.parseArgs(args); 103 | message = parseMessageParam(); 104 | 105 | } 106 | 107 | @Override 108 | public void onCreate(@Nullable Bundle savedInstanceState) { 109 | super.onCreate(savedInstanceState); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/DialogFactory.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.DialogFragment; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentManager; 7 | import android.support.v4.app.FragmentTransaction; 8 | 9 | 10 | 11 | /** 12 | * Created by niuxiaowei on 2016/2/3. 13 | * 对话框工厂 14 | */ 15 | public class DialogFactory { 16 | 17 | /** 18 | * 进度条的tag 19 | */ 20 | private static final String DIALOG_PROGRESS_TAG = "progress"; 21 | 22 | private static final String DIALOG_CONFIRM_TAG = "confirm"; 23 | private static final String DIALOG_LIST_TAG = "list"; 24 | 25 | private FragmentManager mFragmentManager; 26 | 27 | 28 | public DialogListenerHolder mListenerHolder = new DialogListenerHolder(); 29 | 30 | public DialogFactory(FragmentManager fragmentManager, Bundle savedInstanceState){ 31 | this.mFragmentManager = fragmentManager; 32 | mListenerHolder.getDialogListenerKey(savedInstanceState); 33 | } 34 | 35 | 36 | /** 37 | * 这个方法很重要,是恢复dialog listener的一个关键点,在初始化DialogFactory或把DialogFactory赋值后,调用该方法,把调用该方法所在 38 | * 的类的实例作为参数。 该方法会把param中的属性依次遍历,尝试找属性是BaseDialogFragment.BaseDialogListener的实例, 39 | * 并且该属性就是保存在bundle中的dialog listener key对应的dialog listener 40 | * @param o 41 | */ 42 | public void restoreDialogListener(Object o){ 43 | mListenerHolder.restoreDialogListener(o); 44 | } 45 | 46 | 47 | /** 48 | * @param message 进度条显示的信息 49 | * @param cancelable 点击空白处是否可以取消 50 | */ 51 | public void showProgressDialog(String message, boolean cancelable){ 52 | if(mFragmentManager != null){ 53 | 54 | /** 55 | * 为了不重复显示dialog,在显示对话框之前移除正在显示的对话框。 56 | */ 57 | FragmentTransaction ft = mFragmentManager.beginTransaction(); 58 | Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_PROGRESS_TAG); 59 | if (null != fragment) { 60 | ft.remove(fragment).commit(); 61 | } 62 | 63 | ProgressDialogFragment progressDialogFragment = ProgressDialogFragment.newInstance(message, cancelable); 64 | progressDialogFragment.show(mFragmentManager, DIALOG_PROGRESS_TAG); 65 | 66 | mFragmentManager.executePendingTransactions(); 67 | } 68 | } 69 | 70 | /** 71 | * 取消进度条 72 | */ 73 | public void dissProgressDialog(){ 74 | Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_PROGRESS_TAG); 75 | if (null != fragment) { 76 | ((ProgressDialogFragment)fragment).dismiss(); 77 | mFragmentManager.beginTransaction().remove(fragment).commit(); 78 | } 79 | } 80 | 81 | /** 82 | * //显示确认对话框,dialogId是用来区分不同对话框的 83 | 84 | * @param title 对话框title 85 | * @param message 86 | * @param cancelable 87 | * @param listener 88 | */ 89 | public void showConfirmDialog(String title,String message,boolean cancelable, ConfirmDialogFragment.ConfirmDialogListener listener){ 90 | 91 | FragmentTransaction ft = mFragmentManager.beginTransaction(); 92 | Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_CONFIRM_TAG); 93 | if (null != fragment) { 94 | ft.remove(fragment); 95 | } 96 | DialogFragment df = ConfirmDialogFragment.newInstance(title, message, cancelable); 97 | df.show(mFragmentManager,DIALOG_CONFIRM_TAG); 98 | mFragmentManager.executePendingTransactions(); 99 | 100 | mListenerHolder.setDialogListener(listener); 101 | } 102 | 103 | /** 104 | * 显示列表对话框 105 | * @param items 106 | * @param cancelable 107 | * @param listDialogListener 108 | */ 109 | public void showListDialog(String[] items,boolean cancelable,ListDialogFragment.ListDialogListener listDialogListener){ 110 | FragmentTransaction ft = mFragmentManager.beginTransaction(); 111 | Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_LIST_TAG); 112 | if (null != fragment) { 113 | ft.remove(fragment); 114 | } 115 | DialogFragment df = ListDialogFragment.newInstance(items,cancelable); 116 | df.show(mFragmentManager,DIALOG_LIST_TAG); 117 | mFragmentManager.executePendingTransactions(); 118 | 119 | mListenerHolder.setDialogListener(listDialogListener); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/DialogListenerHolder.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | import android.os.Bundle; 4 | 5 | import java.lang.reflect.Field; 6 | 7 | /** 8 | * 对话框listener持有者 9 | * Created by niuxiaowei on 2016/2/19. 10 | */ 11 | public class DialogListenerHolder { 12 | 13 | private BaseDialogFragment.BaseDialogListener mDialogListener; 14 | 15 | /** 16 | * 对话框的listener的key值,用类名作为key值,主要用来在手机配置发生变化时(横屏换为竖屏),当现场恢复时,能正确的找到对话框的listener 17 | */ 18 | private String mDialogListenerKey; 19 | 20 | /** 21 | * 获取dialog的listener,具体由子类实现 22 | * @return 23 | */ 24 | public BaseDialogFragment.BaseDialogListener getDialogListener(){ 25 | return mDialogListener; 26 | } 27 | 28 | 29 | public void setDialogListener(BaseDialogFragment.BaseDialogListener listener){ 30 | mDialogListener = listener; 31 | mDialogListenerKey = listener == null ?null:listener.getClass().getName(); 32 | } 33 | 34 | /** 35 | * 把listener的key值保存在bundle中,配置发生变化的情况下(横屏换为竖屏),在从bundle中取listener的key值 36 | * @param outState 37 | */ 38 | public void saveDialogListenerKey(Bundle outState){ 39 | if(outState != null){ 40 | outState.putString("key",mDialogListenerKey); 41 | } 42 | } 43 | 44 | /** 45 | * 从bundle中尝试取出dialog listener key 46 | * @param savedInstanceState 47 | */ 48 | public void getDialogListenerKey(Bundle savedInstanceState){ 49 | if(savedInstanceState != null){ 50 | mDialogListenerKey = savedInstanceState.getString("key"); 51 | } 52 | } 53 | 54 | /** 55 | * 这个方法很重要,是恢复dialog listener的一个关键点,在初始化DialogFactory或把DialogFactory赋值后,调用该方法,把调用该方法所在 56 | * 的类的实例作为参数。 该方法会把param中的属性依次遍历,尝试找属性是BaseDialogFragment.BaseDialogListener的实例, 57 | * 并且该属性就是保存在bundle中的dialog listener key对应的dialog listener 58 | * @param o 59 | */ 60 | public void restoreDialogListener(Object o){ 61 | if(o == null){ 62 | return; 63 | } 64 | if(!isNeedRestoreDialogListener()){ 65 | return; 66 | } 67 | 68 | //先尝试找传进来的实例 69 | if(o instanceof BaseDialogFragment.BaseDialogListener && o.getClass().getName().equals(mDialogListenerKey)){ 70 | setDialogListener((BaseDialogFragment.BaseDialogListener)o); 71 | return; 72 | } 73 | Class c = o.getClass(); 74 | Field[] fs = c.getDeclaredFields(); 75 | for (int i = 0; i < fs.length; i++) { 76 | Field f = fs[i]; 77 | try { 78 | Object instance = f.get(o); 79 | if((instance instanceof BaseDialogFragment.BaseDialogListener) && instance.getClass().getName().equals(mDialogListenerKey)){ 80 | setDialogListener((BaseDialogFragment.BaseDialogListener) f.get(o)); 81 | 82 | } 83 | } catch (IllegalAccessException e) { 84 | e.printStackTrace(); 85 | } 86 | 87 | 88 | } 89 | } 90 | 91 | private boolean isNeedRestoreDialogListener(){ 92 | return mDialogListenerKey == null? false:mDialogListener== null; 93 | 94 | } 95 | 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/ListDialogFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | import android.app.AlertDialog; 4 | import android.app.Dialog; 5 | import android.content.DialogInterface; 6 | import android.os.Bundle; 7 | import android.support.annotation.NonNull; 8 | import android.support.annotation.Nullable; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.Window; 13 | 14 | import com.niu.dialogfragment.R; 15 | 16 | 17 | /** 18 | * Created by niuxiaowei on 2016/2/3. 19 | * 列表dialog 20 | */ 21 | public class ListDialogFragment extends BaseDialogFragment{ 22 | 23 | private String[] mItemContents; 24 | private ListDialogListener mListener; 25 | 26 | public static interface ListDialogListener extends BaseDialogListener{ 27 | void onItemClick(int position); 28 | } 29 | 30 | /** 31 | * @param itemContents 32 | * @param cancelable 33 | * @return 34 | */ 35 | public static ListDialogFragment newInstance(String[] itemContents,boolean cancelable){ 36 | 37 | ListDialogFragment dialog = new ListDialogFragment(); 38 | Bundle bundle = new Bundle(); 39 | bundle.putStringArray("items", itemContents); 40 | putCancelableParam(bundle, cancelable); 41 | dialog.setArguments(bundle); 42 | return dialog; 43 | } 44 | 45 | @Override 46 | protected void onReceiveDialogListener(BaseDialogListener listener) { 47 | super.onReceiveDialogListener(listener); 48 | if(listener instanceof ListDialogListener){ 49 | mListener = (ListDialogListener)listener; 50 | } 51 | } 52 | 53 | @NonNull 54 | @Override 55 | public Dialog onCreateDialog(Bundle savedInstanceState) { 56 | if(!mIsCustomDialog){ 57 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setItems(mItemContents, new DialogInterface.OnClickListener() { 58 | @Override 59 | public void onClick(DialogInterface dialog, int which) { 60 | if(mListener != null){ 61 | mListener.onItemClick(which); 62 | } 63 | } 64 | }); 65 | 66 | return builder.create(); 67 | }else{ 68 | return super.onCreateDialog(savedInstanceState); 69 | } 70 | } 71 | 72 | 73 | 74 | @Override 75 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 76 | super.onViewCreated(view, savedInstanceState); 77 | } 78 | 79 | @Nullable 80 | @Override 81 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 82 | if(mIsCustomDialog){ 83 | View view = inflater.inflate(R.layout.dialog_custom_progress,container,false); 84 | //启用窗体的扩展特性。 85 | getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 86 | return view; 87 | }else{ 88 | return super.onCreateView(inflater,container,savedInstanceState); 89 | } 90 | } 91 | 92 | 93 | @Override 94 | protected void parseArgs(Bundle args) { 95 | super.parseArgs(args); 96 | mItemContents = args.getStringArray("items"); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/niu/dialogfragment/widget/ProgressDialogFragment.java: -------------------------------------------------------------------------------- 1 | package com.niu.dialogfragment.widget; 2 | 3 | import android.app.Dialog; 4 | import android.app.ProgressDialog; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.view.Window; 12 | 13 | import com.niu.dialogfragment.R; 14 | 15 | 16 | /** 17 | * Created by niuxiaowei on 2015/10/15. 18 | * 自定义进度dialog 19 | */ 20 | public class ProgressDialogFragment extends BaseDialogFragment { 21 | 22 | public static ProgressDialogFragment newInstance(String message, boolean cancelable){ 23 | 24 | ProgressDialogFragment dialog = new ProgressDialogFragment(); 25 | Bundle bundle = new Bundle(); 26 | putMessageParam(bundle,message); 27 | putCancelableParam(bundle,cancelable); 28 | dialog.setArguments(bundle); 29 | return dialog; 30 | } 31 | 32 | 33 | @NonNull 34 | @Override 35 | public Dialog onCreateDialog(Bundle savedInstanceState) { 36 | if(!mIsCustomDialog){ 37 | 38 | ProgressDialog dialog = new ProgressDialog(getActivity()); 39 | String message = parseMessageParam(); 40 | dialog.setMessage(message); 41 | dialog.setCancelable(mIsCancelable); 42 | return dialog; 43 | }else{ 44 | return super.onCreateDialog(savedInstanceState); 45 | } 46 | } 47 | 48 | @Override 49 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 50 | super.onViewCreated(view, savedInstanceState); 51 | } 52 | 53 | @Nullable 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 56 | if(mIsCustomDialog){ 57 | View view = inflater.inflate(R.layout.dialog_custom_progress,container,false); 58 | //启用窗体的扩展特性。 59 | getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 60 | return view; 61 | }else{ 62 | return super.onCreateView(inflater,container,savedInstanceState); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_friend.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 15 | 19 | 20 |