Java Code Examples for android.databinding.ViewDataBinding#setVariable()

The following examples show how to use android.databinding.ViewDataBinding#setVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListBindingAdapters.java    From android-ui-toolkit-demos with Apache License 2.0 7 votes vote down vote up
@Override
public void onItemRangeChanged(ObservableList observableList,
        int start, int count) {
    if (mLayoutId == 0) {
        return;
    }
    LayoutInflater inflater = (LayoutInflater) mTarget.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    startTransition(mTarget);
    final int end = start + count;
    for (int i = start; i < end; i++) {
        Object data = observableList.get(i);
        ViewDataBinding binding = bindLayout(inflater,
                mTarget, mLayoutId, data);
        binding.setVariable(BR.data, observableList.get(i));
        mTarget.removeViewAt(i);
        mTarget.addView(binding.getRoot(), i);
    }
}
 
Example 2
Source File: CharacterActivity.java    From marvel with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_character);

    // inject views using ButterKnife
    ButterKnife.bind(this);

    if (null == getIntent() || null == getIntent().getExtras() || null == getIntent().getExtras().getSerializable(ARG_CHARACTER)) {
        finish();
        return;
    }

    // get args
    CharacterModel character = (CharacterModel) getIntent().getExtras().getSerializable(ARG_CHARACTER);

    // bind value using Android Binding
    binding.setVariable(BR.character, character);

    setupToolbar(character.getName());

    Timber.d("Character Activity Created");
}
 
Example 3
Source File: WeatherDailyAdapter.java    From MVVMArms with Apache License 2.0 6 votes vote down vote up
@Override
protected void convert(BaseBindHolder helper, WeatherDailyResponse.DailyResult.Daily item) {
    ViewDataBinding binding = helper.getBinding();
    binding.setVariable(BR.daily, item);
    binding.executePendingBindings();

    SuperTextView superTextView = helper.getView(R.id.super_item_daily);
    ArmsUtils.INSTANCE.obtainArmsComponent(mContext).imageLoader()
            .loadImage(mContext,
                    ImageConfigImpl.builder()
                            .url(String.format(Locale.CHINESE, Api.API_WEATHER_ICON_URL, item.getCodeDay()))
                            .placeholder(R.mipmap.ic_placeholder)
                            .errorPic(R.mipmap.weather_unknown)
                            .transformation(new FitCenter())
                            .imageView(superTextView.getRightIconIV())
                            .build());
}
 
Example 4
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 6 votes vote down vote up
@Override
public void bind(@NonNull final Object data, final int index,
    @NonNull final RecyclerView.ViewHolder holder) {
  final Object item = getItems(data).get(index);
  final View view = holder.itemView;
  final ViewDataBinding viewDataBinding = DataBindingUtil.bind(view);
  final Integer itemVariable = itemId.apply(item);
  if (itemVariable != BR_NO_ID) {
    viewDataBinding.setVariable(itemVariable, item);
    view.setTag(R.id.agera__rvdatabinding__item_id, itemVariable);
  }
  if (collectionId != BR_NO_ID) {
    viewDataBinding.setVariable(collectionId, data);
    view.setTag(R.id.agera__rvdatabinding__collection_id, collectionId);
  }
  for (int i = 0; i < handlers.size(); i++) {
    final int variableId = handlers.keyAt(i);
    viewDataBinding.setVariable(variableId, handlers.valueAt(i));
  }
  viewDataBinding.executePendingBindings();
}
 
Example 5
Source File: BaseExpandableAdapter.java    From ExpandableRecyclerview-Databinding with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(BindingViewHolder holder, int position) {
    Object o = mDataList.get(position);
    ViewDataBinding binding = holder.getBinding();
    binding.setVariable(getVariable(o, position), o);
    binding.executePendingBindings();
    if (o instanceof BaseExpandableObservable) {
        BaseExpandableObservable baseExpandableObservable = (BaseExpandableObservable) o;
        ObservableArrayList<Object> childList = baseExpandableObservable.getChildList();
        if (childList != null && !childList.isEmpty())
            baseExpandableObservable.setItemListExpandCollapseListener(this);
    }
}
 
Example 6
Source File: SingleBindingAdapter.java    From all-base-adapter with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(ViewGroup parent, int pos, D data) {
    ViewHolder holder = getViewHolderByType(parent, mItemLayoutId);
    ViewDataBinding binding = (ViewDataBinding) holder.itemView.getTag(R.id.zxt_tag_vdb);
    onBindViewHolder(parent, holder, data, pos);
    binding.setVariable(BR.data, data);
    binding.setVariable(BR.itemP, ItemPresenter);
    return holder.itemView;
}
 
Example 7
Source File: MyAdapter.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewDataBinding binding = null;
    if (convertView == null) {
        binding = DataBindingUtil.inflate(inflater, layoutResId, parent, false);

    }else{
        binding = DataBindingUtil.getBinding(convertView);
    }

    binding.setVariable(variableId,list.get(position));

    return binding.getRoot();
}
 
Example 8
Source File: ViewGroupBindings.java    From okuki with Apache License 2.0 5 votes vote down vote up
@BindingAdapter("component")
public static void loadComponent(ViewGroup viewGroup, MvvmComponent component) {
    if (component != null) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), component.getLayoutResId(), viewGroup, false);
        View view = binding.getRoot();
        binding.setVariable(BR.vm, component.getViewModel());
        binding.executePendingBindings();
        viewGroup.removeAllViews();
        viewGroup.addView(view);
    }
}
 
Example 9
Source File: MainActivity.java    From okuki with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    vm = new MainViewModel();
    binding.setVariable(BR.vm, vm);
    binding.executePendingBindings();
}
 
