Issue
I have a strange effect with my RecyclerView after I updated my App to API 28. I have a search button that triggers a search of Wifi devices in my area. The items are no longer displayed directly in the list. I have to close and reopen the app to get the correct display.
I have a toast that should be displayed when the button is pressed. The toast message is also only displayed after closing and opening the app.
Activity.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);
});
}
@Override
public int getItemCount() {
return wifis.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView wifiName;
CardView cardView;
ViewHolder(View itemView) {
super(itemView);
wifiName = itemView.findViewById(R.id.text_wifi_name);
cardView = itemView.findViewById(R.id.cardView_device);
}
}
}
UcDiscoverDevicesImpl.java
private void createRecyclerView(List<DeviceWifiTo> list) {
RecyclerView recyclerView = activity.findViewById(R.id.recycler_view_wifi);
WifiAdapter adapter = new WifiAdapter(list);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(activity.getApplicationContext()));
}
Solution
Set layout manager
before set adapter
to recycler view
, Update like below
private void createRecyclerView(List<DeviceWifiTo> list) {
RecyclerView recyclerView = activity.findViewById(R.id.recycler_view_wifi);
recyclerView.setLayoutManager(new LinearLayoutManager(activity.getApplicationContext()));
WifiAdapter adapter = new WifiAdapter(list);
recyclerView.setAdapter(adapter);
}
Hope it helps you.
Answered By - Jakir Hossain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.