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.content.Context;
     19 import android.databinding.ObservableList;
     20 import android.databinding.ObservableList.OnListChangedCallback;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.BaseAdapter;
     25 import android.widget.TextView;
     26 
     27 import java.util.List;
     28 
     29 class ObservableListAdapter<T> extends BaseAdapter {
     30     private List<T> mList;
     31     private ObservableList.OnListChangedCallback mListChangedCallback;
     32     private final Context mContext;
     33     private final int mDropDownResourceId;
     34     private final int mResourceId;
     35     private final int mTextViewResourceId;
     36     private final LayoutInflater mLayoutInflater;
     37 
     38     public ObservableListAdapter(Context context, List<T> list, int resourceId,
     39             int dropDownResourceId, int textViewResourceId) {
     40         mContext = context;
     41         mResourceId = resourceId;
     42         mDropDownResourceId = dropDownResourceId;
     43         mTextViewResourceId = textViewResourceId;
     44         mLayoutInflater = (resourceId == 0) ? null :
     45                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     46         setList(list);
     47     }
     48 
     49     public void setList(List<T> list) {
     50         if (mList == list) {
     51             return;
     52         }
     53         if (mList instanceof ObservableList) {
     54             ((ObservableList) mList).removeOnListChangedCallback(mListChangedCallback);
     55         }
     56         mList = list;
     57         if (mList instanceof ObservableList) {
     58             if (mListChangedCallback == null) {
     59                 mListChangedCallback = new OnListChangedCallback() {
     60                     @Override
     61                     public void onChanged(ObservableList observableList) {
     62                         notifyDataSetChanged();
     63                     }
     64 
     65                     @Override
     66                     public void onItemRangeChanged(ObservableList observableList, int i,
     67                             int i1) {
     68                         notifyDataSetChanged();
     69                     }
     70 
     71                     @Override
     72                     public void onItemRangeInserted(ObservableList observableList, int i,
     73                             int i1) {
     74                         notifyDataSetChanged();
     75                     }
     76 
     77                     @Override
     78                     public void onItemRangeMoved(ObservableList observableList, int i, int i1,
     79                             int i2) {
     80                         notifyDataSetChanged();
     81                     }
     82 
     83                     @Override
     84                     public void onItemRangeRemoved(ObservableList observableList, int i,
     85                             int i1) {
     86                         notifyDataSetChanged();
     87                     }
     88                 };
     89             }
     90             ((ObservableList) mList).addOnListChangedCallback(mListChangedCallback);
     91         }
     92         notifyDataSetChanged();
     93     }
     94 
     95     @Override
     96     public int getCount() {
     97         return mList.size();
     98     }
     99 
    100     @Override
    101     public Object getItem(int position) {
    102         return mList.get(position);
    103     }
    104 
    105     @Override
    106     public long getItemId(int position) {
    107         return position;
    108     }
    109 
    110     @Override
    111     public View getView(int position, View convertView, ViewGroup parent) {
    112         return getViewForResource(mResourceId, position, convertView, parent);
    113     }
    114 
    115     @Override
    116     public View getDropDownView(int position, View convertView, ViewGroup parent) {
    117         return getViewForResource(mDropDownResourceId, position, convertView, parent);
    118     }
    119 
    120     public View getViewForResource(int resourceId, int position, View convertView,
    121             ViewGroup parent) {
    122         if (convertView == null) {
    123             if (resourceId == 0) {
    124                 convertView = new TextView(mContext);
    125             } else {
    126                 convertView = mLayoutInflater.inflate(resourceId, parent, false);
    127             }
    128         }
    129         TextView text = (TextView) (mTextViewResourceId == 0 ? convertView :
    130                 convertView.findViewById(mTextViewResourceId));
    131         T item = mList.get(position);
    132         CharSequence value;
    133         if (item instanceof CharSequence) {
    134             value = (CharSequence) item;
    135         } else {
    136             value = String.valueOf(item);
    137         }
    138         text.setText(value);
    139         return convertView;
    140     }
    141 }
    142