wjc
2025-04-24 8a249407f9a91b696b88d74eea0fbf789d0ee6a4
app/src/main/java/com/hdl/photovoltaic/ui/powerstation/DeviceSearchActivity.java
@@ -6,7 +6,10 @@
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Context;
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;
@@ -36,6 +39,8 @@
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.utils.KeyboardStateObserverUtils;
import com.hdl.photovoltaic.utils.URLEncodingUtils;
import com.hdl.photovoltaic.widget.refreshlayout.BGARefreshLayout;
import com.hdl.photovoltaic.widget.refreshlayout.HDLRefreshViewHolder;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
@@ -49,7 +54,8 @@
 */
public class DeviceSearchActivity extends CustomBaseActivity {
    private Handler handler;
    private Runnable delayedRunnable;
    private ActivityDeviceSearchBinding viewBinding;
    SearchDeviceAdapter searchDeviceAdapter;//设备适配器
@@ -57,9 +63,9 @@
    private List<CloudInverterDeviceBean> deviceList = new ArrayList<>();
    private int currentHouseListPage = 0; // 当前电站列表页码
    private int currentHouseListTotal = 0; // 电站列表总页码
    private boolean isHouseLoadingMore = false; // 标记电站列表正在加载更多数据
    private int currentDeviceListPage = 0; // 当前电站列表页码
    private int currentDeviceListTotal = 0; // 电站列表总页码
    private boolean isDeviceLoadingMore = false; // 标记电站列表正在加载更多数据
    private String currSearchText;
@@ -74,6 +80,7 @@
    @Override
    public void onBindView(Bundle savedInstanceState) {
        setStatusBarTranslucent();
        getWindow().setNavigationBarColor(getColor(R.color.text_FF000000));
        //历史记录文件夹创建
        HdlFileLogic.getInstance().createFileDir(HdlFileLogic.getInstance().getCurrentUserRootPath());
        //初始化数据
@@ -96,6 +103,54 @@
        }
    }
    /**
     * 下拉刷新的逻辑
     */
    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();
                        loadNextPageDeviceList(true, 1, 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 (!isDeviceLoadingMore) {
                            // 滑动到了底部,执行相应的操作
                            HdlLogLogic.print("--->滑动到了底部");
                            loadNextPageDeviceList(false, ++currentDeviceListPage, 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() {
@@ -119,44 +174,15 @@
                    HdlThreadLogic.toast(_mActivity, R.string.search_content_null);
                    return;
                }
                viewBinding.powerStationSearchEt.clearFocus();
                hideSoftInputFromWindow();
                viewBinding.historyListParent.setVisibility(View.GONE);
                viewBinding.listParent.setVisibility(View.VISIBLE);
                addSearchTextToList();
                loadNextPageHouseList(true, 1, true);
                loadNextPageDeviceList(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);
                    }
                }
            }
        });
        //清除
        viewBinding.powerStationClearIv.setOnClickListener(new View.OnClickListener() {
            @Override
@@ -176,6 +202,8 @@
        viewBinding.powerStationDelIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinding.powerStationSearchEt.clearFocus();
                hideSoftInputFromWindow();
                searchHistoryTitleList = new ArrayList<>();
                searchHistoryAdapter.setList(searchHistoryTitleList);
                viewBinding.deviceNullDataIc.getRoot().setVisibility(View.GONE);
@@ -194,10 +222,12 @@
                    HdlThreadLogic.toast(_mActivity, R.string.search_content_null);
                    return;
                }
                viewBinding.powerStationSearchEt.clearFocus();
                hideSoftInputFromWindow();
                viewBinding.historyListParent.setVisibility(View.GONE);
                viewBinding.listParent.setVisibility(View.VISIBLE);
                addSearchTextToList();
                loadNextPageHouseList(true, 1, true);
                loadNextPageDeviceList(true, 1, true);
            }
        });
        //设备点击是事件
