| | |
| | | package com.application.zhangshi_app_android.ui.function; |
| | | import android.graphics.drawable.ColorDrawable; |
| | | import android.view.MotionEvent; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.PopupWindow; |
| | |
| | | import androidx.databinding.library.baseAdapters.BR; |
| | | import androidx.lifecycle.Observer; |
| | | |
| | | import com.android.app_base.base.viewmodel.BaseViewModel; |
| | | import com.android.app_base.utils.ScreenSizeUtils; |
| | | import com.android.app_base.utils.Utils; |
| | | import com.android.app_base.widget.LinearItemDecoration; |
| | | import com.application.zhangshi_app_android.R; |
| | | |
| | | import com.application.zhangshi_app_android.adapter.FamilyAssetsRvAdapter; |
| | | import com.application.zhangshi_app_android.adapter.FamilyContactsRvAdapter; |
| | | import com.application.zhangshi_app_android.bean.FamilyContactsBean; |
| | | import com.application.zhangshi_app_android.adapter.ContactsRvAdapter; |
| | | import com.application.zhangshi_app_android.bean.ContactsBean; |
| | | import com.application.zhangshi_app_android.bean.ContactsDetailBean; |
| | | import com.application.zhangshi_app_android.databinding.ActivityContactsBinding; |
| | | ; |
| | | import com.application.zhangshi_app_android.ui.DLBaseActivity; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * @author Gss |
| | | * @date 2023.04.18. 20:33 |
| | | * @desc |
| | | * @desc 通讯录 |
| | | */ |
| | | public class ContactsActivity<FamilyContactsActivityViewModel extends BaseViewModel> extends DLBaseActivity<ActivityContactsBinding,FamilyContactsActivityViewModel> { |
| | | private FamilyContactsRvAdapter adapter; |
| | | public class ContactsActivity extends DLBaseActivity<ActivityContactsBinding,ContactsActivityViewModel> { |
| | | private ContactsRvAdapter adapter; |
| | | private PopupWindow mOperatePopupWindow; |
| | | private boolean isSearchLayoutVisible; |
| | | |
| | | @Override |
| | | public int getLayoutId() { |
| | | return R.layout.activity_contacts; |
| | |
| | | |
| | | @Override |
| | | public void initView() { |
| | | adapter = new FamilyContactsRvAdapter(this); |
| | | //下拉刷新布局 |
| | | binding.refreshLayout.setEnableRefresh(false); |
| | | binding.refreshLayout.setEnableLoadMore(true); |
| | | binding.refreshLayout.setEnableOverScrollDrag(true); |
| | | binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> { |
| | | viewModel.loadMoreContacts(); |
| | | }); |
| | | //recyclerView适配器 |
| | | adapter = new ContactsRvAdapter(this); |
| | | LinearItemDecoration itemDecoration = new LinearItemDecoration(); |
| | | itemDecoration.setBottomSpace((int) getResources().getDimension(com.android.app_base.R.dimen.dp_12)); |
| | | itemDecoration.setHorizontalSpace((int) getResources().getDimension(com.android.app_base.R.dimen.dp_20)); |
| | | itemDecoration.setFirstTop((int) getResources().getDimension(com.android.app_base.R.dimen.dp_20)); |
| | | binding.recyclerView.addItemDecoration(itemDecoration); |
| | | binding.recyclerView.setAdapter(adapter); |
| | | |
| | | mOperatePopupWindow = initPopUpWindow(View.inflate(this,R.layout.pop_operate,null)); |
| | | mOperatePopupWindow.getContentView().findViewById(R.id.layout_add).setVisibility(View.GONE); |
| | | mOperatePopupWindow.getContentView().findViewById(R.id.layout_select).setVisibility(View.GONE); |
| | | |
| | | //右上角操作弹窗 |
| | | mOperatePopupWindow = initPopUpWindow(View.inflate(this,R.layout.pop_contacts_import,null)); |
| | | mOperatePopupWindow.getContentView().findViewById(R.id.layout_contacts_import).setOnClickListener(v -> { |
| | | mOperatePopupWindow.dismiss(); |
| | | //TODO 导入通讯录 |
| | | }); |
| | | //点击弹出操作弹窗 |
| | | binding.ivOperate.setOnClickListener(v -> { |
| | | mOperatePopupWindow.showAsDropDown(binding.ivOperate,-ScreenSizeUtils.dip2px(this,85) +binding.ivOperate.getWidth(),0); |
| | | }); |
| | | //点击放大镜按钮显示出搜索框 |
| | | binding.ivSearchDefault.setOnClickListener(v -> { |
| | | binding.layoutSearch.setVisibility(View.VISIBLE); |
| | | binding.layoutDefault.setVisibility(View.GONE); |
| | | }); |
| | | //点击搜索框菜单下拉出 搜索参数 |
| | | binding.ivSearchType.setOnClickListener(v -> { |
| | | if (!isSearchLayoutVisible){ |
| | | binding.layoutSearchParameter.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| | | int targetHeight = binding.layoutSearchParameter.getMeasuredHeight(); |
| | | Utils.dropExpand(binding.containerSearchParameter,0, targetHeight); |
| | | isSearchLayoutVisible = true; |
| | | } |
| | | }); |
| | | //搜索参数layout添加空点击事件,防止点击它内部view时它后面的recycler还能响应点击事件 |
| | | binding.layoutSearchParameter.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | } |
| | | }); |
| | | //点击搜索按钮进行搜索 |
| | | binding.ivSearch.setOnClickListener(v -> { |
| | | viewModel.getContacts(); |
| | | binding.layoutSearch.setVisibility(View.GONE); |
| | | binding.layoutDefault.setVisibility(View.VISIBLE); |
| | | binding.tvTitle.setText("搜索结果"); |
| | | hideSoftKeyboard(); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public boolean dispatchTouchEvent(MotionEvent event) { |
| | | float x = event.getX(); |
| | | float y = event.getY(); |
| | | if (isSearchLayoutVisible){ |
| | | if (!Utils.isPointInsideView(x, y, binding.layoutSearchParameter)) { |
| | | Utils.pullCollapse(binding.containerSearchParameter, binding.layoutSearchParameter.getMeasuredHeight(),0); |
| | | isSearchLayoutVisible = false; |
| | | return true; |
| | | } |
| | | } |
| | | return super.dispatchTouchEvent(event); |
| | | } |
| | | |
| | | @Override |
| | | public void initData() { |
| | | viewModel.getFamilyContacts(); |
| | | viewModel.getContacts(); |
| | | } |
| | | |
| | | @Override |
| | | public void initLiveDataObserve() { |
| | | viewModel.getDataListLiveData().observe(this, familyContactsBeans -> { |
| | | adapter.setData(familyContactsBeans); |
| | | viewModel.getDataListLiveData().observe(this, contactsDetailBeans -> { |
| | | //根据名字分组 |
| | | Set<String> maNames = new HashSet<>(); |
| | | for (ContactsDetailBean bean : contactsDetailBeans) { |
| | | maNames.add(bean.getMyName()); |
| | | } |
| | | List<ContactsBean> list = new ArrayList<>(); |
| | | for (String name : maNames) { |
| | | ContactsBean bean = new ContactsBean(); |
| | | bean.setMyName(name); |
| | | List<ContactsDetailBean> detailBeans = new ArrayList<>(); |
| | | for (ContactsDetailBean detailBean : contactsDetailBeans) { |
| | | if (name.equals(detailBean.getMyName())) { |
| | | detailBeans.add(detailBean); |
| | | } |
| | | } |
| | | bean.setContactsDetailBeans(detailBeans); |
| | | list.add(bean); |
| | | } |
| | | adapter.setData(list); |
| | | }); |
| | | viewModel.getMoreListLiveData().observe(this, list -> { |
| | | //已经有名字的,直接添加到对应的名字下面 |
| | | //没有名字的,新建一个名字,添加到对应的名字下面 |
| | | List<ContactsBean> data = adapter.getData(); |
| | | List<ContactsDetailBean> newData = new ArrayList<>(); |
| | | Set<String> maNames = new HashSet<>(); |
| | | for (ContactsDetailBean contactsDetailBean : list) { |
| | | boolean isAdd = false; |
| | | for (ContactsBean bean : data) { |
| | | if (contactsDetailBean.getMyName().equals(bean.getMyName())) { |
| | | bean.getContactsDetailBeans().add(contactsDetailBean); |
| | | isAdd = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!isAdd) { |
| | | maNames.add(contactsDetailBean.getMyName()); |
| | | newData.add(contactsDetailBean); |
| | | } |
| | | } |
| | | for (String name : maNames) { |
| | | ContactsBean bean = new ContactsBean(); |
| | | bean.setMyName(name); |
| | | List<ContactsDetailBean> detailBeans = new ArrayList<>(); |
| | | for (ContactsDetailBean detailBean : list) { |
| | | if (name.equals(detailBean.getMyName())) { |
| | | detailBeans.add(detailBean); |
| | | } |
| | | } |
| | | bean.setContactsDetailBeans(detailBeans); |
| | | adapter.addItem(bean); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private PopupWindow initPopUpWindow(View view){ |
| | | PopupWindow popupWindow = new PopupWindow(this); |
| | | // 设置布局文件 |
| | | popupWindow.setContentView(view); |
| | | // 为了避免部分机型不显示,需要重新设置一下宽高 |
| | | popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| | | popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
| | | // 设置pop透明效果 |
| | | popupWindow.setBackgroundDrawable(new ColorDrawable(0x0000)); |
| | | // 设置pop出入动画 |
| | | popupWindow.setAnimationStyle(com.android.app_base.R.style.pop_add); |
| | | // 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true |
| | | popupWindow.setFocusable(true); |
| | | // 设置pop可点击,为false点击事件无效,默认为true |
| | | popupWindow.setTouchable(true); |
| | | // 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失 |
| | | popupWindow.setOutsideTouchable(false); |
| | | return popupWindow; |
| | | @Override |
| | | protected void dataNull() { |
| | | super.dataNull(); |
| | | binding.layoutDataNull.setVisibility(View.VISIBLE); |
| | | binding.refreshLayout.setVisibility(View.GONE); |
| | | } |
| | | |
| | | @Override |
| | | protected void dataFinish() { |
| | | super.dataFinish(); |
| | | binding.refreshLayout.finishLoadMore(); |
| | | } |
| | | |
| | | @Override |
| | | protected void hide() { |
| | | super.hide(); |
| | | binding.layoutDataNull.setVisibility(View.GONE); |
| | | binding.refreshLayout.setVisibility(View.VISIBLE); |
| | | binding.refreshLayout.finishLoadMore(); |
| | | } |
| | | } |