1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui;
17 
18 import android.content.Context;
19 import android.support.v4.view.PagerAdapter;
20 import android.support.v4.view.ViewPager;
21 import android.support.v4.view.ViewPager.OnPageChangeListener;
22 import android.util.AttributeSet;
23 import android.util.TypedValue;
24 import android.view.LayoutInflater;
25 import android.widget.LinearLayout;
26 
27 import com.android.messaging.R;
28 import com.android.messaging.util.Assert;
29 
30 /**
31  * A view that contains both a view pager and a tab strip wrapped in a linear layout.
32  */
33 public class CustomHeaderViewPager extends LinearLayout {
34     public final static int DEFAULT_TAB_STRIP_SIZE = -1;
35     private final int mDefaultTabStripSize;
36     private ViewPager mViewPager;
37     private ViewPagerTabs mTabstrip;
38 
CustomHeaderViewPager(final Context context, final AttributeSet attrs)39     public CustomHeaderViewPager(final Context context, final AttributeSet attrs) {
40         super(context, attrs);
41 
42         final LayoutInflater inflater = LayoutInflater.from(context);
43         inflater.inflate(R.layout.custom_header_view_pager, this, true);
44         setOrientation(LinearLayout.VERTICAL);
45 
46         mTabstrip = (ViewPagerTabs) findViewById(R.id.tab_strip);
47         mViewPager = (ViewPager) findViewById(R.id.pager);
48 
49         TypedValue tv = new TypedValue();
50         context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
51         mDefaultTabStripSize = context.getResources().getDimensionPixelSize(tv.resourceId);
52     }
53 
setCurrentItem(final int position)54     public void setCurrentItem(final int position) {
55         mViewPager.setCurrentItem(position);
56     }
57 
setViewPagerTabHeight(final int tabHeight)58     public void setViewPagerTabHeight(final int tabHeight) {
59         mTabstrip.getLayoutParams().height = tabHeight == DEFAULT_TAB_STRIP_SIZE ?
60                 mDefaultTabStripSize : tabHeight;
61     }
62 
setViewHolders(final CustomHeaderPagerViewHolder[] viewHolders)63     public void setViewHolders(final CustomHeaderPagerViewHolder[] viewHolders) {
64         Assert.notNull(mViewPager);
65         final PagerAdapter adapter = new CustomHeaderViewPagerAdapter(viewHolders);
66         mViewPager.setAdapter(adapter);
67         mTabstrip.setViewPager(mViewPager);
68         mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
69 
70             @Override
71             public void onPageScrollStateChanged(int state) {
72                 mTabstrip.onPageScrollStateChanged(state);
73             }
74 
75             @Override
76             public void onPageScrolled(int position, float positionOffset,
77                     int positionOffsetPixels) {
78                 mTabstrip.onPageScrolled(position, positionOffset, positionOffsetPixels);
79             }
80 
81             @Override
82             public void onPageSelected(int position) {
83                 mTabstrip.onPageSelected(position);
84             }
85         });
86     }
87 
getSelectedItemPosition()88     public int getSelectedItemPosition() {
89         return mTabstrip.getSelectedItemPosition();
90     }
91 }
92