@@ -218,9 +248,19 @@
                    @Override
                    public void onSuccess(List<GatewayBean> obj) {
                        GatewayBean newGatewayBean = HDLLinkLocalGateway.getInstance().getLocalGateway(deviceBean.getOsn());
                        if (newGatewayBean == null) {
                            HdlThreadLogic.toast(_mActivity, getString(R.string.device_off));
                            return;
                        }
                        if (TextUtils.isEmpty(newGatewayBean.getGatewayId()) || TextUtils.isEmpty(newGatewayBean.getAesKey())) {
                            //这里只支持远程调试,远程拿不到mqtt通讯秘钥以及gatewayId;
                            HdlThreadLogic.toast(_mActivity,getString(R.string.Device_failed_to_certificate)+"(124004)");
                            return;
                        }
                        String jsonEncryption = URLEncodingUtils.encodeURIComponent(new Gson().toJson(newGatewayBean));
                        String path = HDLUniMP.UNI_EVENT_OPEN_DEVICE_DETAILS + "?inverterInfo=" + jsonEncryption;
                        HdlUniLogic.getInstance().openUniMP(path, null);
                    }
                    @Override
@@ -253,13 +293,14 @@
                viewBinding.powerStationSearchEt.requestFocus();
            }
        });
        pullToRefresh();
        //在界面中使用
        KeyboardStateObserverUtils.getKeyboardStateObserver(_mActivity).setKeyboardVisibilityListener(new KeyboardStateObserverUtils.OnKeyboardVisibilityListener() {
            @Override
            public void onKeyboardShow(int h) {
                //Toast.makeText(MainActivity.this,"键盘弹出",Toast.LENGTH_SHORT).show();
                viewBinding.powerStationSearchEt.requestFocus();
//                viewBinding.powerStationSearchEt.requestFocus();
            }
@@ -330,6 +371,9 @@
            HdlFileLogic.getInstance().deleteFile(getHistoryFileNamePath());
            HdlFileLogic.getInstance().appendFile(getHistoryFileNamePath(), new Gson().toJson(searchHistoryTitleList));
        }
        if (handler != null && delayedRunnable != null) {
            handler.removeCallbacks(delayedRunnable);
        }
        super.onDestroy();
    }
@@ -338,7 +382,7 @@
     *
     * @param isRefreshing 表示是下拉刷新的
     */
    private void loadNextPageHouseList(boolean isRefreshing, long pageNo, boolean isClear) {
    private void loadNextPageDeviceList(boolean isRefreshing, long pageNo, boolean isClear) {
        //搜索关键字
        if (TextUtils.isEmpty(currSearchText)) {
@@ -349,12 +393,12 @@
            clearData();
        }
        //第一页读取数据强制读取
        if (pageNo > 1 && currentHouseListPage > currentHouseListTotal) {
            --currentHouseListPage;
        if (pageNo > 1 && currentDeviceListPage > currentDeviceListTotal) {
            --currentDeviceListPage;
            //当前页不能大于总页数
            return;
        }
        isHouseLoadingMore = true;//标记读取状态
        isDeviceLoadingMore = true;//标记读取状态
        if (isRefreshing) {
            showLoading();
        }
@@ -369,10 +413,10 @@
                        if (isRefreshing) {
                            hideLoading();
                        }
                        isHouseLoadingMore = false;
                        isDeviceLoadingMore = false;
                        if (deviceClass != null) {
                            currentHouseListTotal = (int) deviceClass.getTotalPage();
                            currentHouseListPage = (int) deviceClass.getPageNo();
                            currentDeviceListTotal = (int) deviceClass.getTotalPage();
                            currentDeviceListPage = (int) deviceClass.getPageNo();
                            //更新缓存
                            setDeviceList(deviceClass.getList());
                            if (searchDeviceAdapter != null) {
@@ -391,10 +435,10 @@
                HdlThreadLogic.runMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (currentHouseListPage > 1) {
                            --currentHouseListPage;
                        if (currentDeviceListPage > 1) {
                            --currentDeviceListPage;
                        }
                        isHouseLoadingMore = false;
                        isDeviceLoadingMore = false;
                        if (isRefreshing) {
                            hideLoading();
                        }