我正在学习Android开发,我打算在ListView中显示任何连接/配对的蓝牙设备。每个列表项左侧都有一个设备名,右侧有一个开关。 屏幕截图有点大
我有关于如何在单独的bluetooth_device_entry.xml
文件中显示每个项目的设计。
bluetooth_device_entry.xml
文件与RelativeLayout一起布局,它有两个孩子TextView
和Switch
在主布局XML文件activity_connected_devices.xml
中,我有一个ListView,我用bluetooth_device_entry.xml
文件以编程方式填充。
我的问题出在Java活动ConnectedDevicesActivity.java
中。我可以将setOnItemClickListener
附加到ListView
个项目,但即使在bluetooth_device_entry.xml
充气,setOnChangeListener()
附加到充气布局后,切换也不会被触发。
bluetoothConnectedDeviceItemClickListener
有效但switchCheckedListener
没有。
知道如何解决这个问题吗?感谢。
bluetooth_device_entry.xml
(每个ListView
项目填充的内容)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bluetoothDeviceEntryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/connectedDeviceName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_grey"
android:paddingBottom="12dp"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:paddingTop="12dp"
android:text="@string/txtPlaceHolderConnectedBluetoothText"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/dimen_listTextSizeMedium" />
<Switch
android:id="@+id/deviceStateSwitch"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBaseline="@+id/connectedDeviceName"
android:layout_alignBottom="@+id/connectedDeviceName"
android:layout_alignParentEnd="true"
android:layout_marginEnd="15dp"
android:switchMinWidth="@dimen/dimen_switchWidth" />
</RelativeLayout>
activity_connected_devices.xml
(最初由ConnectedDevicesActivity.java
onCreate()
加载的活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activityConnectedDevicesLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtTitleConnectedDevices"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/dark_blue"
android:gravity="center"
android:text="@string/txtConnectedDevices"
android:textColor="@color/light_blue"
android:textSize="@dimen/dimen_buttonTextSize" />
<ListView
android:id="@+id/bluetoothConnectedDevicesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
</LinearLayout>
ConnectedDevicesActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
public class ConnectedDevicesActivity extends AppCompatActivity
{
ListView bluetoothConnectedDevicesListView;
Switch deviceSwitch;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connected_devices);
setTitle("Connected Devices");
bluetoothConnectedDevicesListView = findViewById(R.id.bluetoothConnectedDevicesListView);
showConnectedBluetoothDevices();
}
public void showConnectedBluetoothDevices()
{
String[] connectedBluetoothDevices = new String[10];
for(int i=0; i<connectedBluetoothDevices.length; i++)
{
connectedBluetoothDevices[i] = "Device " + (i+1);
}
ArrayAdapter<String> connectedBluetoothDevicesAdapter =
new ArrayAdapter<String>(this,
R.layout.bluetooth_connected_devices_entry,
R.id.connectedDeviceName,
connectedBluetoothDevices);
bluetoothConnectedDevicesListView.setAdapter(connectedBluetoothDevicesAdapter);
bluetoothConnectedDevicesListView.setOnItemClickListener(new bluetoothConnectedDeviceItemClickListener());
View inflatedView = getLayoutInflater().inflate(R.layout.bluetooth_connected_devices_entry, null);
deviceSwitch = inflatedView.findViewById(R.id.deviceStateSwitch);
/* This is not responding to switch loaded from inflated view */
deviceSwitch.setOnCheckedChangeListener(new switchCheckedListener());
Toast.makeText(
ConnectedDevicesActivity.this,
String.format("Switch checked? %b", deviceSwitch.isChecked()),
Toast.LENGTH_SHORT).show();
}
public class bluetoothConnectedDeviceItemClickListener implements OnItemClickListener
{
/* When the user taps an item in the list view */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(
ConnectedDevicesActivity.this,
String.format("You tapped %s", bluetoothConnectedDevicesListView.getItemAtPosition(position)),
Toast.LENGTH_SHORT).show();
}
}
/* When the user toggles a switch */
public class switchCheckedListener implements CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Toast.makeText(
ConnectedDevicesActivity.this,
String.format("Checked state is %s", isChecked),
Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:3)
您正在夸大与ListView
项目无关的额外视图
bluetoothConnectedDevicesListView.setAdapter(connectedBluetoothDevicesAdapter);
bluetoothConnectedDevicesListView.setOnItemClickListener(new bluetoothConnectedDeviceItemClickListener());
View inflatedView = getLayoutInflater().inflate(R.layout.bluetooth_connected_devices_entry, null);
// ^^^^^^^^ totally a new item, has no relation with views inside list
deviceSwitch = inflatedView.findViewById(R.id.deviceStateSwitch);
deviceSwitch.setOnCheckedChangeListener(new switchCheckedListener());
解决方案:You need to create a customize Adapter
,它将负责提供每个rowitem视图,而getView
内部可以对子视图应用操作(附加侦听器/操作值)你有点观点。
注意:您还需要管理列表以跟踪切换视图状态,因为适配器会重新创建重用视图。