Android TabHost中的HorizontalScrollView在末端添加额外空间
为了在片段之间轻松切换,我将水平滚动视图嵌入到选项卡布局中,如下所示:Android TabHost中的HorizontalScrollView在末端添加额外空间,android,android-fragments,android-tabs,horizontalscrollview,Android,Android Fragments,Android Tabs,Horizontalscrollview,为了在片段之间轻松切换,我将水平滚动视图嵌入到选项卡布局中,如下所示: <?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_he
<?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>
但是在我的代码中添加了片段(如下所示)之后,在HorizontalScrollView的末尾突然出现了一些额外的空白:
在滚动之前
滚动后
代码相当复杂,但我将尝试展示重要的部分
{
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();
}
{
mTabHost=(TabHost)childLayout.findViewById(android.R.id.TabHost);
mTabHost.setup();
FrameLayout选项卡sfl=(FrameLayout)childLayout.findviewbyd(android.R.id.tabcontent);
tabsFL.setId(TABS\u FRAME\u ID);
对于(int i=0;i
同样不相关,但我也有一个问题,第一个选项卡无法手动加载(单击选项卡可以完美地加载片段,只是第一个选项卡由于某种原因无法加载)。我认为您是手动添加tabhost之类的
@SuppressLint("ResourceAsColor")
public class MainActivity extends FragmentActivity implements
OnTabChangeListener, OnPageChangeListener {
MyPageAdapter pageAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
// Tab Initialization
initialiseTabHost();
// Fragments and ViewPager Initialization
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(pageAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
// Method to add a TabHost
private static void AddTab(MainActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
// mTabHost.getTabWidget().setDividerDrawable(null);
setSelectedTabColor();
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
// mTabHost.getTabWidget().setDividerDrawable(null);
}
@Override
public void onPageSelected(int arg0) {
}
@SuppressLint("ResourceAsColor")
private void setSelectedTabColor() {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().setDividerDrawable(
android.R.color.transparent);
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(getResources().getColor(R.color.white));
mTabHost.getTabWidget()
.setShowDividers(TabWidget.SHOW_DIVIDER_NONE);
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(R.drawable.bottom_home_back);
// mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width =
// 50;
}
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
.setBackgroundResource(R.drawable.btn_selected);
// mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50;
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
// TODO Put here your Fragments
// DealTab f1 = DealTab.newInstance();
DealTab_New f1 = DealTab_New.newInstance();
EventTab f2 = EventTab.newInstance();
MyAccountFragment f3 = MyAccountFragment.newInstance();
MessageFragment f4 = MessageFragment.newInstance();
MoreFragment f5 = MoreFragment.newInstance();
QrCodeFragment f6 = QrCodeFragment.newInstance();
// fList.add(f1);
fList.add(f1);
fList.add(f2);
fList.add(f3);
fList.add(f4);
fList.add(f5);
fList.add(f6);
return fList;
}
// Tabs Creation
@SuppressLint("ResourceAsColor")
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// TODO Put here your Tabs
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab1").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_deals)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab2").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_event)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab3").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_my_account)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab4").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_message)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab5").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_more)));
mTabHost.setOnTabChangedListener(this);
setSelectedTabColor();
}
}
@suppressint(“资源色”)
公共类MainActivity扩展了FragmentActivity实现
OnTabChangeListener,OnPageChangeListener{
MyPageAdapter-pageAdapter;
私有视图寻呼机mViewPager;
私有TabHost-mTabHost;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager=(ViewPager)findViewById(R.id.ViewPager);
//选项卡初始化
initialiseTabHost();
//片段和ViewPager初始化
List fragments=getFragments();
pageAdapter=新的MyPageAdapter(getSupportFragmentManager(),片段);
mViewPager.setAdapter(页面适配器);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
//方法添加TabHost
私有静态void AddTab(MainActivity活动,TabHost TabHost,
TabHost.TabSpec(TabSpec){
tabSpec.setContent(新的MyTabFactory(活动));
tabHost.addTab(tabSpec);
}
//管理选项卡更改,使其与页面同步
已更改的公用void onTabChanged(字符串标记){
int pos=this.mTabHost.getCurrentTab();
此.mviewpage.setCurrentItem(pos);
//mTabHost.getTabWidget().setDividerDrawable(空);
设置SelectedTabColor();
}
@凌驾
公共无效onPageScrollStateChanged(int arg0){
}
//管理页面更改,并将其与选项卡同步
@凌驾
已滚动页面上的公共void(int arg0、float arg1、int arg2){
int pos=this.mviewpage.getCurrentItem();
此.mTabHost.setCurrentTab(位置);
//mTabHost.getTabWidget().setDividerDrawable(空);
}
@凌驾
已选择页面上的公共无效(int arg0){
}
@SuppressLint(“资源色”)
私有void setSelectedTabColor(){
对于(int i=0;i<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:measureWithLargestChild="false">
</TabWidget>