Browse Source

增加老师端学生端首页悬浮按钮

Pq 2 years ago
parent
commit
9508658eea

+ 11 - 0
BaseLibrary/src/main/java/com/cooleshow/base/utils/helper/WebStartHelper.java

@@ -213,4 +213,15 @@ public class WebStartHelper {
                 .withString(WebConstants.WEB_URL, WebConstants.MUSIC_SHEET_DETAIL + musicId)
                 .navigation();
     }
+
+    /**
+     * 打开目标主页
+     * @param
+     */
+    public static void startWeb(String url){
+        ARouter.getInstance()
+                .build(RouterPath.WebCenter.ACTIVITY_HTML)
+                .withString(WebConstants.WEB_URL,url)
+                .navigation();
+    }
 }

+ 139 - 0
BaseLibrary/src/main/java/com/cooleshow/base/widgets/CustomDragView.java

@@ -0,0 +1,139 @@
+package com.cooleshow.base.widgets;
+
+import android.animation.ValueAnimator;
+import android.content.Context;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.MotionEvent;
+import android.view.View;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
+
+import com.cooleshow.base.utils.SizeUtils;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.widget.AppCompatImageView;
+
+/**
+ * Author by pq, Date on 2022/10/20.
+ */
+public class CustomDragView extends AppCompatImageView {
+
+
+    private int hideSize = 0;
+    private int lastX, lastY;
+    private int mStartX, mStartY = 0;
+    private boolean isMove = false;
+
+    public CustomDragView(Context context) {
+        super(context);
+    }
+
+    public CustomDragView(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public CustomDragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        Log.i("pq", "event action:" + event.getAction());
+        int x = (int) event.getRawX();
+        int y = (int) event.getRawY();
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_DOWN:
+                lastX = (int) event.getRawX();
+                lastY = (int) event.getRawY();
+                mStartX = x;
+                mStartY = y;
+                isMove = false;
+                break;
+            case MotionEvent.ACTION_MOVE:
+                int moveX = (int) (event.getRawX() - lastX);
+                int moveY = (int) (event.getRawY() - lastY);
+
+                int top = getTop();
+                int left = getLeft();
+                int right = getRight();
+                int bottom = getBottom();
+                //高度范围
+                if (top < 0) {
+                    top = 0;
+                    bottom = getHeight();
+                }
+
+                if (bottom >= screenHeight()) {
+                    top = screenHeight() - getHeight();
+                    bottom = screenHeight();
+                }
+
+                //宽度范围
+                if (left < -hideSize) {
+                    left = -hideSize;
+                    right = getWidth() - hideSize;
+                }
+                if (right > screenWidth() + hideSize) {
+                    left = screenWidth() - getWidth() + hideSize;
+                    right = screenWidth() + hideSize;
+                }
+                layout(left + moveX, top + moveY, right + moveX, bottom + moveY);
+
+                lastX = (int) event.getRawX();
+                lastY = (int) event.getRawY();
+
+                float deltaX1 = x - mStartX;
+                float deltaY1 = y - mStartY;
+                Log.i("pq", "deltaX:" + deltaX1 + "--x:" + x + "--mStartX:" + mStartX);
+                Log.i("pq", "deltaY:" + deltaY1 + "--y:" + y + "--mStartY:" + mStartY);
+                if (Math.abs(deltaX1) <= 5 && Math.abs(deltaY1) <= 5) {
+                    isMove = false;
+                } else {
+                    isMove = true;
+                }
+                break;
+            case MotionEvent.ACTION_UP:
+                if(isMove){
+                    int maxDuration = 500;
+                    int duration = 0;
+                    int leftLimit = (screenWidth() - getWidth()) / 2;
+                    if (getLeft() < leftLimit) {
+    //                    layout(0,getTop(),getWidth(),getBottom());
+                        duration = maxDuration * (getLeft() + hideSize) / (leftLimit + hideSize);
+                        animSlide(this, getLeft(), -hideSize, duration);
+                    } else {
+                        duration = maxDuration * (screenWidth() - getRight() + hideSize) / (leftLimit + hideSize);
+                        animSlide(this, getLeft(), screenWidth() - getWidth() + hideSize, 500);
+    //                    layout(screenWidth()-getWidth(),getTop(),screenWidth(),getBottom());
+                    }
+                }
+                break;
+        }
+        return isMove ? isMove : super.onTouchEvent(event);
+    }
+
+
+    public void animSlide(View view, int from, int to, int duration) {
+        ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to);
+        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+            @Override
+            public void onAnimationUpdate(ValueAnimator animation) {
+                int viewLeft = (int) animation.getAnimatedValue();
+                layout(viewLeft, getTop(), viewLeft + view.getWidth(), getBottom());
+            }
+        });
+        //为防止溢出边界时,duration时间为负值,做下0判断
+        valueAnimator.setDuration(duration < 0 ? 0 : duration);
+        valueAnimator.start();
+    }
+
+    public int screenWidth() {
+        return getContext().getResources().getDisplayMetrics().widthPixels;
+    }
+
+    public int screenHeight() {
+        return getContext().getResources().getDisplayMetrics().heightPixels - SizeUtils.dp2px(50);
+    }
+}