Example 10
Source File: ComplexRvAdapter.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
    final GirlInfo info = getItem(position);
    if(holder instanceof ViewHolder){
        ViewDataBinding binding = ((ViewHolder) holder).getBinding();
        binding.setVariable(BR.info, info);

        binding.executePendingBindings();
    }
}
 
Example 11
Source File: CurrencyDetailsActivity.java    From exchange-rates-mvvm with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_currency_details);
    binding.setVariable(BR.model, mViewModel);
    if (!isRetained(savedInstanceState)) {
        final String currencyCode = getIntent().getStringExtra(INTENT_EXTRA_PARAM_CURRENCY_CODE);
        Bundle bundle = new Bundle();
        bundle.putString(CurrencyDetailsViewModel.KEY_CURRENCY_CODE, currencyCode);
        mViewModel.onLoad(bundle);
    }
}
 
Example 12
Source File: RecyclerViewBA.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if (holder.hasItem) {
        ViewDataBinding binding = holder.getBinding();
        if (holder.hasItem) binding.setVariable(BR.item, itemList.get(position));
        if (handler != null) binding.setVariable(BR.handler, handler);
    }
}
 
Example 13
Source File: DataBindingLayoutPresenters.java    From agera with Apache License 2.0 5 votes vote down vote up
@Override
public void bind(@NonNull final View view) {
  final ViewDataBinding viewDataBinding = DataBindingUtil.bind(view);
  for (int i = 0; i < handlers.size(); i++) {
    final int variableId = handlers.keyAt(i);
    viewDataBinding.setVariable(variableId, handlers.get(variableId));
  }
  viewDataBinding.executePendingBindings();
}
 
Example 14
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@Override
public void recycle(@NonNull final RecyclerView.ViewHolder holder) {
  if (recycleConfig != 0) {
    final View view = holder.itemView;
    final ViewDataBinding viewDataBinding = DataBindingUtil.bind(view);
    if ((recycleConfig & CLEAR_ITEM) != 0) {
      final Object tag = view.getTag(R.id.agera__rvdatabinding__item_id);
      view.setTag(R.id.agera__rvdatabinding__item_id, null);
      if (tag instanceof Integer) {
        viewDataBinding.setVariable((int) tag, null);
      }
    }
    if ((recycleConfig & CLEAR_COLLECTION) != 0) {
      final Object collectionTag = view.getTag(R.id.agera__rvdatabinding__collection_id);
      view.setTag(R.id.agera__rvdatabinding__collection_id, null);
      if (collectionTag instanceof Integer) {
        viewDataBinding.setVariable((int) collectionTag, null);
      }
    }
    if ((recycleConfig & CLEAR_HANDLERS) != 0) {
      for (int i = 0; i < handlers.size(); i++) {
        viewDataBinding.setVariable(handlers.keyAt(i), null);
      }
    }
    viewDataBinding.executePendingBindings();
  }
}
 
Example 15
Source File: HeaderedRecyclerViewDatabindingAdapter.java    From Android-App-Architecture-MVVM-Databinding with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(final RecyclerViewDatabindingAdapter<ItemTypeT>.BindingHolder holder,
                             final int position) {
    if (position == 0) {
        final ViewDataBinding itemBinding = holder.getBinding();
        itemBinding.setVariable(headerParams.headerDataBrId, headerParams.headerData);
        itemBinding.executePendingBindings();
        return;
    }

    super.onBindViewHolder(holder, position);
}
 
Example 16
Source File: RecyclerViewDatabindingAdapter.java    From Android-App-Architecture-MVVM-Databinding with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(final RecyclerViewDatabindingAdapter<ItemTypeT>.BindingHolder holder,
        final int position) {
    final Object item = getItemForBinding(position);
    final ViewDataBinding itemBinding = holder.getBinding();
    itemBinding.setVariable(itemDataBrId, item);
    itemBinding.executePendingBindings();
}
 
Example 17
Source File: ViewAdapter.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@BindingAdapter({"itemView", "observableList"})
public static void addViews(ViewGroup viewGroup, final ItemBinding itemBinding, final ObservableList<IBindingItemViewModel> viewModelList) {
    if (viewModelList != null && !viewModelList.isEmpty()) {
        viewGroup.removeAllViews();
        for (IBindingItemViewModel viewModel : viewModelList) {
            ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(Utils.getContext()),
                    itemBinding.layoutRes(), viewGroup, true);
            binding.setVariable(itemBinding.variableId(), viewModel);
            viewModel.injecDataBinding(binding);
        }
    }
}
 
Example 18
Source File: DataBindingLayoutPresenters.java    From agera with Apache License 2.0 5 votes vote down vote up
@Override
public void recycle(@NonNull final View view) {
  if ((recycleConfig & CLEAR_HANDLERS) != 0) {
    final ViewDataBinding viewDataBinding = DataBindingUtil.bind(view);
    for (int i = 0; i < handlers.size(); i++) {
      viewDataBinding.setVariable(handlers.keyAt(i), null);
    }
    viewDataBinding.executePendingBindings();
  }
}
 
Example 19
Source File: ItemEmptyAndError.java    From MultiItem with Apache License 2.0 4 votes vote down vote up
@Override
protected void onBindViewHolder(ViewDataBinding dataBinding, ItemEmptyAndError data) {
    dataBinding.setVariable(BR.itemData, data);
}
 
Example 20
Source File: BaseBindingListAdapter.java    From GracefulMovies with Apache License 2.0 4 votes vote down vote up
protected void onBind(@NonNull BaseBindingVH holder, @NonNull ViewDataBinding binding, T item, int position, int viewType) {
    onBind(holder, binding, item, position);
    onBind(binding, item, position);

    binding.setVariable(BR.model, item);
}