feat: icon选择页面

This commit is contained in:
niushuai233 2022-10-25 18:01:13 +08:00
parent 8f885a045e
commit 5bb6f8b609
8 changed files with 273 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import cc.niushuai.project.devcontrol.base.util.ActivityUtil;
import cc.niushuai.project.devcontrol.base.util.GlobalVariables;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.UiUtil;
import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment;
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
import cn.hutool.core.util.StrUtil;
@ -147,6 +148,39 @@ public abstract class BaseActivity extends AppCompatActivity {
findViewById(R.id.activity_title_more_set).setOnClickListener(onClickListener);
}
/**
* 为icon选择绑定点击事件
*
* @author niushuai
* @date: 2022/10/25 16:55
*/
protected void activityIconSelectClickListener() {
findViewById(R.id.device_add_select_icon).setOnClickListener(view -> {
IconSelectDialogFragment iconSelectDialogFragment = new IconSelectDialogFragment();
iconSelectDialogFragment.show(getSupportFragmentManager(), IconSelectDialogFragment.class.getName());
getSupportFragmentManager().setFragmentResultListener(IconSelectDialogFragment.class.getSimpleName(),
this, (requestKey, result) -> {
int iconResId = result.getInt(requestKey + Keys.ID);
System.out.println(iconResId);
device.setIconId(iconResId);
});
});
}
/**
* 为icon选择绑定点击事件
*
* @param onClickListener
* @author niushuai
* @date: 2022/10/25 16:55
*/
protected void activityIconSelectClickListener(View.OnClickListener onClickListener) {
findViewById(R.id.device_add_select_icon).setOnClickListener(onClickListener);
}
/**
* 重建缓存
*

View File

@ -0,0 +1,108 @@
package cc.niushuai.project.devcontrol.ui.common;
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.TextView;
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.IconEnum;
import cc.niushuai.project.devcontrol.databinding.LayoutIconSelectBinding;
/**
* icon选择弹窗
*
* @author niushuai233
* @date 2022/10/25 16:51
*/
public class IconSelectDialogFragment extends DialogFragment {
private LayoutIconSelectBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = LayoutIconSelectBinding.inflate(getLayoutInflater());
this.init();
return binding.getRoot();
}
/**
* 初始化入口
*
* @author niushuai
* @date: 2022/10/25 17:21
*/
private void init() {
this.initIconList();
}
/**
* 加载icon图标列表
*
* @author niushuai
* @date: 2022/10/25 17:22
*/
private void initIconList() {
GridView iconListGridView = binding.iconListGridView;
SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), getIconList(), R.layout.layout_icon_select,
new String[]{"icon_select_list_key", "icon_select_list_icon", "icon_select_list_text"},
new int[]{R.id.icon_select_list_key, R.id.icon_select_list_icon, R.id.icon_select_list_text}
);
iconListGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
iconListGridView.setOnItemClickListener(this::iconListClickListener);
iconListGridView.setAdapter(simpleAdapter);
}
private void iconListClickListener(AdapterView<?> parent, View view, int position, long id) {
// 跳转到相应的activity
String iconId = ((TextView) view.findViewById(R.id.device_type_list_key)).getText().toString();
// 关闭弹出框
this.dismiss();
}
private List<? extends Map<String, ?>> getIconList() {
IconEnum[] allIcon = IconEnum.values();
List<HashMap<String, Object>> gvData = new ArrayList<>(allIcon.length);
for (IconEnum icon : allIcon) {
HashMap<String, Object> item = new HashMap<>(3);
item.put("icon_select_list_key", icon.getIconIdDark());
item.put("icon_select_list_icon", icon.getIconIdDark());
item.put("icon_select_list_text", icon.getDesc());
gvData.add(item);
}
return gvData;
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
binding = null;
}
}

View File

@ -0,0 +1,23 @@
package cc.niushuai.project.devcontrol.ui.common;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
/**
* TODO
*
* @author niushuai233
* @date 2022/10/25 17:11
*/
public class IconSelectViewModel extends ViewModel {
private final MutableLiveData<Integer> iconResId = new MutableLiveData<>();
public MutableLiveData<Integer> getIconResId() {
return iconResId;
}
public void setIconResId(Integer val) {
iconResId.setValue(val);
}
}

View File

@ -17,6 +17,7 @@ import cc.niushuai.project.devcontrol.base.util.ToastUtil;
import cc.niushuai.project.devcontrol.databinding.ActivityDeviceAddPowerSwitchBinding;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.entity.Device;
import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment;
import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
@ -56,6 +57,13 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity {
this.activityButtonBackClickListener(this);
// 保存设备信息事件
this.activityButtonMoreSetClickListener(this::confirm4SaveDataClickListener);
// icon选择器点击事件
this.activityIconSelectClickListener();
}
protected void iconSelectClickListener(View view) {
}
/**

View File

@ -0,0 +1,33 @@
<?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/icon_select_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_bg_color"
android:text="@string/icon_select_title"
android:textSize="24dp" />
<GridView
android:id="@+id/icon_list_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/icon_select_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/icon_select_list_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ImageView
android:id="@id/icon_select_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/icon_select_list_text"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:layout_below="@id/icon_select_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

@ -9,6 +9,11 @@
<item name="activity_title_description" type="id"/>
<item name="activity_title_more_set" type="id"/>
<item name="icon_select_list" type="id"/>
<item name="icon_select_list_key" type="id"/>
<item name="icon_select_list_text" type="id"/>
<item name="icon_select_list_icon" type="id"/>
<!--common end-->
<!--首页 start-->

View File

@ -37,6 +37,9 @@
<string name="device_add_type_select">选择设备类型</string>
<!--添加设备 end-->
<!--添加icon start-->
<string name="icon_select_title">选择图标</string>
<!--添加icon end-->
<!--开关设置页面 start-->
<string name="power_switch_set_iconChange">更换图标</string>