Manage operational layers

View inJavaKotlin
View on GitHub
Sample viewer app

Add, remove, and reorder operational layers in a map.

Image of manage operational layers

Use case

Operational layers display the primary content of the map and usually provide dynamic content for the user to interact with (as opposed to basemap layers that provide context).

The order of operational layers in a map determines the visual hierarchy of layers in the view. You can bring attention to a specific layer by rendering it above other layers.

How to use the sample

When the app starts, tap the floating action button to display lists of operational layers and any removed layers. Long press and drag an active layer to change its position, or swipe it to the side to remove it from the map. Tap removed layers to add them back to the map. The map will be updated automatically.

How it works

  1. Get the operational layers LayerList from the map using map.operationalLayers.
  2. Add or remove layers using layerList.add(layer) and layerList.remove(layer) respectively. The last layer in the list will be rendered on top.

Relevant API

  • ArcGISMap
  • ArcGISMapImageLayer
  • LayerList

Additional information

You cannot add the same layer to the map multiple times or add the same layer to multiple maps. Instead, clone the layer with layer.clone() before duplicating.

Tags

add, delete, layer, map, remove

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
/*
 * 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.manageoperationallayers

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.esri.arcgisruntime.layers.Layer
import com.esri.arcgisruntime.mapping.LayerList

class LayerListAdapter(private val dataSet: LayerList) :
    RecyclerView.Adapter<LayerListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.layer_item, parent, false)
        return ViewHolder(v)
    }

    override fun getItemCount(): Int = dataSet.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = dataSet[position].name
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.layerName)
    }
}

class InactiveListAdapter(
    private val dataSet: MutableList<Layer>,
    private val onItemClick: (pos: Int) -> Unit
) : RecyclerView.Adapter<InactiveListAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v =
            LayoutInflater.from(parent.context).inflate(R.layout.inactive_layer_item, parent, false)
        return ViewHolder(v)
    }

    override fun getItemCount() = dataSet.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = dataSet[position].name
        holder.onClick = onItemClick
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.deletedLayerName)
        var onClick: ((Int) -> Unit)? = null

        init {
            itemView.setOnClickListener { onClick?.invoke(adapterPosition) }
        }
    }
}

class DragCallback(
    private val onItemMove: (oldPosition: Int, targetPosition: Int) -> Unit,
    private val onItemSwiped: (position: Int) -> Unit
) : ItemTouchHelper.Callback() {

    override fun isLongPressDragEnabled() = true
    override fun isItemViewSwipeEnabled() = true

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int = makeMovementFlags(
        ItemTouchHelper.UP or ItemTouchHelper.DOWN,
        ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
    )

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        onItemMove(viewHolder.adapterPosition, target.adapterPosition)
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        onItemSwiped(viewHolder.adapterPosition)
    }
}

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