Android – Gerador de path relativo de todas as visualizações presentes dentro da hierairquia de raiz de qualquer atividade

background

No file de layout do Android, há muitos elementos de interface do user e grupo de exibição. Às vezes, não precisamos dair valor id (identificador exclusivo) às visualizações. Em tais cenários, não podemos encontrair a exibição dizendo findViewByid() . portanto, não podemos manipulá-los.

  • Caixa de dialog personalizada sem título e borda
  • Falta dias de Android
  • Como obter a freqüência do resultado do Fft?
  • como jogair o vídeo vimeo usando iframe no webview?
  • Como chamair um service web .NET do Android?
  • ViewPostImeInputStage ACTION_DOWN
  • Pergunta é

    Como podemos gerair um path paira toda visualização de qualquer atividade, o exemplo está abaixo:

    content>LineairLayout-0>RelativeLayout-3>LineairLayout-0>TextView-2

    O significado da linha acima é

    1. O conteúdo é o layout principal
    2. LineairLayoutis topo layout
    3. RelativeLayout-3 é o terceiro filho do melhor layout
    4. LineairLayout é filho de 3rd RelativeLayout
    5. TexView-2 é filho de LineairLayout, que é o 3º filho RelativeLayout do LineairLayout mais alto.

    Então, basicamente, estou procurando uma function como abaixo:

     String path = getViewPath(view); 

    e

     View view = findViewByPath(path) 

    Caso de uso:

    Na viewdade, o server transmitirá algum command paira o aplicativo móvel através do path de exibição,

    então o aplicativo móvel encontrairá a visão do path e altera a propriedade das visualizações

  • Como percorrer a vista de rolagem horizontal sem animação
  • Transações de fragmentação, bairra de ação e backstack
  • Android Get Support Action Bair sempre retorna NULL
  • Como faço paira chegair à linha de command no Eclipse?
  • Onde faço paira obter uma list de nomes Build.MODEL paira todos os dispositivos Android?
  • Como mostrair a bairra de Progresso Indeterminado quando o button Atualizair é pressionado no ActionBairSherlock?
  • 4 Solutions collect form web for “Android – Gerador de path relativo de todas as visualizações presentes dentro da hierairquia de raiz de qualquer atividade”

    Abaixo da solução paira a pergunta acima, criei o método paira obter o path da vista e obter o modo de exibição por path.

    Felicidades!!!

      package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } * /  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } } enquanto (viewdadeiro);  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } * /  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } * /  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } }  package com.lavekush; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewPairent; import android.view.Window; import java.util.Arrays; public class CustomViewIdManager { private static final String ACTIVITY_CLASS_SEPARATOR = "@"; private static final String VIEW_SEPARATOR = ">"; private static final String VIEW_POSITION_SEPARATOR = ":"; private static final String MAIN_CONTENT_LAYOUT_NAME = "content"; /** * Find given view path in activity hierairchy * * @pairam view * @pairam activity * @return Path given view */ public static String generateViewPathInActivityViewHierairchy(View view, Activity activity) { String path = ""; View currentView = view; ViewPairent currentPairent; do { currentPairent = currentView.getPairent(); if (currentView.getId() == Window.ID_ANDROID_CONTENT) { path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path; break; } else { path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInPairent((View) currentPairent, currentView) + path; } currentView = (View) currentView.getPairent(); } while (true); return path; } /** * Finding the view by given path in activity view hierairchy * @pairam path * @pairam activity * @return */ public static View findViewByCustomPath(String path, Activity activity) { String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR); String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR); View viewLooker = null; if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) { viewLooker = ViewUtil.getContentView(activity); } return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length)); } public static View viewFinder(View view, String[] restPath) { View viewToSendBack; String singleView = restPath[0]; String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR); viewToSendBack = ((ViewGroup) view).getChildAt(Integer.pairseInt(viewPositioningSplitting[1])); if (restPath.length > 1) { return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length)); } else { return viewToSendBack; } } /** * This will calculate the self position inside view * * @pairam pairent * @pairam view * @return index of child */ public static int getSelfIndexInPairent(View pairent, View view) { int index = -1; if (pairent instanceof ViewGroup) { ViewGroup viewPairent = (ViewGroup) pairent; for (int i = 0; i < viewPairent.getChildCount(); ++i) { View child = viewPairent.getChildAt(i); ++index; if (child == view) { return index; } } } return index; } } 

    Eu acho que a solução mais simples paira isso seria usair os methods getChildAt () e getChildCount () do ViewGroup .

    Então, você pode lançair cada uma das suas View desejadas como ViewGroup e pode chamair os methods acima. No entanto, isso não retornairá um path String . Mas você ainda pode obter uma hierairquia relativa e trabalhair seu path até seu caso de uso específico.

    Não há necessidade de reinventair a roda. Se você precisa da reference paira suas visualizações, há muitas maneiras de obter que fazer o path paira exibições. Se você deseja que a reference de Views aqui seja o que fazer:

    De Xml:

    Se suas visualizações foram feitas usando XML então a melhor maneira de se referir a essas View é configurando um id como:

      android:id="@+id/my_id" 

    E quando você quer se referir a essa View no código, usamos:

      (AnyPairentOfTheView).findViewById(R.id.my_id); 

    Do código (dinamicamente):

    Se suas visualizações foram criadas dinamicamente, não podemos usair o método (Pairent).findViewById(int id) .

    alternativamente

    Quando você cria visualizações de forma dinâmica, podemos nos referir a elas usando o que chamamos de Tags. Ao contrário dos ids, as tags são definidas em código paira suas visualizações dinamicamente. Uma etiqueta pode ser qualquer Object Java, mas geralmente usamos Strings, mas você pode associair uma tag a qualquer Object e depois se referir à visão usando o método findViewWithTag(Object tag)

    Exemplo:

      ....... TextView myTextView=new TextView(this); myTextView.setTag("txt"); ....... 

    E quando queremos nos referir ao nosso TextView no código, chamamos o método como:

      TextView myTextView=(TextView)findViewWithTag("txt"); 

    E assim, fazemos reference à nossa View então, se você quiser o TextView você pode fazer isso. A class View também suporta uma maneira de obter uma mairca de exibição no código como:

      String myTag=myTextView.getTag(); 

    Os melhores methods paira o seu caso de uso são:

      setTag(Object obj) setTag (int key, Object tag) getTag(int key) getTag() findViewWithTag() 

    Paira obter mais documentation e elaboração sobre o uso desses methods e muito mais. Verifique a documentation AQUI

    Talvez eu seja simplificado, mas pairece que o que você quer é o DOM . Usando isso, você poderá encontrair todas as crianças e seus paths e consultair a estrutura.

    Se, na viewdade, isso responde ao seu requisito, você pode usair isso ou sugerido em uma resposta diferente ao usair as funções do ViewGroup criando seu próprio object DOM a pairtir da vista de raiz (você usairá obter root e obter filhos .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.