Skip to content

Commit d45d02d

Browse files
author
Othershe
committed
fix
1 parent 3424e61 commit d45d02d

File tree

7 files changed

+144
-69
lines changed

7 files changed

+144
-69
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# NiceDialog
22

33
### 简介:
4-
基于DialogFragment的扩展,让dialog的使用更方便
4+
* 基于DialogFragment的扩展,让dialog的使用更方便
5+
* 原理:<http://www.jianshu.com/p/0529433d4522>
56
### 示例预览:
67
![](https://github.com/Othershe/NiceDialog/blob/master/image/share.gif) ![](https://github.com/Othershe/NiceDialog/blob/master/image/set.gif)
78
![](https://github.com/Othershe/NiceDialog/blob/master/image/commit.gif) ![](https://github.com/Othershe/NiceDialog/blob/master/image/red_packet.gif)
@@ -21,7 +22,7 @@ allprojects {
2122
**Step 2. 添加项目依赖**
2223
``` gradle
2324
dependencies {
24-
compile 'com.github.Othershe:NiceDialog:1.0.0'
25+
compile 'com.github.Othershe:NiceDialog:1.1.0'
2526
}
2627
```
2728
**Step 3. 配置、展示dialog**

app/src/main/java/com/othershe/test/MainActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.View;
88
import android.view.inputmethod.InputMethodManager;
99
import android.widget.EditText;
10+
import android.widget.Toast;
1011

1112
import com.othershe.nicedialog.BaseNiceDialog;
1213
import com.othershe.nicedialog.NiceDialog;
@@ -27,7 +28,12 @@ public void showDialog0(View view) {
2728
.setConvertListener(new ViewConvertListener() {
2829
@Override
2930
public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
30-
31+
holder.setOnClickListener(R.id.wechat, new View.OnClickListener() {
32+
@Override
33+
public void onClick(View v) {
34+
Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
35+
}
36+
});
3137
}
3238
})
3339
.setDimAmount(0.3f)
Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:id="@+id/activity_main"
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
43
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
android:orientation="vertical"
7-
android:paddingBottom="@dimen/activity_vertical_margin"
8-
android:paddingLeft="@dimen/activity_horizontal_margin"
9-
android:paddingRight="@dimen/activity_horizontal_margin"
10-
android:paddingTop="@dimen/activity_vertical_margin">
11-
12-
<Button
13-
android:layout_width="match_parent"
14-
android:layout_height="wrap_content"
15-
android:onClick="showDialog0"
16-
android:text="分享" />
4+
android:layout_height="wrap_content">
175

18-
<Button
6+
<LinearLayout
197
android:layout_width="match_parent"
208
android:layout_height="wrap_content"
21-
android:layout_marginTop="10dp"
22-
android:onClick="showDialog1"
23-
android:text="好友设置" />
9+
android:orientation="vertical"
10+
android:paddingBottom="@dimen/activity_vertical_margin"
11+
android:paddingLeft="@dimen/activity_horizontal_margin"
12+
android:paddingRight="@dimen/activity_horizontal_margin"
13+
android:paddingTop="@dimen/activity_vertical_margin">
2414

25-
<Button
26-
android:layout_width="match_parent"
27-
android:layout_height="wrap_content"
28-
android:layout_marginTop="10dp"
29-
android:onClick="showDialog2"
30-
android:text="评论" />
15+
<Button
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:onClick="showDialog0"
19+
android:text="分享" />
3120

32-
<Button
33-
android:layout_width="match_parent"
34-
android:layout_height="wrap_content"
35-
android:layout_marginTop="10dp"
36-
android:onClick="showDialog3"
37-
android:text="红包" />
21+
<Button
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:layout_marginTop="10dp"
25+
android:onClick="showDialog1"
26+
android:text="好友设置" />
3827

39-
<Button
40-
android:layout_width="match_parent"
41-
android:layout_height="wrap_content"
42-
android:layout_marginTop="10dp"
43-
android:onClick="showDialog4"
44-
android:text="加载提示" />
28+
<Button
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"
31+
android:layout_marginTop="10dp"
32+
android:onClick="showDialog2"
33+
android:text="评论" />
4534

46-
<Button
47-
android:layout_width="match_parent"
48-
android:layout_height="wrap_content"
49-
android:layout_marginTop="10dp"
50-
android:onClick="showDialog5"
51-
android:text="确定-0" />
35+
<Button
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
android:layout_marginTop="10dp"
39+
android:onClick="showDialog3"
40+
android:text="红包" />
5241

53-
<Button
54-
android:layout_width="match_parent"
55-
android:layout_height="wrap_content"
56-
android:layout_marginTop="10dp"
57-
android:onClick="showDialog6"
58-
android:text="确定-1" />
59-
</LinearLayout>
42+
<Button
43+
android:layout_width="match_parent"
44+
android:layout_height="wrap_content"
45+
android:layout_marginTop="10dp"
46+
android:onClick="showDialog4"
47+
android:text="加载提示" />
48+
49+
<Button
50+
android:layout_width="match_parent"
51+
android:layout_height="wrap_content"
52+
android:layout_marginTop="10dp"
53+
android:onClick="showDialog5"
54+
android:text="确定-0" />
55+
56+
<Button
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:layout_marginTop="10dp"
60+
android:onClick="showDialog6"
61+
android:text="确定-1" />
62+
63+
</LinearLayout>
64+
65+
</ScrollView>

app/src/main/res/layout/share_layout.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
<LinearLayout
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
27-
android:paddingTop="15dp"
28-
android:paddingBottom="15dp">
27+
android:paddingBottom="15dp"
28+
android:paddingTop="15dp">
2929

3030
<LinearLayout
31+
android:id="@+id/wechat"
3132
android:layout_width="0dp"
3233
android:layout_height="wrap_content"
3334
android:layout_weight="1"

nicedialog/src/main/java/com/othershe/nicedialog/BaseNiceDialog.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.othershe.nicedialog;
22

33
import android.os.Bundle;
4+
import android.support.annotation.LayoutRes;
45
import android.support.annotation.Nullable;
56
import android.support.annotation.StyleRes;
67
import android.support.v4.app.DialogFragment;
@@ -13,31 +14,53 @@
1314
import android.view.WindowManager;
1415

1516
public abstract class BaseNiceDialog extends DialogFragment {
16-
17-
private int margin;//
18-
private int width;
19-
private int height;
20-
private float dimAmount = 0.5f;
21-
private boolean showBottom;
22-
private boolean outCancel = true;
17+
private static final String MARGIN = "margin";
18+
private static final String WIDTH = "width";
19+
private static final String HEIGHT = "height";
20+
private static final String DIM = "dim_amount";
21+
private static final String BOTTOM = "show_bottom";
22+
private static final String CANCEL = "out_cancel";
23+
private static final String ANIM = "anim_style";
24+
private static final String LAYOUT = "layout_id";
25+
26+
private int margin;//左右边距
27+
private int width;//宽度
28+
private int height;//高度
29+
private float dimAmount = 0.5f;//灰度深浅
30+
private boolean showBottom;//是否底部显示
31+
private boolean outCancel = true;//是否点击外部取消
2332
@StyleRes
2433
private int animStyle;
34+
@LayoutRes
35+
protected int layoutId;
2536

2637
public abstract int intLayoutId();
2738

2839
public abstract void convertView(ViewHolder holder, BaseNiceDialog dialog);
2940

30-
3141
@Override
3242
public void onCreate(@Nullable Bundle savedInstanceState) {
3343
super.onCreate(savedInstanceState);
3444
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NiceDialog);
45+
layoutId = intLayoutId();
46+
47+
//恢复保存的数据
48+
if (savedInstanceState != null) {
49+
margin = savedInstanceState.getInt(MARGIN);
50+
width = savedInstanceState.getInt(WIDTH);
51+
height = savedInstanceState.getInt(HEIGHT);
52+
dimAmount = savedInstanceState.getFloat(DIM);
53+
showBottom = savedInstanceState.getBoolean(BOTTOM);
54+
outCancel = savedInstanceState.getBoolean(CANCEL);
55+
animStyle = savedInstanceState.getInt(ANIM);
56+
layoutId = savedInstanceState.getInt(LAYOUT);
57+
}
3558
}
3659

3760
@Nullable
3861
@Override
3962
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40-
View view = inflater.inflate(intLayoutId(), container, false);
63+
View view = inflater.inflate(layoutId, container, false);
4164
convertView(ViewHolder.create(view), this);
4265
return view;
4366
}
@@ -48,6 +71,24 @@ public void onStart() {
4871
initParams();
4972
}
5073

74+
/**
75+
* 屏幕旋转等导致DialogFragment销毁后重建时保存数据
76+
*
77+
* @param outState
78+
*/
79+
@Override
80+
public void onSaveInstanceState(Bundle outState) {
81+
super.onSaveInstanceState(outState);
82+
outState.putInt(MARGIN, margin);
83+
outState.putInt(WIDTH, width);
84+
outState.putInt(HEIGHT, height);
85+
outState.putFloat(DIM, dimAmount);
86+
outState.putBoolean(BOTTOM, showBottom);
87+
outState.putBoolean(CANCEL, outCancel);
88+
outState.putInt(ANIM, animStyle);
89+
outState.putInt(LAYOUT, layoutId);
90+
}
91+
5192
private void initParams() {
5293
Window window = getDialog().getWindow();
5394
if (window != null) {
@@ -78,7 +119,6 @@ private void initParams() {
78119
setCancelable(outCancel);
79120
}
80121

81-
82122
public BaseNiceDialog setMargin(int margin) {
83123
this.margin = margin;
84124
return this;

nicedialog/src/main/java/com/othershe/nicedialog/NiceDialog.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.othershe.nicedialog;
22

3+
import android.os.Bundle;
34
import android.support.annotation.LayoutRes;
5+
import android.support.annotation.Nullable;
46

57
public class NiceDialog extends BaseNiceDialog {
6-
7-
@LayoutRes
8-
private int layoutId;
98
private ViewConvertListener convertListener;
109

1110
public static NiceDialog init() {
@@ -17,7 +16,6 @@ public int intLayoutId() {
1716
return layoutId;
1817
}
1918

20-
2119
@Override
2220
public void convertView(ViewHolder holder, BaseNiceDialog dialog) {
2321
if (convertListener != null) {
@@ -35,4 +33,23 @@ public NiceDialog setConvertListener(ViewConvertListener convertListener) {
3533
this.convertListener = convertListener;
3634
return this;
3735
}
36+
37+
@Override
38+
public void onCreate(@Nullable Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
if (savedInstanceState != null) {
41+
convertListener = (ViewConvertListener) savedInstanceState.getSerializable("listener");
42+
}
43+
}
44+
45+
/**
46+
* 保存接口
47+
*
48+
* @param outState
49+
*/
50+
@Override
51+
public void onSaveInstanceState(Bundle outState) {
52+
super.onSaveInstanceState(outState);
53+
outState.putSerializable("listener", convertListener);
54+
}
3855
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.othershe.nicedialog;
22

3-
public interface ViewConvertListener {
3+
import java.io.Serializable;
4+
5+
public interface ViewConvertListener extends Serializable {
6+
long serialVersionUID = System.currentTimeMillis();
7+
48
void convertView(ViewHolder holder, BaseNiceDialog dialog);
59
}

0 commit comments

Comments
 (0)