2015-10-20 16 views
6

Tôi muốn tìm giao diện của một Tab trong TabLayout để tôi có thể chuyển nó sang một chức năng khác. Tôi không chắc chắn làm thế nào để đi về việc tìm kiếm xem. myTabLayout.getTabAt(0).getCustomView() trả về giá trị rỗng.Làm cách nào để có được chế độ xem Tab trong TabLayout?

Làm cách nào để có chế độ xem?

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

Trả lời

37

Tôi đã kết thúc bằng cách sử dụng các mục sau để xem các tab. Tôi chỉ không chắc chắn nếu nó thực hành tốt nhất hoặc nếu đó là đáng tin cậy trên tất cả các phiên bản Android:

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

Nhìn vào nguồn chúng ta có thể thấy rằng tabLayout.getChildAt(0) trả về SlidingTabStrip đó là một lớp học nội mở rộng LinearLayout giữ quan điểm tab. Sau đó, bạn có thể truy cập chế độ xem tab với .getChildAt(desiredPosition). Lưu ý rằng khi sử dụng các ranh giới getChildAt() không được kiểm tra, vì vậy hãy đảm bảo rằng bạn đang gọi các chỉ mục chính xác và cũng có thể kiểm tra số tiền trả về null.

+0

Vâng, điều đó giúp ích. Cảm ơn – kushpf

+0

Trong khi điều này làm việc cho tôi, tôi sẽ ngần ngại trước khi sử dụng điều này trong một ứng dụng sản xuất. Nó có thể gây ra một số nhức đầu xuống đường nếu Google thay đổi chức năng bố cục 'Tab'. Vì nó chủ yếu là tư nhân, nó khá khả thi. – fahmad6

+0

cách xem văn bản? tôi cần phải chage phong cách trong onCreate? –

0

Trả về null vì bạn hiện không sử dụng bất kỳ Chế độ xem tùy chỉnh nào. Nó trả về chế độ xem tùy chỉnh chỉ khi bạn sử dụng nó. Để sử dụng chế độ xem tùy chỉnh, mã của bạn phải giống như thế này.

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

Nếu bạn sử dụng dòng ở trên và sau đó cố gắng gọi myTabLayout.getTabAt(0).getCustomView(), nó muốn trả lại cho bạn quan điểm cho rằng bạn đặt.

+1

Tôi đã tìm ra trường hợp đó, nhưng tôi không muốn sử dụng chế độ xem tùy chỉnh. Tôi muốn sử dụng chế độ xem mặc định. – 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(): 

gợi ý:

  • nếu bạn cư TabLayout với ViewPager kiểm tra đầu tiên nếu xem được đặt ra :). Nếu không được đặt onLayoutChangedListener cho ViewPager thì hãy thiết lập với máy nhắn tin!

Tab Nguồn nếu bạn muốn sử dụng phản xạ: 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; 
    } 
} 

hoặc bạn có thể treo trực tiếp vào (thông qua phản ánh):

private final SlidingTabStrip mTabStrip; 

hoặc bạn có thể sao chép mã nguồn và phương pháp thay đổi và các lĩnh vực theo quyết định của bạn.

+0

Thật không may, điều đó đã cho tôi 'Tab', không phải là chế độ xem được liên kết với tab. – NSouth

+0

@NSouth - bố cục tab - là nội dung ở trên nội dung máy nhắn tin - nội dung của máy nhắn tin là phân đoạn - u có muốn nội dung trang hoặc nội dung tab không? xem chỉnh sửa – ceph3us

+0

Cảm ơn, đây là một câu trả lời rất có nhiều thông tin. Có, tôi muốn chế độ xem của tab (không phải nội dung phân đoạn của máy nhắn tin). Đây là phương pháp tôi tìm thấy để làm việc, mặc dù tôi không chắc chắn rằng đó là thực hành tốt nhất. 'mainTab = ((ViewGroup) tabLayout.getChildAt (0)). getChildAt (0);'. Tôi không thể sử dụng 'getCustomView()' vì tôi chưa bao giờ đặt chế độ xem tùy chỉnh. Tuy nhiên, tôi có thể mở rộng lớp học của riêng mình nếu tôi cần. – NSouth

Các vấn đề liên quan