From d166fcda24fdd5dcb2bcb9bb5500b751dfb9be0e Mon Sep 17 00:00:00 2001 From: euxane Date: Wed, 18 Sep 2024 23:41:13 +0200 Subject: activities/lists: workaround for now mishandled emptyView for ListView --- .../activities/start/NetworkListFragment.kt | 13 +++++------- .../activities/status/nodes/NodeListFragment.kt | 14 +++++++++---- .../tincapp/extensions/ListViewExtensions.kt | 23 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt index 9a25a0c..7b5006a 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListFragment.kt @@ -31,6 +31,7 @@ import org.pacien.tincapp.activities.BaseFragment import org.pacien.tincapp.databinding.StartNetworkListBinding import org.pacien.tincapp.extensions.hideBottomSeparator import org.pacien.tincapp.extensions.setElements +import org.pacien.tincapp.extensions.updatePlaceholderVisibility /** * @author euxane @@ -55,7 +56,6 @@ class NetworkListFragment : BaseFragment() { val listHeaderView = layoutInflater.inflate(R.layout.start_network_list_header, startNetworkListBinding.root, false) startNetworkListBinding.startNetworkList.addHeaderView(listHeaderView, null, false) startNetworkListBinding.startNetworkList.hideBottomSeparator() - startNetworkListBinding.startNetworkList.emptyView = startNetworkListBinding.startNetworkListPlaceholder startNetworkListBinding.startNetworkList.onItemClickListener = AdapterView.OnItemClickListener(this::onItemClick) startNetworkListBinding.startNetworkList.adapter = networkListAdapter } @@ -67,13 +67,10 @@ class NetworkListFragment : BaseFragment() { } private fun updateNetworkList(networks: List) { + startNetworkListBinding.startNetworkList.updatePlaceholderVisibility( + startNetworkListBinding.startNetworkListPlaceholder, + networks.isEmpty() + ) networkListAdapter.setElements(networks) - if (networks.isEmpty()) updatePlaceholder() - } - - private fun updatePlaceholder() { - startNetworkListBinding.startNetworkListPlaceholder.post { - startNetworkListBinding.startNetworkListPlaceholderText?.text = getString(R.string.start_network_list_empty_none_found) - } } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt index 05c65d7..474ab6e 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListFragment.kt @@ -18,7 +18,6 @@ package org.pacien.tincapp.activities.status.nodes -import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import android.os.Bundle import androidx.appcompat.app.AlertDialog @@ -33,6 +32,7 @@ import org.pacien.tincapp.databinding.StatusNodeListFragmentBinding import org.pacien.tincapp.extensions.hideBottomSeparator import org.pacien.tincapp.extensions.hideTopSeparator import org.pacien.tincapp.extensions.setElements +import org.pacien.tincapp.extensions.updatePlaceholderVisibility import org.pacien.tincapp.service.TincVpnService /** @@ -44,12 +44,11 @@ class NodeListFragment : BaseFragment() { private val netName by lazy { vpnService.getCurrentNetName()!! } private val nodeListViewModel by lazy { ViewModelProviders.of(this).get(NodeListViewModel::class.java) } private val nodeListAdapter by lazy { NodeInfoArrayAdapter(requireContext(), this::onItemClick) } - private val nodeListObserver by lazy { Observer> { nodeListAdapter.setElements(it) } } private lateinit var statusNodeListFragmentBinding: StatusNodeListFragmentBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - nodeListViewModel.nodeList.observe(this, nodeListObserver) + nodeListViewModel.nodeList.observe(this) { updateNodeList(it) } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { @@ -60,10 +59,17 @@ class NodeListFragment : BaseFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { statusNodeListFragmentBinding.statusNodeList.hideTopSeparator() statusNodeListFragmentBinding.statusNodeList.hideBottomSeparator() - statusNodeListFragmentBinding.statusNodeList.emptyView = statusNodeListFragmentBinding.statusNodeListPlaceholder statusNodeListFragmentBinding.statusNodeList.adapter = nodeListAdapter } + private fun updateNodeList(nodes: List) { + statusNodeListFragmentBinding.statusNodeList.updatePlaceholderVisibility( + statusNodeListFragmentBinding.statusNodeListPlaceholder, + nodes.isEmpty() + ) + nodeListAdapter.setElements(nodes) + } + private fun onItemClick(nodeInfo: NodeInfo) = showNodeInfo(nodeInfo.name) diff --git a/app/src/main/java/org/pacien/tincapp/extensions/ListViewExtensions.kt b/app/src/main/java/org/pacien/tincapp/extensions/ListViewExtensions.kt index 9501a8d..8a52484 100644 --- a/app/src/main/java/org/pacien/tincapp/extensions/ListViewExtensions.kt +++ b/app/src/main/java/org/pacien/tincapp/extensions/ListViewExtensions.kt @@ -45,3 +45,26 @@ fun ListView.hideTopSeparator() { fun ListView.hideBottomSeparator() { addFooterView(View(context), null, false) } + +/** + * Workaround for mishandled list emptyView since Android SDK 34: + +java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams + at android.widget.ListView.removeUnusedFixedViews(ListView.java:2001) + at android.widget.ListView.layoutChildren(ListView.java:1860) + at android.widget.AbsListView.onLayout(AbsListView.java:2271) + at android.widget.AdapterView.updateEmptyStatus(AdapterView.java:794) + at android.widget.AdapterView.checkFocus(AdapterView.java:767) + at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:859) + at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6949) + at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:38) + at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:54) + at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:355) + at org.pacien.tincapp.extensions.ListViewExtensionsKt.setElements(ListViewExtensions.kt:36) + */ +fun ListView.updatePlaceholderVisibility(placeholderView: View, usePlaceholder: Boolean) { + post { + placeholderView.visibility = if (usePlaceholder) View.VISIBLE else View.GONE + visibility = if (usePlaceholder) View.GONE else View.VISIBLE + } +} \ No newline at end of file -- cgit v1.2.3