下面列出了怎么用android.support.annotation.IdRes的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
ConfigInteracter interacter = new ConfigInteracter(this);
if (group.getId() == R.id.group_zoom) {
if (checkedId == R.id.radio_zoom_left) {
interacter.setZoomControlsPosition(false);
} else if (checkedId == R.id.radio_zoom_right) {
interacter.setZoomControlsPosition(true);
}
} else if (group.getId() == R.id.group_mode) {
if (checkedId == R.id.radio_white) {
interacter.setNightMode(1);
} else if (checkedId == R.id.radio_black) {
if (BApp.TYPE_MAP == TypeMap.TYPE_BAIDU) {
Toast.makeText(this, "夜间模式下百度地图可能需要重启应用后生效", Toast.LENGTH_LONG).show();
}
interacter.setNightMode(2);
} else {
interacter.setNightMode(0);
}
((BApp) getApplication()).setNightMode();
}
}
private void assertDeeplink(@IdRes int destId, int expectedStackSize) throws Throwable {
BaseNavigationActivity activity = launchDeepLink(R.navigation.nav_deep_link,
destId, null);
NavController navController = activity.getNavController();
assertThat(navController.getCurrentDestination().getId(), is(destId));
TestNavigator navigator = navController.getNavigatorProvider()
.getNavigator(TestNavigator.class);
assertThat(navigator.mBackStack.size(), is(expectedStackSize));
// Test that the deep link Intent was passed through even though we don't pass in any args
//noinspection ConstantConditions
Intent deepLinkIntent = navigator.mBackStack.peekLast().second
.getParcelable(NavController.KEY_DEEP_LINK_INTENT);
assertThat(deepLinkIntent, is(notNullValue(Intent.class)));
assertThat(deepLinkIntent.getAction(), is(TEST_DEEP_LINK_ACTION));
}
private void assertDeepLinkWithArgs(@IdRes int destId, int expectedStackSize) throws Throwable {
Bundle args = new Bundle();
args.putString(TEST_ARG, TEST_ARG_VALUE);
BaseNavigationActivity activity = launchDeepLink(R.navigation.nav_deep_link,
destId, args);
NavController navController = activity.getNavController();
assertThat(navController.getCurrentDestination().getId(), is(destId));
TestNavigator navigator = navController.getNavigatorProvider()
.getNavigator(TestNavigator.class);
assertThat(navigator.mBackStack.size(), is(expectedStackSize));
//noinspection ConstantConditions
assertThat(navigator.mBackStack.peekLast().second.getString(TEST_ARG), is(TEST_ARG_VALUE));
// Test that the deep link Intent was passed in alongside our args
//noinspection ConstantConditions
Intent deepLinkIntent = navigator.mBackStack.peekLast().second
.getParcelable(NavController.KEY_DEEP_LINK_INTENT);
assertThat(deepLinkIntent, is(notNullValue(Intent.class)));
assertThat(deepLinkIntent.getAction(), is(TEST_DEEP_LINK_ACTION));
}
private void assertUriDeepLink(String path, @IdRes int destId, int expectedStackSize)
throws Throwable {
Uri deepLinkUri = Uri.parse("http://www.example.com/" + path + "/" + TEST_ARG_VALUE);
Intent intent = new Intent(Intent.ACTION_VIEW, deepLinkUri)
.setComponent(new ComponentName(mInstrumentation.getContext(),
getActivityClass()))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
BaseNavigationActivity activity = launchActivity(intent);
NavController navController = activity.getNavController();
navController.setGraph(R.navigation.nav_deep_link);
assertThat(navController.getCurrentDestination().getId(), is(destId));
TestNavigator navigator = navController.getNavigatorProvider()
.getNavigator(TestNavigator.class);
assertThat(navigator.mBackStack.size(), is(expectedStackSize));
//noinspection ConstantConditions
assertThat(navigator.mBackStack.peekLast().second.getString(TEST_ARG), is(TEST_ARG_VALUE));
// Test that the deep link Intent was passed in alongside our args
//noinspection ConstantConditions
Intent deepLinkIntent = navigator.mBackStack.peekLast().second
.getParcelable(NavController.KEY_DEEP_LINK_INTENT);
assertThat(deepLinkIntent, is(notNullValue(Intent.class)));
assertThat(deepLinkIntent.getData(), is(deepLinkUri));
}
private BaseNavigationActivity launchDeepLink(@NavigationRes int graphId, @IdRes int destId,
Bundle args) throws Throwable {
TaskStackBuilder intents = new NavDeepLinkBuilder(mInstrumentation.getTargetContext())
.setGraph(graphId)
.setDestination(destId)
.setArguments(args)
.createTaskStackBuilder();
Intent intent = intents.editIntentAt(0);
intent.setAction(TEST_DEEP_LINK_ACTION);
// Now launch the deeplink Intent
BaseNavigationActivity deeplinkActivity = launchActivity(intent);
NavController navController = deeplinkActivity.getNavController();
navController.setGraph(graphId);
return deeplinkActivity;
}
NavDestination findNode(@IdRes int resid, boolean searchParents) {
NavDestination destination = mNodes.get(resid);
// Search the parent for the NavDestination if it is not a child of this navigation graph
// and searchParents is true
return destination != null
? destination
: searchParents && getParent() != null ? getParent().findNode(resid) : null;
}
private <T extends View> T getChildView(@IdRes int viewId) {
View childView = null;
if (view == null) {
childView = view.findViewById(viewId);
}
return (T) childView;
}
/**
* use {@link #setupWithFragment(Fragment, Class, int)} instead
*/
@Deprecated
@NonNull
public static SceneDelegate setupWithFragment(@NonNull final Fragment fragment,
@IdRes int containerId,
@Nullable Bundle savedInstanceState,
@NonNull Class<? extends Scene> rootScene,
@Nullable Bundle bundle,
boolean supportRestore) {
return setupWithFragment(fragment, containerId, savedInstanceState,
new NavigationSceneOptions(rootScene, bundle),
null,
supportRestore);
}
/**
* use {@link #setupWithFragment(Fragment, Class, int)} instead
*/
@Deprecated
@NonNull
public static SceneDelegate setupWithFragment(@NonNull final Fragment fragment,
@IdRes int containerId,
@Nullable Bundle savedInstanceState,
@NonNull NavigationSceneOptions navigationSceneOptions,
@Nullable SceneComponentFactory rootSceneComponentFactory,
final boolean supportRestore,
@NonNull final String tag,
final boolean immediate) {
return setupWithFragment(fragment, containerId, navigationSceneOptions, rootSceneComponentFactory, supportRestore, tag, immediate);
}
@NonNull
@Override
public <T extends View> T requireViewById(@IdRes int viewId) {
T view = mFragment.getView().findViewById(viewId);
if (view == null) {
try {
String viewIdName = mFragment.getResources().getResourceName(viewId);
throw new IllegalArgumentException(" " + viewIdName + " view not found");
} catch (Resources.NotFoundException exception) {
throw new IllegalArgumentException(" " + viewId + " view not found");
}
}
return view;
}
public SceneLifecycleDispatcher(@IdRes int sceneContainerViewId,
ViewFinder viewFinder,
T rootScene,
Scope.RootScopeFactory rootScopeFactory,
boolean supportRestore) {
this.mSceneContainerViewId = sceneContainerViewId;
this.mViewFinder = viewFinder;
this.mScene = rootScene;
this.mRootScopeFactory = rootScopeFactory;
this.mSupportRestore = supportRestore;
}
/**
* use {@link #setupWithActivity(Activity, Class)} instead
*/
@Deprecated
@NonNull
public static SceneDelegate setupWithActivity(@NonNull final Activity activity,
@IdRes int idRes,
@Nullable Bundle savedInstanceState,
@NonNull NavigationSceneOptions navigationSceneOptions,
@Nullable SceneComponentFactory rootSceneComponentFactory,
final boolean supportRestore) {
return setupWithActivity(activity, idRes, savedInstanceState, navigationSceneOptions,
rootSceneComponentFactory, supportRestore, LIFE_CYCLE_FRAGMENT_TAG, true);
}
/**
* Create an {@link android.view.View.OnClickListener} for navigating
* to a destination. This supports both navigating via an
* {@link NavDestination#getAction(int) action} and directly navigating to a destination.
*
* @param resId an {@link NavDestination#getAction(int) action} id or a destination id to
* navigate to when the view is clicked
* @param args arguments to pass to the final destination
* @return a new click listener for setting on an arbitrary view
*/
@NonNull
public static View.OnClickListener createNavigateOnClickListener(@IdRes final int resId,
@Nullable final Bundle args) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
findNavController(view).navigate(resId, args);
}
};
}
MoveStateOperation(@NonNull Scene scene, @IdRes int viewId, @Nullable String tag, @NonNull State dstState, boolean forceShow, boolean forceHide, boolean forceRemove) {
super(scene, dstState, forceShow, forceHide, forceRemove);
if (forceShow && forceHide) {
throw new IllegalArgumentException("cant forceShow with forceHide");
}
this.viewId = viewId;
this.tag = tag;
this.dstState = dstState;
}
NavOptions(int launchMode, @IdRes int popUpTo, boolean popUpToInclusive,
@AnimRes @AnimatorRes int enterAnim, @AnimRes @AnimatorRes int exitAnim,
@AnimRes @AnimatorRes int popEnterAnim, @AnimRes @AnimatorRes int popExitAnim) {
mLaunchMode = launchMode;
mPopUpTo = popUpTo;
mPopUpToInclusive = popUpToInclusive;
mEnterAnim = enterAnim;
mExitAnim = exitAnim;
mPopEnterAnim = popEnterAnim;
mPopExitAnim = popExitAnim;
}
@BindingAdapter(value = {"onCheckedChangedCommand"}, requireAll = false)
public static void onCheckedChangedCommand(final RadioGroup radioGroup, final BindingCommand<String> bindingCommand) {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
bindingCommand.execute(radioButton.getText().toString());
}
});
}
public <V extends View> V findViewById(@IdRes int id) {
View child = childrenCache.get(id);
if (child == null) {
child = itemView.findViewById(id);
if (child != null)
childrenCache.put(id, child);
}
return (V) child;
}
/**
* Sets a destination ID for an action ID.
*
* @param actionId action ID to bind
* @param action action to associate with this action ID
*/
public void putAction(@IdRes int actionId, @NonNull NavAction action) {
if (actionId == 0) {
throw new IllegalArgumentException("Cannot have an action with actionId 0");
}
if (mActions == null) {
mActions = new SparseArrayCompat<>();
}
mActions.put(actionId, action);
}
@SuppressWarnings("unchecked")
protected <T extends View> T findView(@IdRes int id) {
if (mInflate != null) {
return (T) mInflate.findViewById(id);
}
return null;
}
/**
* Sets the destination id to deep link to.
*
* @param destId destination ID to deep link to.
* @return this object for chaining
*/
@NonNull
public NavDeepLinkBuilder setDestination(@IdRes int destId) {
mDestId = destId;
if (mGraph != null) {
fillInIntent();
}
return this;
}
public BaseViewHolder setCompoundDrawables(@IdRes int id,
@Nullable Drawable left,
@Nullable Drawable top,
@Nullable Drawable right,
@Nullable Drawable bottom) {
View view = findViewById(id);
if (view instanceof TextView)
((TextView) view).setCompoundDrawables(left, top, right, bottom);
return this;
}
@Override
public void onNavigatorNavigated(@NonNull Navigator navigator, @IdRes int destId,
@Navigator.BackStackEffect int backStackEffect) {
if (destId != 0) {
// First remove popped destinations off the back stack
if (backStackEffect == Navigator.BACK_STACK_DESTINATION_POPPED) {
while (!mBackStack.isEmpty()
&& mBackStack.peekLast().getId() != destId) {
mBackStack.removeLast();
}
}
NavDestination newDest = findDestination(destId);
if (newDest == null) {
throw new IllegalArgumentException("Navigator " + navigator
+ " reported navigation to unknown destination id "
+ NavDestination.getDisplayName(mContext, destId));
}
if (backStackEffect == Navigator.BACK_STACK_DESTINATION_ADDED) {
// Add the new destination to the back stack
mBackStack.add(newDest);
}
// Don't dispatchOnNavigated if nothing changed
if (backStackEffect != Navigator.BACK_STACK_UNCHANGED) {
dispatchOnNavigated(newDest);
}
}
}
/**
* 根据resid隐藏view
*/
protected void gone(@IdRes int... id) {
if(id != null && id.length > 0) {
for(int resId : id) {
View view = findViewById(resId);
if(view != null) gone(view);
}
}
}
@NonNull
private String getBackStackName(@IdRes int destinationId) {
// This gives us the resource name if it exists,
// or just the destinationId if it doesn't exist
try {
return mContext.getResources().getResourceName(destinationId);
} catch (Resources.NotFoundException e) {
return Integer.toString(destinationId);
}
}
/**
* Determines whether the given <code>destId</code> matches the NavDestination. This handles
* both the default case (the destination's id matches the given id) and the nested case where
* the given id is a parent/grandparent/etc of the destination.
*/
private static boolean matchDestination(@NonNull NavDestination destination,
@IdRes int destId) {
NavDestination currentDestination = destination;
while (currentDestination.getId() != destId && currentDestination.getParent() != null) {
currentDestination = currentDestination.getParent();
}
return currentDestination.getId() == destId;
}
/**
* Returns the destination ID for a given action. This will recursively check the
* {@link #getParent() parent} of this destination if the action destination is not found in
* this destination.
*
* @param id action ID to fetch
* @return destination ID mapped to the given action id, or 0 if none
*/
@Nullable
public NavAction getAction(@IdRes int id) {
NavAction destination = mActions == null ? null : mActions.get(id);
// Search the parent for the given action if it is not found in this destination
return destination != null
? destination
: getParent() != null ? getParent().getAction(id) : null;
}
/**
* Navigate to a destination from the current navigation graph. This supports both navigating
* via an {@link NavDestination#getAction(int) action} and directly navigating to a destination.
*
* @param resId an {@link NavDestination#getAction(int) action} id or a destination id to
* navigate to
* @param args arguments to pass to the destination
* @param navOptions special options for this navigation operation
*/
@SuppressWarnings("deprecation")
public void navigate(@IdRes int resId, @Nullable Bundle args, @Nullable NavOptions navOptions) {
NavDestination currentNode = mBackStack.isEmpty() ? mGraph : mBackStack.peekLast();
if (currentNode == null) {
throw new IllegalStateException("no current navigation node");
}
@IdRes int destId = resId;
final NavAction navAction = currentNode.getAction(resId);
if (navAction != null) {
if (navOptions == null) {
navOptions = navAction.getNavOptions();
}
destId = navAction.getDestinationId();
}
if (destId == 0 && navOptions != null && navOptions.getPopUpTo() != 0) {
popBackStack(navOptions.getPopUpTo(), navOptions.isPopUpToInclusive());
return;
}
if (destId == 0) {
throw new IllegalArgumentException("Destination id == 0 can only be used"
+ " in conjunction with navOptions.popUpTo != 0");
}
NavDestination node = findDestination(destId);
if (node == null) {
final String dest = NavDestination.getDisplayName(mContext, destId);
throw new IllegalArgumentException("navigation destination " + dest
+ (navAction != null
? " referenced from action " + NavDestination.getDisplayName(mContext, resId)
: "")
+ " is unknown to this NavController");
}
if (navOptions != null) {
if (navOptions.shouldClearTask()) {
// Start with a clean slate
popBackStack(mGraph.getId(), true);
} else if (navOptions.getPopUpTo() != 0) {
popBackStack(navOptions.getPopUpTo(), navOptions.isPopUpToInclusive());
}
}
node.navigate(args, navOptions);
}
protected <T extends View> T findView(@IdRes int id) {
return (T) super.findViewById(id);
}
private Builder(@NonNull Fragment fragment, @NonNull Class<? extends Scene> rootSceneClazz, @IdRes int containerId) {
this.mFragment = Utility.requireNonNull(fragment, "Fragment can't be null");
this.mRootSceneClazz = Utility.requireNonNull(rootSceneClazz, "Root Scene class can't be null");
this.mIdRes = containerId;
}
@NonNull
public static Builder setupWithFragment(@NonNull final Fragment fragment,
@NonNull Class<? extends Scene> rootScene,
@IdRes int containerId) {
return new Builder(fragment, rootScene, containerId);
}