瀏覽代碼

0.0.46.beta15

kongzue 2 年之前
父節點
當前提交
403c34f4c2

+ 11 - 1
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -1034,11 +1034,21 @@ public class PopMenu extends BaseDialog {
     }
     
     public OnBackPressedListener<PopMenu> getOnBackPressedListener() {
-        return (OnBackPressedListener<PopMenu>) onBackPressedListener;
+        return onBackPressedListener;
     }
     
     public PopMenu setOnBackPressedListener(OnBackPressedListener<PopMenu> onBackPressedListener) {
         this.onBackPressedListener = onBackPressedListener;
         return this;
     }
+    
+    public View getBaseView() {
+        return baseView;
+    }
+    
+    public PopMenu setBaseView(View baseView) {
+        this.baseView = baseView;
+        refreshUI();
+        return this;
+    }
 }

+ 27 - 8
DialogX/src/main/java/com/kongzue/dialogx/interfaces/OnBindView.java

@@ -14,6 +14,9 @@ import static com.kongzue.dialogx.DialogX.ERROR_INIT_TIPS;
 
 import androidx.appcompat.app.AppCompatActivity;
 
+import java.lang.reflect.Field;
+import java.util.Random;
+
 /**
  * @author: Kongzue
  * @github: https://github.com/kongzue/
@@ -46,7 +49,7 @@ public abstract class OnBindView<D> {
                 @Override
                 public void run() {
                     super.run();
-                    synchronized (OnBindView.this){
+                    synchronized (OnBindView.this) {
                         customView = LayoutInflater.from(BaseDialog.getTopActivity()).inflate(layoutResId, new RelativeLayout(BaseDialog.getTopActivity()), false);
                         if (waitBindRunnable != null) {
                             waitBindRunnable.run();
@@ -66,11 +69,28 @@ public abstract class OnBindView<D> {
     
     private androidx.fragment.app.Fragment fragment;
     private android.app.Fragment supportFragment;
+    private int fragmentParentId = View.NO_ID;
+    
+    private int getFragmentParentId() {
+        if (fragmentParentId == View.NO_ID) {
+            fragmentParentId = createFragmentParentId();
+        }
+        return fragmentParentId;
+    }
+    
+    private int createFragmentParentId() {
+        fragmentParentId = new Random().nextInt();
+        View somebodyView = BaseDialog.getTopActivity().findViewById(fragmentParentId);
+        if (somebodyView != null) {
+            return createFragmentParentId();
+        }
+        return fragmentParentId;
+    }
     
     public OnBindView(androidx.fragment.app.Fragment fragment) {
         if (BaseDialog.getTopActivity() == null) return;
         this.customView = new FrameLayout(BaseDialog.getTopActivity());
-        this.customView.setId(R.id.id_frame_layout_custom);
+        this.customView.setId(getFragmentParentId());
         this.fragment = fragment;
         this.supportFragment = null;
     }
@@ -78,7 +98,7 @@ public abstract class OnBindView<D> {
     public OnBindView(android.app.Fragment supportFragment) {
         if (BaseDialog.getTopActivity() == null) return;
         this.customView = new FrameLayout(BaseDialog.getTopActivity());
-        this.customView.setId(R.id.id_frame_layout_custom);
+        this.customView.setId(getFragmentParentId());
         this.supportFragment = supportFragment;
         this.fragment = null;
     }
@@ -103,7 +123,6 @@ public abstract class OnBindView<D> {
     public View getCustomView() {
         if (customView == null) {
             customView = LayoutInflater.from(BaseDialog.getTopActivity()).inflate(layoutResId, new RelativeLayout(BaseDialog.getTopActivity()), false);
-            customView.setId(R.id.id_frame_layout_custom);
         }
         return customView;
     }
@@ -154,8 +173,8 @@ public abstract class OnBindView<D> {
         }
         parentView.addView(getCustomView(), lp);
         onBind((D) dialog, getCustomView());
-        if (fragment != null || supportFragment != null){
-            if (dialog.getDialogImplMode()!= DialogX.IMPL_MODE.VIEW){
+        if (fragment != null || supportFragment != null) {
+            if (dialog.getDialogImplMode() != DialogX.IMPL_MODE.VIEW) {
                 BaseDialog.error(dialog.dialogKey() + "非 VIEW 实现模式不支持 fragment 作为子布局显示。\n" +
                         "其原因为 Window 中不存在 FragmentManager,无法对子布局中的 fragment 进行管理。");
                 return;
@@ -166,14 +185,14 @@ public abstract class OnBindView<D> {
                     if (fragment != null && getCustomView() instanceof FrameLayout && BaseDialog.getTopActivity() instanceof AppCompatActivity) {
                         AppCompatActivity appCompatActivity = (AppCompatActivity) BaseDialog.getTopActivity();
                         androidx.fragment.app.FragmentTransaction transaction = appCompatActivity.getSupportFragmentManager().beginTransaction();
-                        transaction.add(R.id.id_frame_layout_custom, fragment);
+                        transaction.add(getFragmentParentId(), fragment);
                         transaction.commit();
                         onFragmentBind((D) dialog, getCustomView(), fragment, appCompatActivity.getSupportFragmentManager());
                     }
                     if (supportFragment != null && getCustomView() instanceof FrameLayout && BaseDialog.getTopActivity() instanceof Activity) {
                         Activity activity = (Activity) BaseDialog.getTopActivity();
                         android.app.FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
-                        transaction.add(R.id.id_frame_layout_custom, supportFragment);
+                        transaction.add(getFragmentParentId(), supportFragment);
                         transaction.commit();
                         onFragmentBind((D) dialog, getCustomView(), supportFragment, activity.getFragmentManager());
                     }

+ 0 - 2
DialogX/src/main/res/values/attrs.xml

@@ -27,6 +27,4 @@
         <attr name="autoSafeArea" format="boolean"/>
         <attr name="interceptBack" format="boolean"/>
     </declare-styleable>
-
-    <item name="id_frame_layout_custom" type="id"/>
 </resources>

+ 7 - 1
app/src/main/java/com/kongzue/dialogxdemo/activity/MainActivity.java

@@ -423,7 +423,13 @@ public class MainActivity extends BaseActivity {
         btnFullScreenDialogFragment.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                CustomFragment customFragment = new CustomFragment();
+                CustomFragment customFragment = new CustomFragment()
+                        .setAddButtonClickListener(new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                btnFullScreenDialogFragment.callOnClick();
+                            }
+                        });
                 FullScreenDialog.show(new OnBindView<FullScreenDialog>(customFragment) {
                     @Override
                     public void onBind(FullScreenDialog dialog, View v) {

+ 21 - 0
app/src/main/java/com/kongzue/dialogxdemo/fragment/CustomFragment.java

@@ -4,9 +4,11 @@ import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.TextView;
 
 import androidx.fragment.app.Fragment;
 
+import com.google.android.material.button.MaterialButton;
 import com.kongzue.dialogxdemo.R;
 
 /**
@@ -18,14 +20,24 @@ import com.kongzue.dialogxdemo.R;
  */
 public class CustomFragment extends Fragment {
     
+    private static int index = 0;
+    private View.OnClickListener addButtonClickListener;
+    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
     
+    private TextView txtInfo;
+    private MaterialButton btnAddDialog;
+    
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_custom, null);
+        txtInfo = view.findViewById(R.id.txt_info);
+        btnAddDialog = view.findViewById(R.id.btn_addDialog);
+        txtInfo.setText("这是第:" + (index++) + " 个 Fragment");
+        btnAddDialog.setOnClickListener(addButtonClickListener);
         return view;
     }
     
