gpt4 book ai didi

android - 选项卡小部件不会在 android 中滚动内容

转载 作者:行者123 更新时间:2023-11-30 03:19:09 25 4
gpt4 key购买 nike

enter image description here

在这里,我在处理选项卡小部件时遇到了问题。我已经成功地实现了它,但是我在这里面临的是当我滑动内容时(这里是图像 1,目前我在选项卡 3 上),选项卡会相应地发生变化,但它们不会被滚动。看看没有图像。 2 enter image description here

现在,在图 2 中,我在选项卡 4 上,但此处的选项卡没有滚动内容。下面是我的代码,

activity_main.xml

<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</HorizontalScrollView>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<FrameLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<FrameLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<FrameLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

<FrameLayout
android:id="@+id/tab5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</TabHost>

主 Activity .java

public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{  
private TabHost tabHost;
private ViewPager pager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
pager = (ViewPager) findViewById(R.id.pager);

tabHost.setup();
TabWidget tabwidget=tabHost.getTabWidget();


Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();




TabSpec spec = tabHost.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Check In");
tabHost.addTab(spec);

spec = tabHost.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Buddies");
tabHost.addTab(spec);

spec = tabHost.newTabSpec("tab3");
spec.setContent(R.id.tab3);
spec.setIndicator("Recommendation");
tabHost.addTab(spec);

spec = tabHost.newTabSpec("tab4");
spec.setContent(R.id.tab4);
spec.setIndicator("Feed");
tabHost.addTab(spec);

spec = tabHost.newTabSpec("tab5");
spec.setContent(R.id.tab5);
spec.setIndicator("Last");
tabHost.addTab(spec);

TextView x = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
x.setTextSize(10);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 110;

TextView x1 = (TextView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
x1.setTextSize(10);
tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = 90;

TextView x2 = (TextView) tabHost.getTabWidget().getChildAt(2).findViewById(android.R.id.title);
x2.setTextSize(10);
tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = 200;

TextView x3 = (TextView) tabHost.getTabWidget().getChildAt(3).findViewById(android.R.id.title);
x3.setTextSize(10);
tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = 30;

TextView x4 = (TextView) tabHost.getTabWidget().getChildAt(4).findViewById(android.R.id.title);
x4.setTextSize(10);
tabHost.getTabWidget().getChildAt(4).getLayoutParams().width = 30;



pager.setAdapter(new MyPagerAdapter(this));
pager.setOnPageChangeListener(this);
tabHost.setOnTabChangedListener(this);

}
@Override
public void onTabChanged(String tabId)
{
int pageNumber = 0;
if(tabId.equals("tab1"))
{
pageNumber = 0;

}

else if(tabId.equals("tab2"))
{
pageNumber = 1;

}

else if(tabId.equals("tab3"))
{
pageNumber = 2;

}

else if(tabId.equals("tab4"))
{
pageNumber = 3;


}

else if(tabId.equals("tab5"))
{
pageNumber = 4;

}
else
{
pageNumber = 0;

}

pager.setCurrentItem(pageNumber);

}

@Override
public void onPageSelected(int pageNumber) {
tabHost.setCurrentTab(pageNumber);


}
@Override
public void onPageScrollStateChanged(int arg0) {

}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


}


}

最佳答案

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
View tabView = tabHost.getTabWidget().getChildAt(position);
if (tabView != null)
{
final int width = mHorizontalScroll.getWidth();
final int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
mHorizontalScroll.scrollTo(scrollPos, 0);
} else {
mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
}
}

关于android - 选项卡小部件不会在 android 中滚动内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19486853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com