2015-10-20 8 views
6

Mi piacerebbe trovare la vista di una scheda in un TabLayout in modo che possa passarla a un'altra funzione. Non sono sicuro di come fare per trovare la vista. myTabLayout.getTabAt(0).getCustomView() restituisce null.Come si ottiene la visualizzazione di una scheda in un TabLayout?

Come si ottiene la vista?

TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout_main); 
tabLayout.addTab(tabLayout.newTab().setText("Page1")); 
tabLayout.addTab(tabLayout.newTab().setText("Page2")); 

viewPager = (ViewPager) rootView.findViewById(R.id.pager_main); 
pagerAdapter = new MyPagerAdapter(getActivity(), getChildFragmentManager(), tabLayout.getTabCount()); 
viewPager.setAdapter(pagerAdapter); 

risposta

37

Ho finito per utilizzare quanto segue per ottenere visualizzazioni tab. Non sono solo sicuro se è delle migliori pratiche o se è affidabile in tutte le versioni di Android:

mainTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition); 

Guardando alla fonte possiamo vedere che tabLayout.getChildAt(0) restituisce il SlidingTabStrip che è una classe interna che si estende LinearLayout che contiene i punti di vista tab. Quindi, è possibile accedere alla visualizzazione schede con .getChildAt(desiredPosition). Si noti che quando si utilizzano i contatori getChildAt() non vengono controllati, quindi assicurarsi di chiamare gli indici corretti e verificare anche i ritorni a null.

+0

Beh, questo aiuta. Grazie – kushpf

+0

Mentre questo ha funzionato per me, esiterei prima di utilizzarlo in un'app di produzione. Potrebbe causare alcuni grattacapi in fondo alla strada se Google modificasse mai la funzionalità di layout 'Tab'. Dal momento che è per lo più privato, è abbastanza probabile. – fahmad6

+0

come ottenere la visualizzazione del testo? ho bisogno di dare uno stile a onCreate? –

0

Esso restituisce null perché non stai utilizzando qualsiasi visualizzazione personalizzata, in primo luogo. Restituisce la vista personalizzata solo quando la usi. Per usare la vista personalizzata, il tuo codice dovrebbe essere qualcosa del genere.

tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_view).setText("Page1")); 

Se si utilizza la linea di cui sopra e poi tenta di chiamare myTabLayout.getTabAt(0).getCustomView(), sarebbe tornare la vista che si imposta.

+1

Ho pensato che fosse il caso, ma non voglio usare una vista personalizzata. Mi piacerebbe ottenere la visualizzazione predefinita utilizzata. – NSouth

0
TabLayout tabLayout = .... (findview or code creation) 
    /** get selected tab index */ 
    int selectedTabPosition = tabLayout.getSelectedTabPosition(); 
    /** get tab for selected index or if u want any other tab set desired index */ 
    TabLayout.Tab tabAt = tabLayout.getTabAt(selectedTabPosition); 
    /** get view - but first u need set custom view on tabl via Tab.setCustomView(View) */ 
    View tabView = tabAt.getCustomView(): 

suggerimento:

  • se si popolano TabLayout con ViewPager verificare prima se vista è disposto :). Se non impostato onLayoutChangedListener per ViewPager, quindi impostare con cercapersone!

Tab origine Se si desidera utilizzare riflessioni: D

/** 
* A tab in this layout. Instances can be created via {@link #newTab()}. 
*/ 
public static final class Tab { 
    /** 
    * An invalid position for a tab. 
    * 
    * @see #getPosition() 
    */ 
    public static final int INVALID_POSITION = -1; 
    private Object mTag; 
    private Drawable mIcon; 
    private CharSequence mText; 
    private CharSequence mContentDesc; 
    private int mPosition = INVALID_POSITION; 
    private View mCustomView; 
    private final TabLayout mParent; 

    Tab(TabLayout parent) { 
     mParent = parent; 
    } 

    /** 
    * @return This Tab's tag object. 
    */ 
    @Nullable 
    public Object getTag() { 
     return mTag; 
    } 

    /** 
    * Give this Tab an arbitrary object to hold for later use. 
    * 
    * @param tag Object to store 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setTag(@Nullable Object tag) { 
     mTag = tag; 
     return this; 
    } 

    /** 
    * Returns the custom view used for this tab. 
    * 
    * @see #setCustomView(View) 
    * @see #setCustomView(int) 
    */ 
    @Nullable 
    public View getCustomView() { 
     return mCustomView; 
    } 

    /** 
    * Set a custom view to be used for this tab. 
    * <p> 
    * If the provided view contains a {@link TextView} with an ID of 
    * {@link android.R.id#text1} then that will be updated with the value given 
    * to {@link #setText(CharSequence)}. Similarly, if this layout contains an 
    * {@link ImageView} with ID {@link android.R.id#icon} then it will be updated with 
    * the value given to {@link #setIcon(Drawable)}. 
    * </p> 
    * 
    * @param view Custom view to be used as a tab. 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setCustomView(@Nullable View view) { 
     mCustomView = view; 
     if (mPosition >= 0) { 
      mParent.updateTab(mPosition); 
     } 
     return this; 
    } 

    /** 
    * Set a custom view to be used for this tab. 
    * <p> 
    * If the inflated layout contains a {@link TextView} with an ID of 
    * {@link android.R.id#text1} then that will be updated with the value given 
    * to {@link #setText(CharSequence)}. Similarly, if this layout contains an 
    * {@link ImageView} with ID {@link android.R.id#icon} then it will be updated with 
    * the value given to {@link #setIcon(Drawable)}. 
    * </p> 
    * 
    * @param layoutResId A layout resource to inflate and use as a custom tab view 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setCustomView(@LayoutRes int layoutResId) { 
     return setCustomView(
       LayoutInflater.from(mParent.getContext()).inflate(layoutResId, null)); 
    } 

    /** 
    * Return the icon associated with this tab. 
    * 
    * @return The tab's icon 
    */ 
    @Nullable 
    public Drawable getIcon() { 
     return mIcon; 
    } 

