| | |
| | | package com.hdl.photovoltaic.ui.message; |
| | | |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.AsyncTask; |
| | | import android.os.Bundle; |
| | | import android.os.Handler; |
| | | import android.os.Looper; |
| | | import android.text.Editable; |
| | | import android.text.TextUtils; |
| | | import android.text.TextWatcher; |
| | | import android.view.View; |
| | | import android.view.inputmethod.InputMethodManager; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.base.CustomBaseActivity; |
| | | import com.hdl.photovoltaic.bean.PageNumberObject; |
| | | import com.hdl.photovoltaic.databinding.ActivitySearchMessgeBinding; |
| | | import com.hdl.photovoltaic.enums.ShowErrorMode; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlDeviceLogic; |
| | | import com.hdl.photovoltaic.other.HdlCommonLogic; |
| | | import com.hdl.photovoltaic.other.HdlFileLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.other.HdlMessageLogic; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.ui.adapter.SearchHistoryAdapter; |
| | | import com.hdl.photovoltaic.ui.adapter.SearchMessageAdapter; |
| | | import com.hdl.photovoltaic.ui.bean.DeviceBean; |
| | | import com.hdl.photovoltaic.ui.bean.MessageBean; |
| | | import com.hdl.photovoltaic.utils.KeyboardStateObserverUtils; |
| | | import com.hdl.photovoltaic.widget.refreshlayout.BGARefreshLayout; |
| | | import com.hdl.photovoltaic.widget.refreshlayout.HDLRefreshViewHolder; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | |
| | | */ |
| | | public class SearchMessageActivity extends CustomBaseActivity { |
| | | |
| | | private Handler handler; |
| | | private Runnable delayedRunnable; |
| | | |
| | | private ActivitySearchMessgeBinding viewBinding; |
| | | |
| | | SearchMessageAdapter searchMessageAdapter;//设备适配器 |
| | | SearchHistoryAdapter searchHistoryAdapter;//历史记录适配器 |
| | | |
| | | private List<DeviceBean> deviceList = new ArrayList<>(); |
| | | |
| | | private int currentHouseListPage = 0; // 当前电站列表页码 |
| | | private int currentHouseListTotal = 0; // 电站列表总页码 |
| | | private boolean isHouseLoadingMore = false; // 标记电站列表正在加载更多数据 |
| | | private final List<MessageBean> mList = new ArrayList<>(); |
| | | private int mCurrentPage = 0; // 当前页码 |
| | | private int mCurrentTotal = 0; // 总页码 |
| | | private boolean isLoadingMore = false; // 标记正在加载更多数据 |
| | | |
| | | private String currSearchText; |
| | | |
| | | List<String> searchHistoryTitleList = new ArrayList<>(); |
| | | |
| | | String mMessageStateType = ""; |
| | | |
| | | @Override |
| | | public Object getContentView() { |
| | |
| | | @Override |
| | | public void onBindView(Bundle savedInstanceState) { |
| | | setStatusBarTranslucent(); |
| | | getWindow().setNavigationBarColor(getColor(R.color.text_FF000000)); |
| | | mMessageStateType = getIntent().getStringExtra("MessageStateType"); |
| | | //历史记录文件夹创建 |
| | | HdlFileLogic.getInstance().createFileDir(HdlFileLogic.getInstance().getCurrentUserRootPath()); |
| | | //初始化数据 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 下拉刷新的逻辑 |
| | | */ |
| | | protected void pullToRefresh() { |
| | | viewBinding.listSrl.setDelegate(new BGARefreshLayout.BGARefreshLayoutDelegate() { |
| | | @Override |
| | | public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) { |
| | | handler = new Handler(Looper.getMainLooper()); |
| | | |
| | | delayedRunnable = new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | viewBinding.listSrl.endRefreshing(); |
| | | loadNextPageMessageList(false, 1, viewBinding.messageSearchEt.getText().toString(), true); |
| | | } |
| | | }; |
| | | // 延迟 5 秒执行 |
| | | handler.postDelayed(delayedRunnable, 1000); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) { |
| | | |
| | | handler = new Handler(Looper.getMainLooper()); |
| | | delayedRunnable = new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | viewBinding.listSrl.endLoadingMore(); |
| | | if (!isLoadingMore) { |
| | | // 滑动到了底部,执行相应的操作 |
| | | loadNextPageMessageList(false, ++mCurrentPage, viewBinding.messageSearchEt.getText().toString(), false); |
| | | } |
| | | } |
| | | }; |
| | | // 延迟 5 秒执行 |
| | | handler.postDelayed(delayedRunnable, 10); |
| | | |
| | | return true; |
| | | } |
| | | }); |
| | | HDLRefreshViewHolder hdlRefreshViewHolder = new HDLRefreshViewHolder(_mActivity, true); |
| | | hdlRefreshViewHolder.setPullDownImageResource(R.mipmap.loading_44); |
| | | hdlRefreshViewHolder.setChangeToReleaseRefreshAnimResId(R.drawable.loading_refresh); |
| | | hdlRefreshViewHolder.setRefreshingAnimResId(R.drawable.loading_refresh_end); |
| | | viewBinding.listSrl.setRefreshViewHolder(hdlRefreshViewHolder); |
| | | } |
| | | |
| | | private void initEvent() { |
| | | |
| | |
| | | } |
| | | }); |
| | | viewBinding.messageSearchEt.addTextChangedListener(textWatcher); |
| | | |
| | | |
| | | //搜索 |
| | | viewBinding.powerStationSearchTv.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | |
| | | HdlThreadLogic.toast(_mActivity, R.string.search_content_null); |
| | | return; |
| | | } |
| | | viewBinding.messageSearchEt.clearFocus(); |
| | | hideSoftInputFromWindow(); |
| | | viewBinding.historyListParent.setVisibility(View.GONE); |
| | | viewBinding.listParent.setVisibility(View.VISIBLE); |
| | | addSearchTextToList(); |
| | | loadNextPageHouseList(true, 1, true); |
| | | } |
| | | }); |
| | | |
| | | |
| | | //下拉箭头颜色 |
| | | viewBinding.listSrl.setColorSchemeResources(R.color.text_FF245EC3); |
| | | //下拉读取 |
| | | viewBinding.listSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| | | @Override |
| | | public void onRefresh() { |
| | | viewBinding.listSrl.setRefreshing(false); |
| | | loadNextPageHouseList(true, 1, true); |
| | | } |
| | | }); |
| | | //上拉读取 |
| | | viewBinding.listRcv.addOnScrollListener(new RecyclerView.OnScrollListener() { |
| | | @Override |
| | | public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { |
| | | LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); |
| | | if (layoutManager == null) { |
| | | return; |
| | | } |
| | | int visibleItemCount = layoutManager.getChildCount(); |
| | | int totalItemCount = layoutManager.getItemCount(); |
| | | int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); |
| | | if (visibleItemCount > 0 && visibleItemCount + firstVisibleItemPosition == totalItemCount) { |
| | | if (!isHouseLoadingMore) { |
| | | // 滑动到了底部,执行相应的操作 |
| | | HdlLogLogic.print("--->滑动到了底部"); |
| | | loadNextPageHouseList(false, ++currentHouseListPage, false); |
| | | } |
| | | } |
| | | loadNextPageMessageList(true, 1, viewBinding.messageSearchEt.getText().toString(), true); |
| | | } |
| | | }); |
| | | //清除 |
| | |
| | | currSearchText = ""; |
| | | viewBinding.messageSearchEt.setText(""); |
| | | viewBinding.historyListParent.setVisibility(View.VISIBLE); |
| | | viewBinding.deviceNullDataIc.getRoot().setVisibility(View.GONE); |
| | | viewBinding.listParent.setVisibility(View.GONE); |
| | | searchHistoryAdapter.setList(searchHistoryTitleList); |
| | | clearData(); |
| | | clearCacheData(); |
| | | viewBinding.messageSearchEt.requestFocus(); |
| | | showSoftInput(); |
| | | } |
| | | }); |
| | | //删除 |
| | | viewBinding.messageDelIv.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | viewBinding.messageSearchEt.clearFocus(); |
| | | hideSoftInputFromWindow(); |
| | | searchHistoryTitleList = new ArrayList<>(); |
| | | searchHistoryAdapter.setList(searchHistoryTitleList); |
| | | viewBinding.deviceNullDataIc.getRoot().setVisibility(View.GONE); |
| | | HdlFileLogic.getInstance().deleteFile(getHistoryFileNamePath()); |
| | | } |
| | | }); |
| | |
| | | @Override |
| | | public void onClick(int position, String title) { |
| | | viewBinding.messageSearchEt.setText(title); |
| | | //搜索关键字 |
| | | currSearchText = viewBinding.messageSearchEt.getText().toString().replace(" ", ""); |
| | | if (TextUtils.isEmpty(currSearchText)) { |
| | | HdlThreadLogic.toast(_mActivity, R.string.search_content_null); |
| | | return; |
| | | } |
| | | viewBinding.messageSearchEt.clearFocus(); |
| | | hideSoftInputFromWindow(); |
| | | viewBinding.historyListParent.setVisibility(View.GONE); |
| | | viewBinding.listParent.setVisibility(View.VISIBLE); |
| | | addSearchTextToList(); |
| | | loadNextPageMessageList(true, 1, viewBinding.messageSearchEt.getText().toString(), true); |
| | | } |
| | | }); |
| | | //电站点击是事件 |
| | | //消息点击是事件 |
| | | searchMessageAdapter.setOnclickListener(new SearchMessageAdapter.OnClickListener() { |
| | | @Override |
| | | public void onClick(int position, DeviceBean deviceBean) { |
| | | |
| | | public void onClick(int position, MessageBean messageBean) { |
| | | Intent intent = new Intent(); |
| | | intent.putExtra("messageBean", new Gson().toJson(messageBean)); |
| | | intent.setClass(_mActivity, MessageInfoActivity.class); |
| | | startActivity(intent); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 显示软键盘 |
| | | */ |
| | | public void showSoftInput() { |
| | | InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); |
| | | inputMethodManager.showSoftInput(viewBinding.messageSearchEt, 0); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 隐藏软键盘 |
| | | */ |
| | | public void hideSoftInputFromWindow() { |
| | | InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); |
| | | inputMethodManager.hideSoftInputFromWindow(viewBinding.getRoot().getWindowToken(), 0); |
| | | } |
| | | |
| | | private void initView() { |
| | |
| | | viewBinding.historyListRcv.setLayoutManager(new LinearLayoutManager(_mActivity)); |
| | | viewBinding.historyListRcv.setAdapter(searchHistoryAdapter); |
| | | searchHistoryAdapter.setList(searchHistoryTitleList); |
| | | //初始化电站适配器 |
| | | //初始化消息适配器 |
| | | searchMessageAdapter = new SearchMessageAdapter(_mActivity); |
| | | viewBinding.listRcv.setLayoutManager(new LinearLayoutManager(_mActivity)); |
| | | viewBinding.listRcv.setAdapter(searchMessageAdapter); |
| | | viewBinding.messageSearchEt.post(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | viewBinding.messageSearchEt.requestFocus(); |
| | | } |
| | | }); |
| | | pullToRefresh(); |
| | | |
| | | //在界面中使用 |
| | | KeyboardStateObserverUtils.getKeyboardStateObserver(_mActivity).setKeyboardVisibilityListener(new KeyboardStateObserverUtils.OnKeyboardVisibilityListener() { |
| | | @Override |
| | | public void onKeyboardShow(int h) { |
| | | //Toast.makeText(MainActivity.this,"键盘弹出",Toast.LENGTH_SHORT).show(); |
| | | // viewBinding.messageSearchEt.requestFocus(); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | |
| | | public void onKeyboardHide(int h) { |
| | | viewBinding.messageSearchEt.clearFocus(); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 刷新列表数据 |
| | | */ |
| | | private void updateListData() { |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (searchMessageAdapter != null) { |
| | | searchMessageAdapter.setList(mList); |
| | | } |
| | | nullDataUpdateUi(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 物理按键返回事件 |
| | | */ |
| | | @Override |
| | |
| | | HdlFileLogic.getInstance().deleteFile(getHistoryFileNamePath()); |
| | | HdlFileLogic.getInstance().appendFile(getHistoryFileNamePath(), new Gson().toJson(searchHistoryTitleList)); |
| | | } |
| | | if (handler != null && delayedRunnable != null) { |
| | | handler.removeCallbacks(delayedRunnable); |
| | | } |
| | | super.onDestroy(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 刷新UI(设备) |
| | | * 加载一页数据 |
| | | * |
| | | * @param isRefreshing 表示是下拉刷新的 |
| | | * @param isShowLoading 是否启动加载框 true启动 |
| | | * @param pageNo 页数 |
| | | * @param search_title 搜索文本 |
| | | * @param isClear true表示清空缓存 |
| | | */ |
| | | private void loadNextPageHouseList(boolean isRefreshing, long pageNo, boolean isClear) { |
| | | |
| | | //搜索关键字 |
| | | if (TextUtils.isEmpty(currSearchText)) { |
| | | return; |
| | | } |
| | | |
| | | private void loadNextPageMessageList(boolean isShowLoading, int pageNo, String search_title, boolean isClear) { |
| | | if (isClear) { |
| | | clearData(); |
| | | //表示从第一页可以读取,默认清空所有缓存数据; |
| | | clearCacheData(); |
| | | } |
| | | //第一页读取数据强制读取 |
| | | if (pageNo > 1 && currentHouseListPage > currentHouseListTotal) { |
| | | --currentHouseListPage; |
| | | //第一页读取数据强制读 |
| | | if (pageNo > 1 && mCurrentPage > mCurrentTotal) { |
| | | --mCurrentPage; |
| | | //当前页不能大于总页数 |
| | | return; |
| | | } |
| | | isHouseLoadingMore = true;//标记读取状态 |
| | | if (isRefreshing) { |
| | | isLoadingMore = true;//标记读取状态 |
| | | if (isShowLoading) { |
| | | showLoading(); |
| | | } |
| | | |
| | | //获取云端上设备列表 |
| | | HdlDeviceLogic.getInstance().getPowerStationDeviceList(currSearchText, pageNo, 20, new CloudCallBeak<PageNumberObject<DeviceBean>>() { |
| | | @Override |
| | | public void onSuccess(PageNumberObject<DeviceBean> deviceClass) { |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | HdlMessageLogic.getInstance().getPageNoMessageList( |
| | | "", |
| | | pageNo, |
| | | mMessageStateType, |
| | | "", |
| | | "", |
| | | "", |
| | | "", |
| | | "", |
| | | "", |
| | | search_title, |
| | | new CloudCallBeak<HdlMessageLogic.MessageListClass>() { |
| | | @Override |
| | | public void run() { |
| | | if (isRefreshing) { |
| | | public void onSuccess(HdlMessageLogic.MessageListClass messageListClass) { |
| | | if (messageListClass != null) { |
| | | mCurrentTotal = (int) messageListClass.getTotalPage(); |
| | | mCurrentPage = (int) messageListClass.getPageNo(); |
| | | setMessageList(messageListClass.getList()); |
| | | updateListData(); |
| | | } |
| | | isLoadingMore = false; |
| | | if (isShowLoading) { |
| | | hideLoading(); |
| | | } |
| | | isHouseLoadingMore = false; |
| | | if (deviceClass != null) { |
| | | currentHouseListTotal = (int) deviceClass.getTotalPage(); |
| | | currentHouseListPage = (int) deviceClass.getPageNo(); |
| | | //更新缓存 |
| | | setDeviceList(deviceClass.getList()); |
| | | if (searchMessageAdapter != null) { |
| | | initData(); |
| | | //更新UI |
| | | searchMessageAdapter.setList(deviceList); |
| | | } |
| | | } |
| | | } |
| | | }, _mActivity, ShowErrorMode.YES); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (currentHouseListPage > 1) { |
| | | --currentHouseListPage; |
| | | public void onFailure(HDLException e) { |
| | | if (mCurrentPage > 1) { |
| | | --mCurrentPage; |
| | | } |
| | | isHouseLoadingMore = false; |
| | | if (isRefreshing) { |
| | | isLoadingMore = false; |
| | | if (isShowLoading) { |
| | | hideLoading(); |
| | | } |
| | | |
| | | } |
| | | }, _mActivity, ShowErrorMode.YES); |
| | | } |
| | | }); |
| | | }); |
| | | |
| | | |
| | | } |
| | | |
| | | private void clearData() { |
| | | if (deviceList != null && deviceList.size() > 0) { |
| | | deviceList.clear(); |
| | | |
| | | private void clearCacheData() { |
| | | if (mList != null && mList.size() > 0) { |
| | | mList.clear(); |
| | | } |
| | | } |
| | | |
| | | public void setDeviceList(List<DeviceBean> list) { |
| | | public void setMessageList(List<MessageBean> list) { |
| | | if (list == null || list.size() == 0) { |
| | | return; |
| | | } |
| | | if (this.deviceList.size() == 0) { |
| | | this.deviceList.addAll(list); |
| | | if (this.mList.size() == 0) { |
| | | this.mList.addAll(list); |
| | | return; |
| | | } |
| | | for (int i = 0; i < list.size(); i++) { |
| | |
| | | /** |
| | | * 添加设备到列表里面 |
| | | * |
| | | * @param deviceBean -设备对象 |
| | | * @param messageBean -设备对象 |
| | | */ |
| | | public void setSingleDevice(DeviceBean deviceBean) { |
| | | public void setSingleDevice(MessageBean messageBean) { |
| | | try { |
| | | if (deviceBean == null) { |
| | | if (messageBean == null) { |
| | | return; |
| | | } |
| | | boolean if_boolean = false; |
| | | for (int i = 0; i < deviceList.size(); i++) { |
| | | if (deviceList.get(i).getHomeId().equals(deviceBean.getHomeId())) { |
| | | for (int i = 0; i < mList.size(); i++) { |
| | | if (mList.get(i).getHomeId().equals(messageBean.getHomeId())) { |
| | | //存在替换 |
| | | deviceList.remove(i); |
| | | deviceList.add(i, deviceBean); |
| | | mList.remove(i); |
| | | mList.add(i, messageBean); |
| | | if_boolean = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!if_boolean) { |
| | | //没有添加 |
| | | this.deviceList.add(deviceBean); |
| | | this.mList.add(messageBean); |
| | | } |
| | | } catch (Exception e) { |
| | | String mes = e.getMessage(); |
| | |
| | | * 添加历史记录 |
| | | */ |
| | | private void addSearchTextToList() { |
| | | int maxValue = 10; |
| | | for (int i = 0; i < searchHistoryTitleList.size(); i++) { |
| | | if (searchHistoryTitleList.get(i).equals(currSearchText)) { |
| | | searchHistoryTitleList.remove(searchHistoryTitleList.get(i)); |
| | | } |
| | | } |
| | | searchHistoryTitleList.add(currSearchText); |
| | | // 倒序排列数据(搜索最后排在前面) |
| | | Collections.reverse(searchHistoryTitleList); |
| | | if (searchHistoryTitleList.size() > 10) { |
| | | searchHistoryTitleList.add(0, currSearchText); |
| | | if (searchHistoryTitleList.size() > maxValue) { |
| | | //历史记录不能超出10 |
| | | searchHistoryTitleList.remove(searchHistoryTitleList.get(11)); |
| | | searchHistoryTitleList.remove(searchHistoryTitleList.get(maxValue)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 没有消息列表的样式 |
| | | */ |
| | | private void nullDataUpdateUi() { |
| | | boolean is_data = mList != null && mList.size() > 0; |
| | | HdlCommonLogic.getInstance().nullDataUpdateUi(_mActivity, viewBinding.deviceNullDataIc.getRoot(), viewBinding.deviceNullDataIc.nullDataGifAnimationIv, viewBinding.deviceNullDataIc.nullDataTv, getString(R.string.message_alarm_data_null), is_data); |
| | | } |
| | | |
| | | /** |
| | |
| | | public String getHistoryFileNamePath() { |
| | | return HdlFileLogic.getInstance().getCurrentUserRootPath() + "/message.txt"; |
| | | } |
| | | |
| | | |
| | | } |