将应用程序更新到 API 28 后,我的 RecyclerView 出现了奇怪的效果。我有一个搜索按钮,可以触发对我所在区域的 Wifi 设备的搜索。这些项目不再直接显示在列表中。我必须关闭并重新打开应用程序才能获得正确的显示。
我有一个在按下按钮时应该显示的吐司。Toast 消息也仅在关闭和打开应用程序后显示。
活动.kt
private fun setFloatingSearchButton() {
floating_button_discover_devices.setOnClickListener { view ->
Snackbar.make(view, "Search for new devices ...",
Snackbar.LENGTH_SHORT).setAction("Action", null).show()
discoverDevices()
registerReceiver(mWifiReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
mWifiManager!!.startScan()
}
}
WifiAdapter.java
public class WifiAdapter extends RecyclerView.Adapter<WifiAdapter.ViewHolder> {
private final int CONFIGURE_DEVICE_REQUEST_CODE = 0x00000001;
private static final String TAG = "WifiAdapter";
private List<DeviceWifiTo> wifis;
private Context context;
private Activity activity;
public WifiAdapter(List<DeviceWifiTo> wifis) {
this.wifis = wifis;
}
@Override
public WifiAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
activity = (Activity) context;
LayoutInflater inflater = LayoutInflater.from(context);
View wifiView = inflater.inflate(R.layout.item_wifi, parent, false);
return new WifiAdapter.ViewHolder(wifiView);
}
@Override
public void onBindViewHolder(WifiAdapter.ViewHolder holder, int position) {
DeviceWifiTo wifi = wifis.get(position);
Log.d(TAG, wifi.getSsid());
TextView wifiName = holder.wifiName;
wifiName.setText(wifi.getSsid());
CardView cardView = holder.cardView;
cardView.setOnClickListener(view -> {
Intent intent = new Intent(context, ConfigureDeviceActivity.class);
intent.putExtra("ssid", wifi.getSsid());
activity.startActivityForResult(intent, CONFIGURE_DEVICE_REQUEST_CODE);
});
}
慕仙森
qq_遁去的一_1
大话西游666
喵喔喔
相关分类