Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Othershe committed Aug 3, 2017
1 parent 3424e61 commit d45d02d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 69 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# NiceDialog

### 简介:
基于DialogFragment的扩展,让dialog的使用更方便
* 基于DialogFragment的扩展,让dialog的使用更方便
* 原理:<http://www.jianshu.com/p/0529433d4522>
### 示例预览:
![](https://github.com/Othershe/NiceDialog/blob/master/image/share.gif) ![](https://github.com/Othershe/NiceDialog/blob/master/image/set.gif)
![](https://github.com/Othershe/NiceDialog/blob/master/image/commit.gif) ![](https://github.com/Othershe/NiceDialog/blob/master/image/red_packet.gif)
Expand All @@ -21,7 +22,7 @@ allprojects {
**Step 2. 添加项目依赖**
``` gradle
dependencies {
compile 'com.github.Othershe:NiceDialog:1.0.0'
compile 'com.github.Othershe:NiceDialog:1.1.0'
}
```
**Step 3. 配置、展示dialog**
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/othershe/test/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
Expand All @@ -27,7 +28,12 @@ public void showDialog0(View view) {
.setConvertListener(new ViewConvertListener() {
@Override
public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {

holder.setOnClickListener(R.id.wechat, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
}
});
}
})
.setDimAmount(0.3f)
Expand Down
104 changes: 55 additions & 49 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog0"
android:text="分享" />
android:layout_height="wrap_content">

<Button
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog1"
android:text="好友设置" />
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog2"
android:text="评论" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog0"
android:text="分享" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog3"
android:text="红包" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog1"
android:text="好友设置" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog4"
android:text="加载提示" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog2"
android:text="评论" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog5"
android:text="确定-0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog3"
android:text="红包" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog6"
android:text="确定-1" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog4"
android:text="加载提示" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog5"
android:text="确定-0" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showDialog6"
android:text="确定-1" />

</LinearLayout>

</ScrollView>
5 changes: 3 additions & 2 deletions app/src/main/res/layout/share_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp">
android:paddingBottom="15dp"
android:paddingTop="15dp">

<LinearLayout
android:id="@+id/wechat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.othershe.nicedialog;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
Expand All @@ -13,31 +14,53 @@
import android.view.WindowManager;

public abstract class BaseNiceDialog extends DialogFragment {

private int margin;//
private int width;
private int height;
private float dimAmount = 0.5f;
private boolean showBottom;
private boolean outCancel = true;
private static final String MARGIN = "margin";
private static final String WIDTH = "width";
private static final String HEIGHT = "height";
private static final String DIM = "dim_amount";
private static final String BOTTOM = "show_bottom";
private static final String CANCEL = "out_cancel";
private static final String ANIM = "anim_style";
private static final String LAYOUT = "layout_id";

private int margin;//左右边距
private int width;//宽度
private int height;//高度
private float dimAmount = 0.5f;//灰度深浅
private boolean showBottom;//是否底部显示
private boolean outCancel = true;//是否点击外部取消
@StyleRes
private int animStyle;
@LayoutRes
protected int layoutId;

public abstract int intLayoutId();

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


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NiceDialog);
layoutId = intLayoutId();

//恢复保存的数据
if (savedInstanceState != null) {
margin = savedInstanceState.getInt(MARGIN);
width = savedInstanceState.getInt(WIDTH);
height = savedInstanceState.getInt(HEIGHT);
dimAmount = savedInstanceState.getFloat(DIM);
showBottom = savedInstanceState.getBoolean(BOTTOM);
outCancel = savedInstanceState.getBoolean(CANCEL);
animStyle = savedInstanceState.getInt(ANIM);
layoutId = savedInstanceState.getInt(LAYOUT);
}
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(intLayoutId(), container, false);
View view = inflater.inflate(layoutId, container, false);
convertView(ViewHolder.create(view), this);
return view;
}
Expand All @@ -48,6 +71,24 @@ public void onStart() {
initParams();
}

/**
* 屏幕旋转等导致DialogFragment销毁后重建时保存数据
*
* @param outState
*/
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(MARGIN, margin);
outState.putInt(WIDTH, width);
outState.putInt(HEIGHT, height);
outState.putFloat(DIM, dimAmount);
outState.putBoolean(BOTTOM, showBottom);
outState.putBoolean(CANCEL, outCancel);
outState.putInt(ANIM, animStyle);
outState.putInt(LAYOUT, layoutId);
}

private void initParams() {
Window window = getDialog().getWindow();
if (window != null) {
Expand Down Expand Up @@ -78,7 +119,6 @@ private void initParams() {
setCancelable(outCancel);
}


public BaseNiceDialog setMargin(int margin) {
this.margin = margin;
return this;
Expand Down
25 changes: 21 additions & 4 deletions nicedialog/src/main/java/com/othershe/nicedialog/NiceDialog.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.othershe.nicedialog;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;

public class NiceDialog extends BaseNiceDialog {

@LayoutRes
private int layoutId;
private ViewConvertListener convertListener;

public static NiceDialog init() {
Expand All @@ -17,7 +16,6 @@ public int intLayoutId() {
return layoutId;
}


@Override
public void convertView(ViewHolder holder, BaseNiceDialog dialog) {
if (convertListener != null) {
Expand All @@ -35,4 +33,23 @@ public NiceDialog setConvertListener(ViewConvertListener convertListener) {
this.convertListener = convertListener;
return this;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
convertListener = (ViewConvertListener) savedInstanceState.getSerializable("listener");
}
}

/**
* 保存接口
*
* @param outState
*/
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("listener", convertListener);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.othershe.nicedialog;

public interface ViewConvertListener {
import java.io.Serializable;

public interface ViewConvertListener extends Serializable {
long serialVersionUID = System.currentTimeMillis();

void convertView(ViewHolder holder, BaseNiceDialog dialog);
}

0 comments on commit d45d02d

Please sign in to comment.