Home | History | Annotate | Download | only in car
      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 
     17 package com.example.androidx.car;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.view.Menu;
     22 
     23 import androidx.appcompat.app.AppCompatActivity;
     24 import androidx.appcompat.widget.Toolbar;
     25 import androidx.car.widget.ListItem;
     26 import androidx.car.widget.ListItemAdapter;
     27 import androidx.car.widget.ListItemProvider;
     28 import androidx.car.widget.PagedListView;
     29 import androidx.car.widget.TextListItem;
     30 
     31 /**
     32  * Demo activity for creating {@code App Bar} with {@link Toolbar}.
     33  */
     34 public class AppBarActivity extends AppCompatActivity {
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.activity_app_bar);
     40         Toolbar toolbar = findViewById(R.id.car_toolbar);
     41         setSupportActionBar(toolbar);
     42         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     43         getSupportActionBar().setTitle("Title");
     44         getSupportActionBar().setSubtitle("Subtitle");
     45 
     46         setUpList();
     47     }
     48 
     49     private void setUpList() {
     50         PagedListView list = findViewById(R.id.list);
     51         list.setAdapter(new ListItemAdapter(this, new SampleProvider(this, 50)));
     52     }
     53 
     54     @Override
     55     public boolean onCreateOptionsMenu(Menu menu) {
     56         getMenuInflater().inflate(R.menu.demo_menu, menu);
     57         return super.onCreateOptionsMenu(menu);
     58     }
     59 
     60     private static class SampleProvider extends ListItemProvider {
     61 
     62         private int mCount;
     63         private Context mContext;
     64 
     65         SampleProvider(Context context, int count) {
     66             mContext = context;
     67             mCount = count;
     68         }
     69 
     70         @Override
     71         public ListItem get(int position) {
     72             if (position < 0 || position >= mCount) {
     73                 throw new IndexOutOfBoundsException();
     74             }
     75             TextListItem item = new TextListItem(mContext);
     76             item.setPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false);
     77             item.setTitle("title");
     78             return item;
     79         }
     80 
     81         @Override
     82         public int size() {
     83             return mCount;
     84         }
     85     }
     86 }
     87