diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/DeviceTypeEnum.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/DeviceTypeEnum.java index 4a8669f..356b854 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/DeviceTypeEnum.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/DeviceTypeEnum.java @@ -1,5 +1,6 @@ package cc.niushuai.project.devcontrol.base.enums; +import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.ui.BaseActivity; import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; @@ -14,15 +15,19 @@ public enum DeviceTypeEnum { /** * 开关 */ - Switch("Switch", PowerSwitchActivity.class), + Switch("Switch", "开关", R.drawable.ic_device_type_switch, PowerSwitchActivity.class), + Custom("Custom", "自定义设备", R.drawable.ic_device_type_custom, PowerSwitchActivity.class), ; - private String value; + private String text; + private int resId; private Class activity; - DeviceTypeEnum(String value, Class activity) { + DeviceTypeEnum(String value, String text, int resId, Class activity) { this.value = value; + this.text = text; + this.resId = resId; this.activity = activity; } @@ -34,6 +39,22 @@ public enum DeviceTypeEnum { this.value = value; } + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public int getResId() { + return resId; + } + + public void setResId(int resId) { + this.resId = resId; + } + public Class getActivity() { return activity; } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/DeviceAddDialogFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/DeviceAddDialogFragment.java new file mode 100644 index 0000000..fa93ae5 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/DeviceAddDialogFragment.java @@ -0,0 +1,117 @@ +package cc.niushuai.project.devcontrol.ui.nav.device; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.GridView; +import android.widget.SimpleAdapter; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; +import cc.niushuai.project.devcontrol.databinding.DeviceAddBinding; + +public class DeviceAddDialogFragment extends DialogFragment { + + private DeviceAddBinding binding; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + + binding = DeviceAddBinding.inflate(getLayoutInflater()); + + this.init(); + + return binding.getRoot(); + } + + /** + * 初始化入口 + * + * @author niushuai + * @date: 2022/10/20 17:00 + */ + private void init() { + + this.initDeviceTypeList(); + } + + /** + * 初始化设备列表页面 + * + * @author niushuai + * @date: 2022/10/20 16:59 + */ + private void initDeviceTypeList() { + GridView deviceAddGridView = binding.deviceTypeListGridView; + + SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), getDeviceTypeList(), R.layout.device_type_list, + new String[]{ + "device_type_list_key", + "device_type_list_icon", + "device_type_list_text"}, + new int[]{ + R.id.device_type_list_key, + R.id.device_type_list_icon, + R.id.device_type_list_text} + ); + + deviceAddGridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); + deviceAddGridView.setOnItemClickListener(this::deviceTypeListItemClickListener); + deviceAddGridView.setAdapter(simpleAdapter); + } + + /** + * 设备列表grid item点击事件 + * + * @param parent + * @param view + * @param position + * @param id + * @author niushuai + * @date: 2022/10/20 16:59 + */ + private void deviceTypeListItemClickListener(AdapterView parent, View view, int position, long id) { + Toast.makeText(getActivity(), "xxx", Toast.LENGTH_SHORT).show(); + } + + /** + * 加载初始化设备类型列表 + * + * @author niushuai + * @date: 2022/10/20 16:59 + * @return: {@link List>} + */ + private List> getDeviceTypeList() { + + DeviceTypeEnum[] allDeviceType = DeviceTypeEnum.values(); + + List> gvData = new ArrayList<>(allDeviceType.length); + + for (DeviceTypeEnum deviceType : allDeviceType) { + HashMap item = new HashMap<>(3); + item.put("device_type_list_key", deviceType.getValue()); + item.put("device_type_list_icon", deviceType.getResId()); + item.put("device_type_list_text", deviceType.getText()); + gvData.add(item); + } + + return gvData; + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java index 039944d..442f081 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/device/NavDeviceFragment.java @@ -25,7 +25,6 @@ import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.databinding.DeviceItemBinding; import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; -import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; import cn.hutool.core.util.RandomUtil; /** @@ -36,18 +35,18 @@ import cn.hutool.core.util.RandomUtil; */ public class NavDeviceFragment extends Fragment { - private MainNavFragmentDeviceBinding deviceBinding; + private MainNavFragmentDeviceBinding binding; private DeviceItemBinding deviceItemBinding; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - deviceBinding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); + binding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); deviceItemBinding = DeviceItemBinding.inflate(inflater, container, false); NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class); - View rootView = deviceBinding.getRoot(); + View rootView = binding.getRoot(); // TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; // @@ -55,13 +54,25 @@ public class NavDeviceFragment extends Fragment { setDevices(); + this.addListener(); + return rootView; } + private void addListener() { + binding.deviceAdd.setOnClickListener(this::deviceAddClickListener); + } + + private void deviceAddClickListener(View view) { + + DeviceAddDialogFragment dialogFragment = new DeviceAddDialogFragment(); + dialogFragment.show(getActivity().getSupportFragmentManager(), DeviceAddDialogFragment.class.getName()); + } + private void setDevices() { // GridLayout deviceGridLayout = binding.deviceGridLayout; - GridView deviceGv = deviceBinding.deviceGv; + GridView deviceGv = binding.deviceGv; deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); deviceGv.setOnItemClickListener(this::onItemClick); @@ -107,10 +118,10 @@ public class NavDeviceFragment extends Fragment { /** * 条目点击事件 * - * @param parent 父view - * @param view 被点击的view + * @param parent 父view + * @param view 被点击的view * @param position 当前view中的位置顺序 - * @param id 组件id + * @param id 组件id * @author niushuai * @date: 2022/10/19 11:13 */ @@ -125,7 +136,7 @@ public class NavDeviceFragment extends Fragment { @Override public void onDestroyView() { super.onDestroyView(); - deviceBinding = null; + binding = null; deviceItemBinding = null; } diff --git a/app/src/main/res/layout/device_add.xml b/app/src/main/res/layout/device_add.xml new file mode 100644 index 0000000..50c895a --- /dev/null +++ b/app/src/main/res/layout/device_add.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/device_type_list.xml b/app/src/main/res/layout/device_type_list.xml new file mode 100644 index 0000000..ed8a133 --- /dev/null +++ b/app/src/main/res/layout/device_type_list.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 3635c03..b7a3837 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -15,6 +15,11 @@ + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f29c70..0597e86 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,6 +15,7 @@ + Hello World Hello blank fragment Hello blank textview 1 Hello blank textview 2 @@ -32,6 +33,12 @@ + + 选择设备类型 + + + + 更换图标 参数设置