diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java index fa36d72..b7ac57c 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java @@ -6,10 +6,13 @@ 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 android.widget.Toast; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.gridlayout.widget.GridLayout; import androidx.lifecycle.ViewModelProvider; @@ -20,7 +23,10 @@ import java.util.List; import java.util.Map; import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.entity.device.DeviceInfo; +import cc.niushuai.project.devcontrol.databinding.DeviceItemBinding; import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; +import cn.hutool.core.util.RandomUtil; /** * 设备页面ui @@ -30,16 +36,18 @@ import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; */ public class NavDeviceFragment extends Fragment { - private MainNavFragmentDeviceBinding binding; + private MainNavFragmentDeviceBinding deviceBinding; + private DeviceItemBinding deviceItemBinding; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - binding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); + deviceBinding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); + deviceItemBinding = DeviceItemBinding.inflate(inflater, container, false); NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class); - View rootView = binding.getRoot(); + View rootView = deviceBinding.getRoot(); // TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; // @@ -53,30 +61,61 @@ public class NavDeviceFragment extends Fragment { private void setDevices() { // GridLayout deviceGridLayout = binding.deviceGridLayout; - GridView deviceGv = binding.deviceGv; + GridView deviceGv = deviceBinding.deviceGv; deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); - List> dataItem = new ArrayList<>(); - for (int i = 0; i < 31; i++) { - HashMap m1 = new HashMap<>(); - - m1.put("device_item_cardView_image", R.drawable.ic_device_light_1); - m1.put("device_item_cardView_text", "卧室灯" + i); - dataItem.add(m1); - } + deviceGv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + View text = view.findViewById(R.id.device_item_cardView_text); + String x = ";"; + if (text != null) { + x = ((TextView) text).getText().toString(); + } + Toast.makeText(getContext(), "Item Clicked" + x, Toast.LENGTH_SHORT).show(); + } + }); // SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item, // new String[]{"device_item_cardView_text"}, new int[]{R.id.device_item_cardView_text}); - SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item, + SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), loadDeviceInfo(), R.layout.device_item, new String[]{"device_item_cardView_image", "device_item_cardView_text"}, new int[]{R.id.device_item_cardView_image, R.id.device_item_cardView_text}); deviceGv.setAdapter(gvAdapter); } + /** + * 读取数据 加载数据库信息 + * + * @author niushuai + * @date: 2022/10/17 16:58 + * @return: {@link List>} + */ + private List> loadDeviceInfo() { + List> gvData = new ArrayList<>(); + + // 此处数据要加载为真实数据 + List list = DeviceInfo.mock(RandomUtil.randomInt(9, 27), R.drawable.ic_device_light_1); + + // 循环处理数据 + for (DeviceInfo deviceInfo : list) { + + HashMap m1 = new HashMap<>(); + // 设置当前设备的图标和名称 + m1.put("device_item_cardView_image", deviceInfo.getIconId()); + m1.put("device_item_cardView_text", deviceInfo.getName()); + + // 统一添加到data中 + gvData.add(m1); + } + return gvData; + } + @Override public void onDestroyView() { super.onDestroyView(); - binding = null; + deviceBinding = null; + deviceItemBinding = null; } } \ No newline at end of file diff --git a/app/src/main/res/layout/device_item.xml b/app/src/main/res/layout/device_item.xml index 4a04519..770285c 100644 --- a/app/src/main/res/layout/device_item.xml +++ b/app/src/main/res/layout/device_item.xml @@ -6,21 +6,24 @@ android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_height="100dp" + android:clickable="false" + android:longClickable="false" + android:contextClickable="false" > + > + android:layout_marginLeft="10dp"> + android:text="TextView" + android:textSize="12dp" />