@@ -33,4 +45,13 @@ public class CustomFragment extends Fragment {
     public void onPause() {
         super.onPause();
     }
+    
+    public View.OnClickListener getAddButtonClickListener() {
+        return addButtonClickListener;
+    }
+    
+    public CustomFragment setAddButtonClickListener(View.OnClickListener addButtonClickListener) {
+        this.addButtonClickListener = addButtonClickListener;
+        return this;
+    }
 }

+ 35 - 2
app/src/main/res/layout/fragment_custom.xml

@@ -1,12 +1,45 @@
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
-    <TextView
+    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
+        android:layout_centerInParent="true"
         android:gravity="center"
-        android:text="看到这个说明\nFragment 已经正常显示"/>
+        android:orientation="vertical">
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:text="看到这个说明\nFragment 已经正常显示" />
+
+        <TextView
+            android:id="@+id/txt_info"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:text="" />
+
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/btn_addDialog"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_margin="5dp"
+            android:layout_marginTop="15dp"
+            android:paddingLeft="15dp"
+            android:paddingTop="10dp"
+            android:paddingRight="15dp"
+            android:paddingBottom="10dp"
+            android:text="新建一个 FullScreenDialog"
+            android:textAllCaps="false"
+            android:textSize="13dp"
+            android:textStyle="bold"
+            app:cornerRadius="99dp" />
+
+    </LinearLayout>
 
 </RelativeLayout>

+ 1 - 1
gradle.properties

@@ -19,5 +19,5 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.46.beta14
+BUILD_VERSION=0.0.46.beta15
 BUILD_VERSION_INT=45