android中简单的listview代码
16lz
2022-05-27
1.主界面布局xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="#339933" android:gravity="center" android:text="配置设备" android:textColor="#ffffff" android:textSize="20dp" /> <ListView android:id="@+id/smart_control_lamp_listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
2.SceneMatchSwitchActivity界面代码
public class SceneMatchSwitchActivity extends Activity { private ListView mLampListview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载布局 setContentView(R.layout.activity_scene_match_switches); mLampListview = (ListView) findViewById(R.id.smart_control_lamp_listview); // listview绑定Adapter mLampListview.setAdapter(new myAdapter()); // 获取服务器反馈回来的设备类型 } private class myAdapter extends BaseAdapter { @Override public int getCount() { // 返回listview的item的总数 return 7; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { // 通过布局创建listview的item convertView = View.inflate(getApplicationContext(), R.layout.item_bluetooth_device_status, null); holder = new ViewHolder(); convertView.setTag(holder); // 给holder找view holder.mTextViewDeviceName = (TextView) convertView.findViewById(R.id.bluetooth_device_name_textview); holder.mTextViewDeviceState = (TextView) convertView.findViewById(R.id.bluetooth_device_state); holder.mTextViewDeviceSwitch = (ImageView) convertView.findViewById(R.id.bluetooth_device_switch_imageview); holder.mTextViewDeviceIcon = (ImageView) convertView.findViewById(R.id.bluetooth_device_icon_imageview); } else { holder = (ViewHolder) convertView.getTag(); } // 给控件设置数据 // mTextViewPosition.setText("位置 " + position); // mTextViewName.setText("姓名 " + position); return convertView; } // holder把item的控件变量集合起来,避免每次加载都再找一次id private class ViewHolder { TextView mTextViewDeviceName; // 设备名字 TextView mTextViewDeviceState; // 设备状态 ImageView mTextViewDeviceSwitch; // 设备打开或者关闭 ImageView mTextViewDeviceIcon; // 设备图片 } }}
3.listview要加载的item样式:图片以及布局代码
- 布局代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:padding="5dp" > <ImageView android:id="@+id/bluetooth_device_switch_imageview" style="@style/HotelModeSelect_001" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="30dp" android:src="@drawable/openguan" /> <ImageView android:id="@+id/bluetooth_device_icon_imageview" style="@style/HotelModeSelect_001" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="9dp" android:src="@drawable/lock_close_icon" /> <LinearLayout style="@style/HotelModeSelect_001" android:layout_centerVertical="true" android:layout_marginLeft="16dp" android:layout_toRightOf="@+id/bluetooth_device_icon_imageview" android:orientation="vertical" > <TextView android:id="@+id/bluetooth_device_name_textview" style="@style/HotelModeSelect_001" android:textColor="#000000" android:text="门锁" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" > <TextView style="@style/HotelModeSelect_001" android:text="状态:" android:textColor="#66000000" android:textSize="10sp" /> <TextView android:id="@+id/bluetooth_device_state" style="@style/HotelModeSelect_001" android:text="未上锁" android:textColor="#66000000" android:textSize="10sp" /> </LinearLayout> </LinearLayout></RelativeLayout>
更多相关文章
- android在java中动态改变控件大小
- android 按钮按下效果2
- 判断android设备是否支持多点触控
- [置顶] 通过SIM卡获取GPS,android基站定位原理
- 一个Android实现图片的倒影效果的代码示例
- Android(安卓)沉浸式状态栏 +DrawerLayout+Toolbar,适配4.4X及以
- PC端与android手机端使用adb forword通信
- Android(安卓)O 设备不允许安装第三方APP
- android 用代码编写linearlayout布局