2015-10-20 15 views
6

TabLayout'ta bir sekme görünümünü bulmak istiyorum, böylece başka bir işleve iletebilirim. Manzarayı bulmak için nasıl gideceğimi bilmiyorum. myTabLayout.getTabAt(0).getCustomView(), null değerini döndürür.TabLayout'ta bir Sekme görünümünü nasıl edinebilirim?

Görünümü nasıl alırım? ilk etapta herhangi Özel Görünüm kullanarak etmediklerinden

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); 

cevap

37

Sekme görünümlerini almak için aşağıdakileri kullanarak bitti. Sadece en iyi yöntem olup olmadığından emin değilim yoksa tüm Android sürümleri arasında güvenilir olup olmadığını:

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

görebildiğimiz kaynağında baktığımızda o tabLayout.getChildAt(0) döner sekme görüntülerini tutan LinearLayout uzanan bir iç sınıftır SlidingTabStrip. Ardından sekme görünümüne .getChildAt(desiredPosition) ile erişebilirsiniz. getChildAt() sınırlarının işaretlenmediğini kontrol ettiğinizde, doğru dizinleri aradığınızdan emin olun ve null dönüşlerini kontrol edin.

+0

Peki, bu yardımcı olur. Teşekkürler – kushpf

+0

Bu benim için çalıştı, ancak bir üretim uygulamasında kullanmadan önce tereddüt ediyorum. Google, "Sekme" işlevini değiştirirse yolun aşağısında biraz baş ağrısına neden olabilir. Çoğunlukla özel olduğu için oldukça muhtemel. – fahmad6

+0

Metin görüntüsünü nasıl alırsınız? OnCreate'da stili uyarmalı mıyım? –

0

O null döndürür. Özel görünümü yalnızca onu kullandığınızda döndürür. Özel görünümü kullanabilmeniz için kodunuzun böyle bir şey olması gerekir. Yukarıdaki satırını kullanın ve sonra myTabLayout.getTabAt(0).getCustomView() çağırmak çalışırsanız

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

, bu size ayarladığınız görünümü dönecekti.

+1

Durumun böyle olduğunu düşündüm, ancak özel bir görünüm kullanmak istemiyorum. Kullanılan varsayılan görünümü almak istiyorum. – 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(): 

ipucu: view :) düzenlendiği ilk eğer TabLayout onay ViewPager ile size doldurmak eğer

  • . 'u ViewPager için ayarlamazsanız, çağrı cihazı ile kurun!

Tab Kaynağı yansımaları kullanmak istiyorsanız: 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; 
    } 
} 

veya (yansıma yoluyla) doğrudan kanca:

private final SlidingTabStrip mTabStrip; 

veya kaynak kodunu ve değişim yöntemlerini kopyalayabilirsiniz ve kendi takdirine göre alanlar.

+0

Maalesef, bu sekme ile ilişkili görünüm değil, "Tab" mesajını alır. – NSouth

+0

@NSouth - sekme düzeni - çağrı cihazı içeriğinin üstünde olan - çağrı cihazı içeriği bölümdür - çağrı cihazı içeriği mi yoksa sekme içeriği mi? bkz. düzenle – ceph3us

+0

Teşekkürler, bu çok bilgilendirici bir cevaptır. Evet, sekmenin görünümünü istiyorum (çağrı parçası içeriği değil). Çalıştığımı bulduğum yöntem bu olsa da, en iyi uygulama olduğundan emin değilim. mainTab = ((ViewGroup) tabLayout.getChildAt (0)) getChildAt (0); '. GetCustomView() işlevini kullanamıyorum çünkü özel bir görünüm ayarlamam. Ancak, ihtiyacım olursa kendi sınıfımı genişletebilirim. – NSouth