Group layers

View inJavaKotlinView on GitHubSample viewer app

Group a collection of layers together and toggle their visibility as a group.

Image of group layers

Use case

Group layers communicate to the user that layers are related and can be managed together.

In a land development project, you might group layers according to the phase of development.

How to use the sample

The layers in the map will be displayed in a table of contents. Toggle the checkbox next to a layer's name to change its visibility. Turning a group layer's visibility off will override the visibility of its child layers.

How it works

  1. Create an empty GroupLayer.
  2. Add a child layer to the group layer's layers collection.
  3. Set the group layer's GroupVisibilityMode to change its behavior:
  • GroupVisibilityMode.INDEPENDENT allows each sublayer to change its visibility independently.
  • GroupVisibilityMode.EXCLUSIVE allows only one sublayer to be visible at a time.
  • GroupVisibilityMode.INHERITED treats the group layer as if it is one merged layer.
  1. To toggle the visibility of the group, simply change the group layer's visibility property.

Relevant API

  • GroupLayer

Additional information

The full extent of a group layer may change when child layers are added/removed. Group layers do not have a spatial reference, but the full extent will have the spatial reference of the first child layer.

Group layers can be saved to web scenes. In web maps, group layers will be ignored.

Tags

group layer, layers

Sample Code

LayerListAdapter.ktLayerListAdapter.ktMainActivity.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright 2020 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.esri.arcgisruntime.sample.grouplayers

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.esri.arcgisruntime.layers.GroupLayer
import com.esri.arcgisruntime.layers.GroupVisibilityMode
import com.esri.arcgisruntime.layers.Layer
import com.esri.arcgisruntime.mapping.LayerList

/**
 * A custom RecyclerView.Adapter to display group layers and sublayers, accounting for group layers
 * with an exclusive visibility mode.
 *
 * @param dataSet the list of layers for the scene
 * @param onLayerCheckedChanged a callback function which is invoked by each layer and sublayer's onCheckedChangedListener
 */
class LayerListAdapter(
    private val dataSet: LayerList,
    private val onLayerCheckedChanged: (layer: Layer, isChecked: Boolean) -> Unit
) :
    RecyclerView.Adapter<LayerListAdapter.ViewHolder>() {

    private val TYPE_DEFAULT = 0
    private val TYPE_EXCLUSIVE = 1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // There are two view holder types, for independent and exclusive visibility modes
        return when (viewType) {
            TYPE_EXCLUSIVE -> {
                val v = LayoutInflater.from(parent.context).inflate(
                    R.layout.group_layers_radio_group, parent, false
                )
                ExclusiveLayerViewHolder(v)
            }
            else -> {
                val v = LayoutInflater.from(parent.context).inflate(
                    R.layout.group_layers_checkbox_group, parent, false
                )
                DefaultLayerViewHolder(v)
            }
        }
    }


    override fun getItemViewType(position: Int): Int {
        val layer = dataSet[position]
        return if (layer is GroupLayer && layer.visibilityMode == GroupVisibilityMode.EXCLUSIVE) {
            TYPE_EXCLUSIVE
        } else TYPE_DEFAULT
    }

    override fun getItemCount(): Int = dataSet.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (holder.itemViewType) {
            TYPE_EXCLUSIVE -> (holder as ExclusiveLayerViewHolder).let { exclusiveLayerViewHolder ->
                val layer = dataSet[position]
                exclusiveLayerViewHolder.apply {
                    this.layer = layer
                    textView.text = layer.name
                    onLayerChecked = onLayerCheckedChanged
                    sublayers.apply {
                        clear()
                        addAll((layer as GroupLayer).layers)
                    }
                    populate()
                }
            }
            else -> (holder as DefaultLayerViewHolder).let { defaultLayerViewHolder ->
                val layer = dataSet[position]
                defaultLayerViewHolder.apply {
                    this.layer = layer
                    textView.text = layer.name
                    onLayerChecked = onLayerCheckedChanged
                    sublayers.apply {
                        clear()
                        addAll((layer as GroupLayer).layers)
                    }
                    populate()
                }
            }
        }
    }


    abstract class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        abstract val textView: TextView
    }

    class DefaultLayerViewHolder(itemView: View) : ViewHolder(itemView) {
        override val textView: TextView = itemView.findViewById(
            R.id.checkbox_grouplayer_name
        )
        private val sublayerLayout: LinearLayout = itemView.findViewById(
            R.id.sublayer_layout
        )
        val sublayers = mutableListOf<Layer>()
        var layer: Layer? = null

        var onLayerChecked: ((Layer, Boolean) -> Unit)? = null

        /**
         * Sets the OnCheckedChangeListener of the top-level layer
         * and creates checkboxes for each of the layer's sublayers with a label and OnCheckedChangeListener.
         */
        fun populate() {
            val checkBox: CheckBox = itemView.findViewById(R.id.layer_checkbox)
            layer?.let { layer ->
                checkBox.setOnCheckedChangeListener { _, isChecked ->
                    onLayerChecked?.invoke(layer, isChecked)
                }
                checkBox.isChecked = layer.isVisible
            }
            sublayers.forEach { sublayer ->
                CheckBox(itemView.context).apply {
                    id = View.generateViewId()
                    text = sublayer.name
                    sublayerLayout.addView(this)
                    setOnCheckedChangeListener { _, isChecked ->
                        onLayerChecked?.invoke(
                            sublayer,
                            isChecked
                        )
                    }
                    isChecked = sublayer.isVisible
                }
            }
        }
    }

    class ExclusiveLayerViewHolder(itemView: View) : ViewHolder(itemView) {
        override val textView: TextView = itemView.findViewById(
            R.id.radio_group_layer_name
        )
        private val radioGroup: RadioGroup = itemView.findViewById(
            R.id.radioGroup
        )
        val sublayers = mutableListOf<Layer>()
        var layer: Layer? = null

        var onLayerChecked: ((Layer, Boolean) -> Unit)? = null

        /**
         * Sets the OnCheckedChangeListener of the top-level layer
         * and creates radio buttons for each of the layer's sublayers with a label and OnCheckedChangeListener.
         */
        fun populate() {
            val checkBox: CheckBox = itemView.findViewById(R.id.layer_checkbox)
            layer?.let { layer ->
                checkBox.setOnCheckedChangeListener { _, isChecked ->
                    onLayerChecked?.invoke(layer, isChecked)
                }
                checkBox.isChecked = layer.isVisible
            }
            sublayers.forEach { sublayer ->
                RadioButton(itemView.context).apply {
                    id = View.generateViewId()
                    text = sublayer.name
                    radioGroup.addView(this)
                    setOnCheckedChangeListener { _, isChecked ->
                        onLayerChecked?.invoke(
                            sublayer,
                            isChecked
                        )
                    }
                    isChecked = sublayer.isVisible
                }
            }
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.