Skip to content

Instantly share code, notes, and snippets.

@wongcain
Created February 13, 2018 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wongcain/d8747c627163b09ace24d41fd88e6bcf to your computer and use it in GitHub Desktop.
Save wongcain/d8747c627163b09ace24d41fd88e6bcf to your computer and use it in GitHub Desktop.
Navigation Data Bindings
package com.example.cain.mvvmexample;
import android.content.Intent;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.support.annotation.Nullable;
import android.view.View;
public class ViewBindings {
@BindingAdapter(value = {"navRequest", "navRequestAttrChanged"}, requireAll = false)
public static void setNavRequest(View view, @Nullable NavigationRequest navRequest, @Nullable InverseBindingListener inverseBindingListener) {
if (navRequest != null) {
Intent intent = navRequest.getIntent(view.getContext());
view.getContext().startActivity(intent);
if (inverseBindingListener != null) {
inverseBindingListener.onChange();
}
}
}
@InverseBindingAdapter(attribute = "navRequest")
public static NavigationRequest getNavRequest(View view) {
return null; // clear nav request
}
}
package com.example.cain.mvvmexample;
import android.content.Context;
import android.content.Intent;
public interface NavigationRequest {
Intent getIntent(Context context);
}
package com.example.cain.mvvmexample;
import android.content.Context;
import android.content.Intent;
public class OtherScreenRequest implements NavigationRequest {
public final String EXTRA_ID = "id";
private final int id;
public OtherScreenRequest(int id) {
this.id = id;
}
@Override
public Intent getIntent(Context context) {
Intent intent = new Intent(context, OtherScreenActivity.class);
intent.putExtra(EXTRA_ID, id);
return intent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment