• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.android.gridviewpager;
18 
19 import android.app.Activity;
20 import android.content.res.Resources;
21 import android.os.Bundle;
22 import android.support.wearable.view.GridViewPager;
23 import android.view.View;
24 import android.view.View.OnApplyWindowInsetsListener;
25 import android.view.WindowInsets;
26 
27 public class MainActivity extends Activity {
28 
29     @Override
onCreate(Bundle savedInstanceState)30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_main);
33         final Resources res = getResources();
34         final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);
35         pager.setOnApplyWindowInsetsListener(new OnApplyWindowInsetsListener() {
36             @Override
37             public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
38                 // Adjust page margins:
39                 //   A little extra horizontal spacing between pages looks a bit
40                 //   less crowded on a round display.
41                 final boolean round = insets.isRound();
42                 int rowMargin = res.getDimensionPixelOffset(R.dimen.page_row_margin);
43                 int colMargin = res.getDimensionPixelOffset(round ?
44                         R.dimen.page_column_margin_round : R.dimen.page_column_margin);
45                 pager.setPageMargins(rowMargin, colMargin);
46 
47                 // GridViewPager relies on insets to properly handle
48                 // layout for round displays. They must be explicitly
49                 // applied since this listener has taken them over.
50                 pager.onApplyWindowInsets(insets);
51                 return insets;
52             }
53         });
54         pager.setAdapter(new SampleGridPagerAdapter(this, getFragmentManager()));
55     }
56 }
57