fix: 调整初始化顺序&设置新增后刷新

This commit is contained in:
niushuai233 2022-10-25 13:55:04 +08:00
parent f904d4aa60
commit 7f5dbacc10
2 changed files with 32 additions and 7 deletions

View File

@ -18,6 +18,9 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
// 最先执行初始化代码
App.init(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater()); activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
@ -25,8 +28,6 @@ public class MainActivity extends AppCompatActivity {
// 设置底部导航栏 // 设置底部导航栏
setBottomNavigationView(); setBottomNavigationView();
App.init(this);
} }
/** /**

View File

@ -16,16 +16,17 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.GlobalVariables;
import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.databinding.DeviceItemBinding; import cc.niushuai.project.devcontrol.databinding.DeviceItemBinding;
import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding;
import cn.hutool.core.util.RandomUtil; import cc.niushuai.project.devcontrol.vo.DeviceInfo;
/** /**
* 设备页面ui * 设备页面ui
@ -37,8 +38,10 @@ public class NavDeviceFragment extends Fragment {
private MainNavFragmentDeviceBinding binding; private MainNavFragmentDeviceBinding binding;
private DeviceItemBinding deviceItemBinding; private DeviceItemBinding deviceItemBinding;
private List<DeviceInfo> deviceInfoList;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -59,6 +62,7 @@ public class NavDeviceFragment extends Fragment {
return rootView; return rootView;
} }
private void addListener() { private void addListener() {
binding.deviceAdd.setOnClickListener(this::deviceAddClickListener); binding.deviceAdd.setOnClickListener(this::deviceAddClickListener);
} }
@ -71,6 +75,11 @@ public class NavDeviceFragment extends Fragment {
private void setDevices() { private void setDevices() {
deviceInfoList = GlobalVariables.DEVICE_INFO_MAP.values()
.stream()
.sorted(Comparator.comparingInt(DeviceInfo::getOrder))
.collect(Collectors.toList());
// GridLayout deviceGridLayout = binding.deviceGridLayout; // GridLayout deviceGridLayout = binding.deviceGridLayout;
GridView deviceGv = binding.deviceGv; GridView deviceGv = binding.deviceGv;
deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
@ -80,7 +89,8 @@ public class NavDeviceFragment extends Fragment {
// SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item, // SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item,
// new String[]{"device_item_cardView_text"}, new int[]{R.id.device_item_cardView_text}); // new String[]{"device_item_cardView_text"}, new int[]{R.id.device_item_cardView_text});
SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), loadDeviceInfo(), R.layout.device_item, List<HashMap<String, Object>> data = loadDeviceInfo();
SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), data, R.layout.device_item,
new String[]{"device_item_cardView_id", "device_item_cardView_image", "device_item_cardView_text"}, new String[]{"device_item_cardView_id", "device_item_cardView_image", "device_item_cardView_text"},
new int[]{R.id.device_item_cardView_id, R.id.device_item_cardView_image, R.id.device_item_cardView_text}); new int[]{R.id.device_item_cardView_id, R.id.device_item_cardView_image, R.id.device_item_cardView_text});
@ -98,10 +108,10 @@ public class NavDeviceFragment extends Fragment {
List<HashMap<String, Object>> gvData = new ArrayList<>(); List<HashMap<String, Object>> gvData = new ArrayList<>();
// 此处数据要加载为真实数据 // 此处数据要加载为真实数据
List<DeviceInfo> list = DeviceInfo.mock(RandomUtil.randomInt(9, 27), R.drawable.ic_device_light_1); // List<DeviceInfo> list = DeviceInfo.mock(RandomUtil.randomInt(9, 27), R.drawable.ic_device_light_1);
// 循环处理数据 // 循环处理数据
for (DeviceInfo deviceInfo : list) { for (DeviceInfo deviceInfo : deviceInfoList) {
HashMap<String, Object> m1 = new HashMap<>(); HashMap<String, Object> m1 = new HashMap<>();
// 设置当前设备的图标和名称 // 设置当前设备的图标和名称
@ -140,4 +150,18 @@ public class NavDeviceFragment extends Fragment {
deviceItemBinding = null; deviceItemBinding = null;
} }
/**
* {@inheritDoc}
* <p>
* Dispatch onResume() to fragments. Note that for better inter-operation
* with older versions of the platform, at the point of this call the
* fragments attached to the activity are <em>not</em> resumed.
*/
@Override
public void onResume() {
super.onResume();
// 重新初始化值
GlobalVariables.initDeviceInfoMap();
this.setDevices();
}
} }