Home | History | Annotate | Download | only in adapters
      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 android.databinding.adapters;
     17 
     18 import android.databinding.BindingAdapter;
     19 import android.databinding.InverseBindingAdapter;
     20 import android.databinding.InverseBindingListener;
     21 import android.widget.TabHost;
     22 import android.widget.TabHost.OnTabChangeListener;
     23 
     24 public class TabHostBindingAdapter {
     25 
     26     @InverseBindingAdapter(attribute = "android:currentTab")
     27     public static int getCurrentTab(TabHost view) {
     28         return view.getCurrentTab();
     29     }
     30 
     31     @InverseBindingAdapter(attribute = "android:currentTab")
     32     public static String getCurrentTabTag(TabHost view) {
     33         return view.getCurrentTabTag();
     34     }
     35 
     36     @BindingAdapter("android:currentTab")
     37     public static void setCurrentTab(TabHost view, int tab) {
     38         if (view.getCurrentTab() != tab) {
     39             view.setCurrentTab(tab);
     40         }
     41     }
     42 
     43     @BindingAdapter("android:currentTab")
     44     public static void setCurrentTabTag(TabHost view, String tabTag) {
     45         if (view.getCurrentTabTag() != tabTag) {
     46             view.setCurrentTabByTag(tabTag);
     47         }
     48     }
     49 
     50     @BindingAdapter(value = {"android:onTabChanged", "android:currentTabAttrChanged"},
     51             requireAll = false)
     52     public static void setListeners(TabHost view, final OnTabChangeListener listener,
     53             final InverseBindingListener attrChange) {
     54         if (attrChange == null) {
     55             view.setOnTabChangedListener(listener);
     56         } else {
     57             view.setOnTabChangedListener(new OnTabChangeListener() {
     58                 @Override
     59                 public void onTabChanged(String tabId) {
     60                     if (listener != null) {
     61                         listener.onTabChanged(tabId);
     62                     }
     63                     attrChange.onChange();
     64                 }
     65             });
     66         }
     67     }
     68 }
     69