1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package androidx.recyclerview.widget;
17
18
19import android.view.View;
20
21import androidx.annotation.NonNull;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Simple class that can collect list of view attach and detach events so that we can assert on them
28 */
29public class AttachDetachCollector implements RecyclerView.OnChildAttachStateChangeListener {
30    private final List<View> mAttached = new ArrayList<>();
31    private final List<View> mDetached = new ArrayList<>();
32
33    public AttachDetachCollector(RecyclerView recyclerView) {
34        recyclerView.addOnChildAttachStateChangeListener(this);
35    }
36
37    @Override
38    public void onChildViewAttachedToWindow(@NonNull View view) {
39        mAttached.add(view);
40    }
41
42    @Override
43    public void onChildViewDetachedFromWindow(@NonNull View view) {
44        mDetached.add(view);
45    }
46
47    public void reset() {
48        mAttached.clear();
49        mDetached.clear();
50    }
51
52    public List<View> getAttached() {
53        return mAttached;
54    }
55
56    public List<View> getDetached() {
57        return mDetached;
58    }
59}
60