Để dễ dàng chuyển đổi giữa các mảnh vỡ, tôi nhúng một HorizontalScrollView vào layout Tab tôi như vậy:HorizontalScrollView trong TabHost bổ sung thêm không gian ở cuối dòng
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
Nhưng sau khi thêm Những mảnh vỡ trong mã của tôi (hiển thị dưới đây) thì bỗng dưng xuất hiện một số khoảng trắng thêm vào cuối của HorizontalScrollView:
Trước khi di chuyển
Sau khi di chuyển
Mã này là khá phức tạp nhưng tôi sẽ cố gắng hiển thị những phần quan trọng.
{
mTabHost = (TabHost) childLayout.findViewById(android.R.id.tabhost);
mTabHost.setup();
FrameLayout tabsFL = (FrameLayout) childLayout.findViewById(android.R.id.tabcontent);
tabsFL.setId(TABS_FRAME_ID);
for (int i = 0; i < list.size(); i++) {
mTabHost.addTab(newTab(String.valueOf(i), list.get(i).getTitle(), tabsFL.getId()));
}
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
updateTab(tabId, Integer.parseInt(tabId), list);
}
});
//manually load first fragment
mTabHost.setCurrentTab(mCurrentTab);
updateTab(String.valueOf(mCurrentTab), mCurrentTab, list);
}
private TabSpec newTab(String tag, String tabLabel, int tabContentId) {
int count = Integer.parseInt(tag);
count +=1;
View indicator = inflater.inflate(R.layout.details_tab,
(ViewGroup) childLayout.findViewById(android.R.id.tabs), false);
((TextView) indicator.findViewById(R.id.text)).setText(count + ". " + tabLabel);
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
private void updateTab(String tabId, int id, ArrayList<CustomObject> frags) {
mCurrentTab = id;
FragmentManager fm = activity.getSupportFragmentManager();
fm.beginTransaction()
.replace(TABS_FRAME_ID, DetailsFragment.newInstance(frags.get(id)), tabId)
.commitAllowingStateLoss();
}
Cũng không liên quan, nhưng tôi cũng gặp sự cố khi tab đầu tiên không tải theo cách thủ công (chỉ cần nhấn tab đầu tiên tải không hoàn toàn vì lý do nào đó).
Điều này có thể là do của 'android: fillViewport = "true"' trong 'HorizontalScrollView', cố gắng loại bỏ nó cho một thể hiện và xem liệu điều này có hiệu quả không. Cả hai 'HorizontalScrollView' và' TabWidget' đều có 'android: layout_height =" wrap_content "', do đó 'fillViewport' có thể là lý do. – g00dy
Thật không may tôi đã thử điều đó và nó không có sự khác biệt. Tôi nghĩ có lẽ tôi vô tình thêm một tab phụ (vô hình?), Nhưng không thể tìm thấy nơi mà có thể xảy ra với các báo cáo đăng nhập. –
Có thể nó nằm trong 'tabFL'? Tôi không biết, nhưng cũng có lẽ Tab cuối cùng là một nửa vô hình, chech mà một quá :) – g00dy