+ 1 - 0
student/src/main/java/com/cooleshow/student/bean/AppHomeBean.java

@@ -14,6 +14,7 @@ public class AppHomeBean {
     public List<ItemBean> banner;
     public List<ItemBean> flashPage;//dialog
     public List<ItemBean> information;//资讯
+    public List<ItemBean> suspension;//悬浮按钮
 
     public static class ItemBean implements Serializable {
         /*

+ 21 - 0
student/src/main/java/com/cooleshow/student/ui/main/MainActivity.java

@@ -26,6 +26,7 @@ import com.cooleshow.base.event.LoginStatusEvent;
 import com.cooleshow.base.router.RouterPath;
 import com.cooleshow.base.ui.activity.BaseMVPActivity;
 import com.cooleshow.base.utils.FileUtils;
+import com.cooleshow.base.utils.GlideUtils;
 import com.cooleshow.base.utils.GsonUtils;
 import com.cooleshow.base.utils.JumpUtils;
 import com.cooleshow.base.utils.LogUtils;
@@ -33,6 +34,7 @@ import com.cooleshow.base.utils.ToastUtil;
 import com.cooleshow.base.utils.helper.MidiFileHelper;
 import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
 import com.cooleshow.base.utils.helper.UpdateAppHelper;
+import com.cooleshow.base.utils.helper.WebStartHelper;
 import com.cooleshow.base.widgets.TabAnimationView;
 import com.cooleshow.student.App;
 import com.cooleshow.student.R;
@@ -89,6 +91,7 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
     private static final int EXIT_APP_DELAY = 1000;
     private long lastTime = 0;
     private boolean isNeedSetPushId = true;
+    private String floatViewEventUrl = "";
 
     /**
      * 打开首页并选中指定tab
@@ -178,6 +181,14 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
                 return onTabClick(item.getItemId());
             }
         });
+        viewBinding.viewSuspension.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (!TextUtils.isEmpty(floatViewEventUrl)) {
+                    WebStartHelper.startWeb(floatViewEventUrl);
+                }
+            }
+        });
     }
 
     @Override
@@ -408,6 +419,16 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
         }
     }
 
+    public void showFloatView(String eventUrl, String imgUrl) {
+        this.floatViewEventUrl = eventUrl;
+        viewBinding.viewSuspension.setVisibility(View.VISIBLE);
+        GlideUtils.INSTANCE.loadImage(this, imgUrl, viewBinding.viewSuspension);
+    }
+
+    public void hideFloatView() {
+        viewBinding.viewSuspension.setVisibility(View.GONE);
+    }
+
     @Override
     public void onCountChanged(int count) {
         if (count > 0) {

+ 9 - 0
student/src/main/java/com/cooleshow/student/ui/main/NewHomeFragment.java

@@ -441,6 +441,15 @@ public class NewHomeFragment extends BaseMVPFragment<FragmentNewHomeLayoutBindin
         //后台配置menu
         initMenu(appHomeBean.appMenu);
         showFlashDialog(appHomeBean.flashPage);
+        if (getActivity() instanceof MainActivity) {
+            MainActivity mainActivity = (MainActivity) getActivity();
+            if (appHomeBean.suspension != null && appHomeBean.suspension.size() > 0) {
+                AppHomeBean.ItemBean itemBean = appHomeBean.suspension.get(0);
+                mainActivity.showFloatView(itemBean.linkUrl, itemBean.coverImage);
+            } else {
+                mainActivity.hideFloatView();
+            }
+        }
     }
 
     private void showFlashDialog(List<AppHomeBean.ItemBean> flashPage) {

+ 11 - 1
student/src/main/res/layout/activity_main.xml

@@ -31,13 +31,13 @@
 
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/cs_bottom"
-        android:visibility="visible"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@color/white"
         android:elevation="5dp"
         android:orientation="vertical"
         android:paddingBottom="10dp"
+        android:visibility="visible"
         app:layout_constraintBottom_toBottomOf="parent">
 
         <com.cooleshow.base.widgets.TabAnimationView
@@ -239,4 +239,14 @@
             app:layout_constraintTop_toTopOf="parent" />
 
     </androidx.constraintlayout.widget.ConstraintLayout>
+
+    <com.cooleshow.base.widgets.CustomDragView
+        android:id="@+id/view_suspension"
+        android:visibility="gone"
+        android:layout_width="100dp"
+        android:layout_height="100dp"
+        android:layout_marginEnd="12dp"
+        android:layout_marginBottom="12dp"
+        app:layout_constraintBottom_toTopOf="@+id/cs_bottom"
+        app:layout_constraintRight_toRightOf="parent"/>
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 0
teacher/src/main/java/com/cooleshow/teacher/bean/HomeMenuBean.java

@@ -11,6 +11,7 @@ import java.util.List;
 public class HomeMenuBean {
     public List<ItemBean> appMenu;
     public List<BannerBean> banner;
+    public List<ItemBean> suspension;//悬浮按钮
 
     public static class BannerBean implements Parcelable{
         /**

+ 22 - 8
teacher/src/main/java/com/cooleshow/teacher/ui/main/MainActivity.java

@@ -21,6 +21,7 @@ import com.cooleshow.base.event.LoginStatusEvent;
 import com.cooleshow.base.router.RouterPath;
 import com.cooleshow.base.ui.activity.BaseMVPActivity;
 import com.cooleshow.base.utils.FileUtils;
+import com.cooleshow.base.utils.GlideUtils;
 import com.cooleshow.base.utils.GsonUtils;
 import com.cooleshow.base.utils.JumpUtils;
 import com.cooleshow.base.utils.LogUtils;
@@ -28,6 +29,7 @@ import com.cooleshow.base.utils.ToastUtil;
 import com.cooleshow.base.utils.helper.MidiFileHelper;
 import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
 import com.cooleshow.base.utils.helper.UpdateAppHelper;
+import com.cooleshow.base.utils.helper.WebStartHelper;
 import com.cooleshow.base.widgets.TabAnimationView;
 import com.cooleshow.teacher.App;
 import com.cooleshow.teacher.R;
@@ -85,6 +87,7 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
     private static final int EXIT_APP_DELAY = 1000;
     private long lastTime = 0;
     private boolean isNeedSetPushId = true;
+    private String floatViewEventUrl = "";
 
     /**
      * 打开首页并选中指定tab
@@ -171,14 +174,14 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
         getViewBinding().viewPager.setOffscreenPageLimit(mFragments.size());
         getViewBinding().viewPager.setUserInputEnabled(false);
         getViewBinding().viewPager.setSaveEnabled(false);
-//        getViewBinding().navigation.setItemIconTintList(null);
-//        getViewBinding().navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
-//            @Override
-//            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
-//
-//                return onTabClick(item.getItemId());
-//            }
-//        });
+        viewBinding.viewSuspension.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (!TextUtils.isEmpty(floatViewEventUrl)) {
+                    WebStartHelper.startWeb(floatViewEventUrl);
+                }
+            }
+        });
     }
 
     public void setPositionItem(int position) {
@@ -477,6 +480,17 @@ public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPrese
         return super.onKeyDown(keyCode, event);
     }
 
+    public void showFloatView(String eventUrl, String imgUrl) {
+        this.floatViewEventUrl = eventUrl;
+        viewBinding.viewSuspension.setVisibility(View.VISIBLE);
+        GlideUtils.INSTANCE.loadImage(this, imgUrl, viewBinding.viewSuspension);
+    }
+
+    public void hideFloatView() {
+        viewBinding.viewSuspension.setVisibility(View.GONE);
+    }
+
+
     @Override
     public void onDestroy() {
         super.onDestroy();

+ 10 - 0
teacher/src/main/java/com/cooleshow/teacher/ui/main/NewHomeFragment.java

@@ -409,6 +409,16 @@ public class NewHomeFragment extends BaseMVPFragment<FragmentNewHomeLayoutBindin
             menuAdapter.notifyDataSetChanged();
             mViewBinding.flTopMenu.setVisibility(View.GONE);
         }
+
+        if (getActivity() instanceof MainActivity) {
+            MainActivity mainActivity = (MainActivity) getActivity();
+            if (homeMenuBean.suspension != null && homeMenuBean.suspension.size() > 0) {
+                HomeMenuBean.ItemBean itemBean = homeMenuBean.suspension.get(0);
+                mainActivity.showFloatView(itemBean.linkUrl, itemBean.coverImage);
+            } else {
+                mainActivity.hideFloatView();
+            }
+        }
     }
 
     @Override

+ 10 - 0
teacher/src/main/res/layout/activity_main.xml

@@ -240,4 +240,14 @@
             app:layout_constraintTop_toTopOf="parent" />
 
     </androidx.constraintlayout.widget.ConstraintLayout>
+
+    <com.cooleshow.base.widgets.CustomDragView
+        android:id="@+id/view_suspension"
+        android:visibility="gone"
+        android:layout_width="100dp"
+        android:layout_height="100dp"
+        android:layout_marginEnd="12dp"
+        android:layout_marginBottom="12dp"
+        app:layout_constraintBottom_toTopOf="@+id/cs_bottom"
+        app:layout_constraintRight_toRightOf="parent"/>
 </androidx.constraintlayout.widget.ConstraintLayout>