├── .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 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
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 |
73 |
74 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
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 |
91 |
113 |
114 |
115 |
1207 |
1208 |
1209 |
1212 |
1213 |
1214 |
1524 |
1525 |
1526 |
1527 |
1528 |
1529 |
1530 |
1531 |
1532 |
1533 |
1534 |
1535 |
1536 |
1537 |
1538 |
1539 |
1540 |
1541 |
1542 |
1543 |
1544 |
1545 |
1546 |
1547 |
1548 |
1549 |
1550 |
1551 |
1552 |
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 |
1582 |
1583 |
1584 |
1585 |
1586 |
1587 |
1588 |
1589 |
1590 |
1591 |
1592 |
1593 |
1594 |
1595 |
1596 |
1597 |
1598 |
1599 |
1600 |
1601 |
1602 |
1603 |
1604 |
1605 |
1606 |
1607 |
1608 |
1609 |
1610 |
1611 |
1612 |
1613 |
1614 |
1615 |
1616 |
1617 |
1618 |
1619 |
1620 |
1621 |
1622 |
1623 |
1624 |
1625 |
1626 |
1627 |
1628 |
1629 |
1630 |
1631 |
1632 |
1633 |
1634 |
1635 |
1636 |
1637 |
1638 |
1639 |
1640 |
1641 |
1642 |
1643 |
1644 |
1645 |
1646 |
1647 |
1648 |
1649 |
1650 |
1651 |
1652 |
1653 |
1654 |
1655 |
1656 |
1657 |
1658 |
1659 |
1660 |
1661 |
1662 |
1663 |
1664 |
1665 |
1666 |
1667 |
1668 |
1669 |
1670 |
1671 |
1672 |
1673 |
1674 |
1675 |
1676 |
1677 |
1678 |
1679 |
1680 |
1681 |
1682 |
1683 |
1684 |
1685 |
1686 |
1687 |
1688 |
1689 |
1690 |
1691 |
1692 |
1693 |
1694 |
1695 |
1696 |
1697 |
1698 |
1699 |
1700 |
1701 |
1702 |
1703 |
1704 |
1705 |
1706 |
1707 |
1708 |
1709 |
1710 |
1711 |
1712 |
1713 |
1714 |
1715 |
1716 |
1717 |
1718 |
1719 |
1720 |
1721 |
1722 |
1723 |
1724 |
1725 |
1726 |
1727 |
1728 |
1729 |
1730 |
1731 |
1732 |
1733 |
1734 |
1735 |
1736 |
1737 |
1738 |
1739 |
1740 |
1741 |
1742 |
1743 |
1744 |
1745 |
1746 |
1747 |
1748 |
1749 |
1750 |
1751 |
1752 |
1753 |
1754 |
1755 |
1756 |
1757 |
1758 |
1759 |
1760 |
1761 |
1762 |
1763 |
1764 |
1765 |
1766 |
1767 |
1768 |
1769 |
1770 |
1771 |
1772 |
1773 |
1774 |
1775 |
1776 |
1777 |
1778 |
1779 |
1780 |
1781 |
1782 |
1783 |
1784 |
1785 |
1786 |
1787 |
1788 |
1789 |
1790 |
1791 |
1792 |
1793 |
1794 |
1795 |
1796 |
1797 |
1798 |
1799 |
1800 |
1801 |
1802 |
1803 |
1804 |
1805 |
1806 |
1807 |
1808 |
1809 |
1810 |
1811 |
1812 |
1813 |
1814 |
1815 |
1816 |
1817 |
1818 |
1819 |
1820 |
1821 |
1822 |
1823 |
1824 |
1825 |
1826 |
1827 |
1828 |
1829 |
1830 |
1831 |
1832 |
1833 |
1834 |
1835 |
1836 |
1837 |
1838 |
1839 |
1840 |
1841 |
1842 |
1843 |
1844 |
1845 |
1846 |
1847 |
1848 |
1849 |
1850 |
1851 |
1852 |
1853 |
1854 |
1855 |
1856 |
1857 |
1858 |
1859 |
1860 |
1861 |
1862 |
1863 |
1864 |
1865 |
1866 |
1867 |
1868 |
1869 |
1870 |
1871 |
1872 | 1460898029156
1873 |
1874 | 1460898029156
1875 |
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 |
1946 |
1947 |
1948 |
1949 |
1950 |
1951 | file://$PROJECT_DIR$/app/src/main/java/com/niu/dialogfragment/widget/DialogFactory.java
1952 | 58
1953 |
1954 |
1955 |
1956 |
1957 |
1958 |
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 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/DialogFragmentUse3.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
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 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
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 |
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 |
25 |
26 |
31 |
32 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/adapter_friend_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_custom_progress.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_friend.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
15 |
20 |
21 |
26 |
27 |
28 |
29 |
33 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/niuxiaowei/DialogFragmentUse/d90da645a67c302e019b8a55c77c8a9fc56c2fee/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/niuxiaowei/DialogFragmentUse/d90da645a67c302e019b8a55c77c8a9fc56c2fee/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/niuxiaowei/DialogFragmentUse/d90da645a67c302e019b8a55c77c8a9fc56c2fee/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/niuxiaowei/DialogFragmentUse/d90da645a67c302e019b8a55c77c8a9fc56c2fee/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DialogFragmentUse
3 |
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/niuxiaowei/DialogFragmentUse/d90da645a67c302e019b8a55c77c8a9fc56c2fee/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------