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