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 */
16
17package com.example.androidx.car;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.os.Handler;
23
24import androidx.car.widget.ListItem;
25import androidx.car.widget.ListItemAdapter;
26import androidx.car.widget.ListItemProvider;
27import androidx.car.widget.PagedListView;
28import androidx.car.widget.TextListItem;
29
30/**
31 * A demo activity that will display a {@link PagedListView} that has less items than can fit
32 * on the screen and will vertically center these. This activity will continually add more
33 * items as time goes on.
34 */
35public class VerticallyCenteredListDemo extends Activity {
36    private static final int ADD_ITEM_DELAY_MS = 1000;
37    private static final int MAX_NUM_OF_ITEMS_TO_ADD = 10;
38
39    private int mNumOfItemsAdded;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44        setContentView(R.layout.vertically_centered_paged_list_view);
45
46        PagedListView list = findViewById(R.id.paged_list_view);
47
48        CustomListProvider provider = new CustomListProvider(/* context= */ this);
49        ListItemAdapter adapter = new ListItemAdapter(/* context= */ this, provider);
50
51        list.setAdapter(adapter);
52
53        Handler handler = new Handler();
54
55        // Continually add items to the list until MAX_NUM_OF_ITEMS_TO_ADD is reached.
56        handler.postDelayed(new Runnable() {
57            @Override
58            public void run() {
59                if (++mNumOfItemsAdded >= MAX_NUM_OF_ITEMS_TO_ADD) {
60                    return;
61                }
62
63                provider.addListItem();
64                adapter.notifyDataSetChanged();
65                handler.postDelayed(this, ADD_ITEM_DELAY_MS);
66            }
67        }, ADD_ITEM_DELAY_MS);
68    }
69
70    /**
71     * A custom {@link ListItemProvider} that allows additional {@link ListItem}s to be added to it.
72     */
73    private static class CustomListProvider extends ListItemProvider {
74        private static final int INITIAL_NUM_OF_ITEMS = 3;
75
76        private int mNumOfItems = INITIAL_NUM_OF_ITEMS;
77        private final Context mContext;
78
79        CustomListProvider(Context context) {
80            mContext = context;
81        }
82
83        public void addListItem() {
84            mNumOfItems++;
85        }
86
87        public ListItem get(int position) {
88            TextListItem item = new TextListItem(mContext);
89            item.setTitle("Item " + position);
90            return item;
91        }
92
93        public int size() {
94            return mNumOfItems;
95        }
96
97    }
98}
99