| | |
| | | package com.application.zhangshi_app_android.ui.function; |
| | | |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.view.GestureDetector; |
| | | import android.view.MotionEvent; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.FrameLayout; |
| | | import android.widget.ImageView; |
| | | import android.widget.PopupWindow; |
| | | |
| | | import androidx.fragment.app.Fragment; |
| | |
| | | import androidx.lifecycle.Observer; |
| | | |
| | | import com.android.app_base.manager.AppManager; |
| | | import com.android.app_base.utils.GlideUtil; |
| | | import com.android.app_base.utils.ScreenSizeUtils; |
| | | import com.application.zhangshi_app_android.BR; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.BannerBean; |
| | | import com.application.zhangshi_app_android.databinding.FragmentFunctionBinding; |
| | | import com.application.zhangshi_app_android.ui.DLBaseFragment; |
| | | import com.application.zhangshi_app_android.ui.TestActivity; |
| | | import com.application.zhangshi_app_android.ui.dialog.TotalAddDialog; |
| | | import com.application.zhangshi_app_android.ui.home.HomeFragment; |
| | | import com.application.zhangshi_app_android.ui.main.MainActivity; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | public class FunctionFragment extends DLBaseFragment<FragmentFunctionBinding,FunctionFragmentViewModel> { |
| | | |
| | | private PopupWindow mOperatePopupWindow; |
| | | private PopupWindow mAddPopupWindow; |
| | | private ImageCarouselFragment imageCarouselFragment; |
| | | private VideoFragment videoFragment; |
| | | private VideoCarouselFragment videoCarouselFragment; |
| | | |
| | | private FunctionFragment() { |
| | | } |
| | |
| | | binding.layoutContacts.setOnClickListener(v->{ |
| | | AppManager.getAppManager().startActivity(ContactsActivity.class); |
| | | }); |
| | | binding.ivOperate.setOnClickListener(v->{ |
| | | AppManager.getAppManager().startActivity(TestActivity.class); |
| | | |
| | | binding.fabAdd.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | |
| | | } |
| | | }); |
| | | binding.fabAdd.setOnTouchListener(new View.OnTouchListener() { |
| | | |
| | | private boolean isDragging = false; |
| | | private int startX, startY; |
| | | private float dX, dY; |
| | | |
| | | @Override |
| | | public boolean onTouch(View v, MotionEvent event) { |
| | | boolean flag = false; |
| | | |
| | | switch (event.getAction()) { |
| | | case MotionEvent.ACTION_DOWN: |
| | | // 计算手指按下时相对于 FloatingActionButton 的偏移量 |
| | | dX = v.getX() - event.getRawX(); |
| | | dY = v.getY() - event.getRawY(); |
| | | startY = (int) event.getRawY(); |
| | | startX = (int) event.getRawX(); |
| | | isDragging = false; |
| | | break; |
| | | case MotionEvent.ACTION_MOVE: |
| | | // 更新 FloatingActionButton 的位置 |
| | | float newX = event.getRawX() + dX; |
| | | float newY = event.getRawY() + dY; |
| | | |
| | | // 限制移动范围 |
| | | float minX = 0; |
| | | float maxX = binding.container.getWidth() - v.getWidth(); |
| | | float minY = 0; |
| | | float maxY = binding.container.getHeight() - v.getHeight(); |
| | | |
| | | // 确保不超出范围 |
| | | if (newX < minX) { |
| | | newX = minX; |
| | | } else if (newX > maxX) { |
| | | newX = maxX; |
| | | } |
| | | |
| | | if (newY < minY) { |
| | | newY = minY; |
| | | } else if (newY > maxY) { |
| | | newY = maxY; |
| | | } |
| | | v.setX(newX); |
| | | v.setY(newY); |
| | | if (Math.abs(event.getRawX() - startX) > 10 || Math.abs(event.getRawY() - startY) > 10) { |
| | | isDragging = true; // 如果移动距离大于阈值,认为是拖动 |
| | | }else { |
| | | isDragging = false; |
| | | } |
| | | flag = true; |
| | | break; |
| | | case MotionEvent.ACTION_UP: |
| | | if (!isDragging) { |
| | | // 如果没有拖动,认为是点击事件 |
| | | new TotalAddDialog.Builder(getContext()).show(); |
| | | } |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | return flag; |
| | | } |
| | | }); |
| | | |
| | | imageCarouselFragment = ImageCarouselFragment.newInstance(); |
| | | videoFragment = VideoFragment.newInstance(); |
| | | replaceFragment(imageCarouselFragment); |
| | | binding.ivOperateSelect.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | AppManager.getAppManager().startActivity(new Intent(getSelfActivity(),ImageSelectActivity.class).putExtra("flag",0).putExtra("list", (Serializable) imageCarouselFragment.getImageList())); |
| | | } |
| | | }); |
| | | |
| | | //右上角操作弹窗 |
| | | mOperatePopupWindow = initPopUpWindow(View.inflate(getSelfActivity(),R.layout.pop_switch_video_or_image,null)); |
| | | mOperatePopupWindow.getContentView().findViewById(R.id.tv_image).setOnClickListener(v -> { |
| | | mOperatePopupWindow.dismiss(); |
| | | if (imageCarouselFragment == null){ |
| | | imageCarouselFragment = ImageCarouselFragment.newInstance(); |
| | | } |
| | | imageCarouselFragment = ImageCarouselFragment.newInstance(); |
| | | replaceFragment(imageCarouselFragment); |
| | | |
| | | imageCarouselFragment.setImageList(viewModel.getImageListLiveData().getValue()); |
| | | binding.ivOperateSelect.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | AppManager.getAppManager().startActivity(new Intent(getSelfActivity(),ImageSelectActivity.class).putExtra("flag",0).putExtra("list", (Serializable) imageCarouselFragment.getImageList())); |
| | | } |
| | | }); |
| | | }); |
| | | mOperatePopupWindow.getContentView().findViewById(R.id.tv_video).setOnClickListener(v -> { |
| | | mOperatePopupWindow.dismiss(); |
| | | if (videoFragment == null){ |
| | | videoFragment = VideoFragment.newInstance(); |
| | | } |
| | | replaceFragment(videoFragment); |
| | | videoCarouselFragment = VideoCarouselFragment.newInstance(); |
| | | replaceFragment(videoCarouselFragment); |
| | | videoCarouselFragment.setVideoList(viewModel.getVideoListLiveData().getValue()); |
| | | binding.ivOperateSelect.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | AppManager.getAppManager().startActivity(new Intent(getSelfActivity(),ImageSelectActivity.class).putExtra("flag",1).putExtra("list", (Serializable) videoCarouselFragment.getVideoList())); |
| | | } |
| | | |
| | | }); |
| | | }); |
| | | //点击弹出操作弹窗 |
| | | binding.ivOperate.setOnClickListener(v -> { |
| | | mOperatePopupWindow.showAsDropDown(binding.ivOperate,-ScreenSizeUtils.dip2px(getSelfActivity(),85) +binding.ivOperate.getWidth(),0); |
| | | }); |
| | | |
| | | imageCarouselFragment = ImageCarouselFragment.newInstance(); |
| | | replaceFragment(imageCarouselFragment); |
| | | } |
| | | public void replaceFragment(Fragment fragment){ |
| | | FragmentTransaction transaction = getSelfActivity().getSupportFragmentManager().beginTransaction(); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void onResume() { |
| | | super.onResume(); |
| | | initData(); |
| | | } |
| | | |
| | | @Override |
| | | protected void initLiveDataObserve() { |
| | | viewModel.getBannerListLiveData().observe(this, new Observer<List<BannerBean>>() { |
| | | viewModel.getImageListLiveData().observe(this, new Observer<List<BannerBean>>() { |
| | | @Override |
| | | public void onChanged(List<BannerBean> bannerBeans) { |
| | | List<String> imageList = new ArrayList<>(); |
| | | List<String> videoList = new ArrayList<>(); |
| | | for (BannerBean bannerBean : bannerBeans) { |
| | | if (bannerBean.getFlag() == 0){ |
| | | //图片 |
| | | imageList.add(bannerBean.getUrl()); |
| | | }else if (bannerBean.getFlag() == 1){ |
| | | //视频 |
| | | videoList.add(bannerBean.getUrl()); |
| | | } |
| | | if (imageCarouselFragment != null){ |
| | | imageCarouselFragment.setImageList(bannerBeans); |
| | | } |
| | | imageCarouselFragment.setImageList(imageList); |
| | | videoFragment.setVideoList(videoList); |
| | | } |
| | | }); |
| | | viewModel.getVideoListLiveData().observe(this, new Observer<List<BannerBean>>() { |
| | | @Override |
| | | public void onChanged(List<BannerBean> bannerBeans) { |
| | | if (videoCarouselFragment != null){ |
| | | videoCarouselFragment.setVideoList(bannerBeans); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | @Override |
| | |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | protected void dataNull() { |
| | | super.dataNull(); |
| | | binding.layoutFrame.setVisibility(View.GONE); |
| | | binding.layoutDataNull.setVisibility(View.VISIBLE); |
| | | } |
| | | |
| | | @Override |
| | | protected void hide() { |
| | | super.hide(); |
| | | binding.layoutFrame.setVisibility(View.VISIBLE); |
| | | binding.layoutDataNull.setVisibility(View.GONE); |
| | | } |
| | | } |
| | | |