feat: gridview实现多个设备的展示效果

This commit is contained in:
niushuai233 2022-10-13 18:02:22 +08:00
parent ff29ccbc8b
commit ccf0ba86a6
3 changed files with 81 additions and 14 deletions

View File

@ -1,14 +1,25 @@
package cc.niushuai.project.devcontrol.ui.nav.main.device; package cc.niushuai.project.devcontrol.ui.nav.main.device;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle; 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.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView; import android.widget.TextView;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.gridlayout.widget.GridLayout;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
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.databinding.MainNavFragmentDeviceBinding; import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding;
/** /**
@ -19,27 +30,50 @@ import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding;
*/ */
public class NavDeviceFragment extends Fragment { public class NavDeviceFragment extends Fragment {
private MainNavFragmentDeviceBinding navFragmentDeviceBinding; private MainNavFragmentDeviceBinding binding;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
navFragmentDeviceBinding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); binding = MainNavFragmentDeviceBinding.inflate(inflater, container, false);
NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class); NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class);
View rootView = navFragmentDeviceBinding.getRoot(); View rootView = binding.getRoot();
// TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; // TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview;
// //
// navDeviceViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); // navDeviceViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
setDevices();
return rootView; return rootView;
} }
private void setDevices() {
// GridLayout deviceGridLayout = binding.deviceGridLayout;
GridView deviceGv = binding.deviceGv;
deviceGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
List<HashMap<String, Object>> dataItem = new ArrayList<>();
for (int i = 0; i < 3; i++) {
HashMap<String, Object> m1 = new HashMap<>();
m1.put("device_item_imageView", R.drawable.ic_mark_as_read);
m1.put("device_item_textView", "卧室灯" + i);
dataItem.add(m1);
}
SimpleAdapter gvAdapter = new SimpleAdapter(getContext(), dataItem, R.layout.device_item,
new String[]{"device_item_imageView", "device_item_textView"}, new int[]{R.id.device_item_imageView, R.id.device_item_textView});
deviceGv.setAdapter(gvAdapter);
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
navFragmentDeviceBinding = null; binding = null;
} }
} }

View File

@ -0,0 +1,25 @@
<?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="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/device_item_imageView"
android:layout_width="56dp"
android:layout_height="56dp"
app:srcCompat="@drawable/ic_home"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/device_item_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/device_item_imageView"
android:layout_centerHorizontal="true"
android:text="TextView"
/>
</RelativeLayout>

View File

@ -6,16 +6,24 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.nav.main.device.NavDeviceFragment"> tools:context=".ui.nav.main.device.NavDeviceFragment">
<TextView <RelativeLayout
android:id="@+id/nav_device_fragment_textview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:background="@color/teal_200"
android:text="@string/hello_blank_fragment"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.gridlayout.widget.GridLayout
android:id="@+id/device_gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2">
</androidx.gridlayout.widget.GridLayout>
<GridView
android:id="@+id/device_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>