    /** 
    * Return the current position of this tab in the action bar. 
    * 
    * @return Current position, or {@link #INVALID_POSITION} if this tab is not currently in 
    * the action bar. 
    */ 
    public int getPosition() { 
     return mPosition; 
    } 

    void setPosition(int position) { 
     mPosition = position; 
    } 

    /** 
    * Return the text of this tab. 
    * 
    * @return The tab's text 
    */ 
    @Nullable 
    public CharSequence getText() { 
     return mText; 
    } 

    /** 
    * Set the icon displayed on this tab. 
    * 
    * @param icon The drawable to use as an icon 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setIcon(@Nullable Drawable icon) { 
     mIcon = icon; 
     if (mPosition >= 0) { 
      mParent.updateTab(mPosition); 
     } 
     return this; 
    } 

    /** 
    * Set the icon displayed on this tab. 
    * 
    * @param resId A resource ID referring to the icon that should be displayed 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setIcon(@DrawableRes int resId) { 
     return setIcon(TintManager.getDrawable(mParent.getContext(), resId)); 
    } 

    /** 
    * Set the text displayed on this tab. Text may be truncated if there is not room to display 
    * the entire string. 
    * 
    * @param text The text to display 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setText(@Nullable CharSequence text) { 
     mText = text; 
     if (mPosition >= 0) { 
      mParent.updateTab(mPosition); 
     } 
     return this; 
    } 

    /** 
    * Set the text displayed on this tab. Text may be truncated if there is not room to display 
    * the entire string. 
    * 
    * @param resId A resource ID referring to the text that should be displayed 
    * @return The current instance for call chaining 
    */ 
    @NonNull 
    public Tab setText(@StringRes int resId) { 
     return setText(mParent.getResources().getText(resId)); 
    } 

    /** 
    * Select this tab. Only valid if the tab has been added to the action bar. 
    */ 
    public void select() { 
     mParent.selectTab(this); 
    } 

    /** 
    * Returns true if this tab is currently selected. 
    */ 
    public boolean isSelected() { 
     return mParent.getSelectedTabPosition() == mPosition; 
    } 

    /** 
    * Set a description of this tab's content for use in accessibility support. If no content 
    * description is provided the title will be used. 
    * 
    * @param resId A resource ID referring to the description text 
    * @return The current instance for call chaining 
    * @see #setContentDescription(CharSequence) 
    * @see #getContentDescription() 
    */ 
    @NonNull 
    public Tab setContentDescription(@StringRes int resId) { 
     return setContentDescription(mParent.getResources().getText(resId)); 
    } 

    /** 
    * Set a description of this tab's content for use in accessibility support. If no content 
    * description is provided the title will be used. 
    * 
    * @param contentDesc Description of this tab's content 
    * @return The current instance for call chaining 
    * @see #setContentDescription(int) 
    * @see #getContentDescription() 
    */ 
    @NonNull 
    public Tab setContentDescription(@Nullable CharSequence contentDesc) { 
     mContentDesc = contentDesc; 
     if (mPosition >= 0) { 
      mParent.updateTab(mPosition); 
     } 
     return this; 
    } 

    /** 
    * Gets a brief description of this tab's content for use in accessibility support. 
    * 
    * @return Description of this tab's content 
    * @see #setContentDescription(CharSequence) 
    * @see #setContentDescription(int) 
    */ 
    @Nullable 
    public CharSequence getContentDescription() { 
     return mContentDesc; 
    } 
} 

oppure si può collegare direttamente (attraverso la riflessione):

private final SlidingTabStrip mTabStrip; 

oppure è possibile copiare il codice sorgente e le modalità di cambiamento e campi a tua discrezione.

+0

Sfortunatamente, questo mi dà la 'Tab', non la vista associata alla scheda. – NSouth

+0

@NSouth - layout della scheda - è ciò che è sopra il contenuto del cercapersone - il contenuto del cercapersone è frammentato - vuoi il contenuto del cercapersone o il contenuto della scheda? vedi modifica – ceph3us

+0

Grazie, questa è una risposta molto informativa. Sì, desidero visualizzare la scheda (non il contenuto del frammento del cercapersone). Questo è il metodo che ho trovato per funzionare, anche se non sono sicuro che sia la migliore pratica. 'mainTab = ((ViewGroup) tabLayout.getChildAt (0)). getChildAt (0);'. Non riesco a utilizzare 'getCustomView()' perché non ho mai impostato una visualizzazione personalizzata. Tuttavia, potrei estendere la mia classe se necessario. – NSouth

Problemi correlati