feat: 给每一个item添加点击事件

This commit is contained in:
niushuai233 2022-10-17 17:25:45 +08:00
parent f9af20a555
commit 14c849d44f
2 changed files with 62 additions and 20 deletions

View File

@ -6,10 +6,13 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView; import android.widget.GridView;
import android.widget.SimpleAdapter; import android.widget.SimpleAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.gridlayout.widget.GridLayout; import androidx.gridlayout.widget.GridLayout;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
@ -20,7 +23,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import cc.niushuai.project.devcontrol.R; 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 cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding;
import cn.hutool.core.util.RandomUtil;
/** /**
* 设备页面ui * 设备页面ui
@ -30,16 +36,18 @@ import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding;
*/ */
public class NavDeviceFragment extends Fragment { public class NavDeviceFragment extends Fragment {
private MainNavFragmentDeviceBinding binding; private MainNavFragmentDeviceBinding deviceBinding;
private DeviceItemBinding deviceItemBinding;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { 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); NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class);
View rootView = binding.getRoot(); View rootView = deviceBinding.getRoot();
// TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; // TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview;
// //
@ -53,30 +61,61 @@ public class NavDeviceFragment extends Fragment {
private void setDevices() { private void setDevices() {
// GridLayout deviceGridLayout = binding.deviceGridLayout; // GridLayout deviceGridLayout = binding.deviceGridLayout;
GridView deviceGv = binding.deviceGv; GridView deviceGv = deviceBinding.deviceGv;
deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT)); deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
List<HashMap<String, Object>> dataItem = new ArrayList<>(); deviceGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
for (int i = 0; i < 31; i++) { @Override
HashMap<String, Object> m1 = new HashMap<>(); public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View text = view.findViewById(R.id.device_item_cardView_text);
m1.put("device_item_cardView_image", R.drawable.ic_device_light_1); String x = ";";
m1.put("device_item_cardView_text", "卧室灯" + i); if (text != null) {
dataItem.add(m1); 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, // 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(), 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}); 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); deviceGv.setAdapter(gvAdapter);
} }
/**
* 读取数据 加载数据库信息
*
* @author niushuai
* @date: 2022/10/17 16:58
* @return: {@link List<HashMap<String, Object>>}
*/
private List<HashMap<String, Object>> loadDeviceInfo() {
List<HashMap<String, Object>> gvData = new ArrayList<>();
// 此处数据要加载为真实数据
List<DeviceInfo> list = DeviceInfo.mock(RandomUtil.randomInt(9, 27), R.drawable.ic_device_light_1);
// 循环处理数据
for (DeviceInfo deviceInfo : list) {
HashMap<String, Object> 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 @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
binding = null; deviceBinding = null;
deviceItemBinding = null;
} }
} }

View File

@ -6,21 +6,24 @@
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
android:layout_height="100dp" android:layout_height="100dp"
android:clickable="false"
android:longClickable="false"
android:contextClickable="false"
> >
<androidx.cardview.widget.CardView <androidx.cardview.widget.CardView
android:id="@id/device_item_cardView" android:id="@id/device_item_cardView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerInParent="true"
app:cardCornerRadius="12dp" app:cardCornerRadius="12dp"
android:layout_centerInParent="true"> >
<RelativeLayout <RelativeLayout
android:id="@id/device_item_cardView_content" android:id="@id/device_item_cardView_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp">
>
<ImageView <ImageView
android:id="@id/device_item_cardView_image" android:id="@id/device_item_cardView_image"
@ -33,15 +36,15 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/device_item_cardView_image" android:layout_below="@id/device_item_cardView_image"
android:layout_marginTop="10dp"
android:textSize="12dp"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:shadowColor="#000000" android:shadowColor="#000000"
android:shadowDx="0.3" android:shadowDx="0.3"
android:shadowDy="0.3" android:shadowDy="0.3"
android:shadowRadius="0.3" android:shadowRadius="0.3"
android:text="TextView" /> android:text="TextView"
android:textSize="12dp" />
</RelativeLayout> </RelativeLayout>
</androidx.cardview.widget.CardView> </androidx.cardview.widget.CardView>