feat: 新增设备添加弹窗

This commit is contained in:
niushuai233 2022-10-20 17:47:41 +08:00
parent ade06068fd
commit 8b1617ce45
7 changed files with 267 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package cc.niushuai.project.devcontrol.base.enums; 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.base.ui.BaseActivity;
import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; 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 value;
private String text;
private int resId;
private Class<? extends BaseActivity> activity; private Class<? extends BaseActivity> activity;
DeviceTypeEnum(String value, Class<? extends BaseActivity> activity) { DeviceTypeEnum(String value, String text, int resId, Class<? extends BaseActivity> activity) {
this.value = value; this.value = value;
this.text = text;
this.resId = resId;
this.activity = activity; this.activity = activity;
} }
@ -34,6 +39,22 @@ public enum DeviceTypeEnum {
this.value = value; 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<? extends BaseActivity> getActivity() { public Class<? extends BaseActivity> getActivity() {
return activity; return activity;
} }

View File

@ -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<? extends Map<String,?>>}
*/
private List<? extends Map<String, ?>> getDeviceTypeList() {
DeviceTypeEnum[] allDeviceType = DeviceTypeEnum.values();
List<HashMap<String, Object>> gvData = new ArrayList<>(allDeviceType.length);
for (DeviceTypeEnum deviceType : allDeviceType) {
HashMap<String, Object> 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;
}
}

View File

@ -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.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 cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
/** /**
@ -36,18 +35,18 @@ import cn.hutool.core.util.RandomUtil;
*/ */
public class NavDeviceFragment extends Fragment { public class NavDeviceFragment extends Fragment {
private MainNavFragmentDeviceBinding deviceBinding; private MainNavFragmentDeviceBinding binding;
private DeviceItemBinding deviceItemBinding; private DeviceItemBinding deviceItemBinding;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
deviceBinding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); binding = MainNavFragmentDeviceBinding.inflate(inflater, container, false);
deviceItemBinding = DeviceItemBinding.inflate(inflater, container, false); deviceItemBinding = DeviceItemBinding.inflate(inflater, container, false);
NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class); NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class);
View rootView = deviceBinding.getRoot(); View rootView = binding.getRoot();
// TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; // TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview;
// //
@ -55,13 +54,25 @@ public class NavDeviceFragment extends Fragment {
setDevices(); setDevices();
this.addListener();
return rootView; 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() { private void setDevices() {
// GridLayout deviceGridLayout = binding.deviceGridLayout; // GridLayout deviceGridLayout = binding.deviceGridLayout;
GridView deviceGv = deviceBinding.deviceGv; GridView deviceGv = binding.deviceGv;
deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
deviceGv.setOnItemClickListener(this::onItemClick); deviceGv.setOnItemClickListener(this::onItemClick);
@ -107,10 +118,10 @@ public class NavDeviceFragment extends Fragment {
/** /**
* 条目点击事件 * 条目点击事件
* *
* @param parent 父view * @param parent 父view
* @param view 被点击的view * @param view 被点击的view
* @param position 当前view中的位置顺序 * @param position 当前view中的位置顺序
* @param id 组件id * @param id 组件id
* @author niushuai * @author niushuai
* @date: 2022/10/19 11:13 * @date: 2022/10/19 11:13
*/ */
@ -125,7 +136,7 @@ public class NavDeviceFragment extends Fragment {
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
deviceBinding = null; binding = null;
deviceItemBinding = null; deviceItemBinding = null;
} }

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="500dp"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/device_add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:background="@color/main_bg_color"
android:text="@string/device_add_type_select"/>
<GridView
android:id="@+id/device_type_list_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/device_add_layout"
android:background="@color/main_bg_color"
android:horizontalSpacing="12dp"
android:numColumns="2"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:verticalSpacing="12dp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:clickable="false"
android:longClickable="false"
android:contextClickable="false"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:cardCornerRadius="12dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:id="@id/device_type_list_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
<ImageView
android:id="@id/device_type_list_icon"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/ic_device_type_custom" />
<TextView
android:id="@id/device_type_list_text"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:layout_below="@id/device_type_list_icon"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:shadowColor="#000000"
android:shadowDx="0.3"
android:shadowDy="0.3"
android:shadowRadius="0.3"
android:text="TextView"
android:textSize="12dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>

View File

@ -15,6 +15,11 @@
<item name="device_add" type="id"/> <item name="device_add" type="id"/>
<item name="device_type_list" type="id"/>
<item name="device_type_list_key" type="id"/>
<item name="device_type_list_text" type="id"/>
<item name="device_type_list_icon" type="id"/>
<!-- 首页 底部导航栏 start--> <!-- 首页 底部导航栏 start-->
<item name="bottom_nav_device" type="id"/> <item name="bottom_nav_device" type="id"/>
<item name="bottom_nav_log" type="id"/> <item name="bottom_nav_log" type="id"/>

View File

@ -15,6 +15,7 @@
<!--common end--> <!--common end-->
<!--测试字符串 start--> <!--测试字符串 start-->
<string name="hello_world">Hello World</string>
<string name="hello_blank_fragment">Hello blank fragment</string> <string name="hello_blank_fragment">Hello blank fragment</string>
<string name="test_hello_blank_textview_1">Hello blank textview 1</string> <string name="test_hello_blank_textview_1">Hello blank textview 1</string>
<string name="test_hello_blank_textview_2">Hello blank textview 2</string> <string name="test_hello_blank_textview_2">Hello blank textview 2</string>
@ -32,6 +33,12 @@
<!--设备 start--> <!--设备 start-->
<!--添加设备 start-->
<string name="device_add_type_select">选择设备类型</string>
<!--添加设备 end-->
<!--开关设置页面 start--> <!--开关设置页面 start-->
<string name="power_switch_set_iconChange">更换图标</string> <string name="power_switch_set_iconChange">更换图标</string>
<string name="power_switch_set_paramSet">参数设置</string> <string name="power_switch_set_paramSet">参数设置</string>