mirror of
https://github.com/niushuai233/DevControl.git
synced 2024-10-27 22:43:20 +08:00
feat: 给每一个item添加点击事件
This commit is contained in:
parent
f9af20a555
commit
14c849d44f
@ -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<HashMap<String, Object>> dataItem = new ArrayList<>();
|
||||
for (int i = 0; i < 31; i++) {
|
||||
HashMap<String, Object> 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<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
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
binding = null;
|
||||
deviceBinding = null;
|
||||
deviceItemBinding = null;
|
||||
}
|
||||
}
|
@ -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"
|
||||
>
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:id="@id/device_item_cardView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_centerInParent="true"
|
||||
app:cardCornerRadius="12dp"
|
||||
android:layout_centerInParent="true">
|
||||
>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@id/device_item_cardView_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="10dp"
|
||||
>
|
||||
android:layout_marginLeft="10dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@id/device_item_cardView_image"
|
||||
@ -33,15 +36,15 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/device_item_cardView_image"
|
||||
android:layout_marginTop="10dp"
|
||||
android:textSize="12dp"
|
||||
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:text="TextView"
|
||||
android:textSize="12dp" />
|
||||
</RelativeLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
Loading…
x
Reference in New Issue
Block a user