TabHost自动滚动使用HorizontalScrollView - 以编程方式

4
我正在开发一个使用TabHost的示例应用程序,其中包含HorizontalScrollView。 下面是我的问题:
  • 我有超过10个选项卡,当我单击其中任何一个选项卡时,它应该将重心设置为中心? 我该如何解决这个问题。
提前感谢!!!

嗨,画廊视图是实现这一目标的最佳选择。 - milind
我知道可以使用GalleryView实现,但我希望能用TabHost来实现。 - NikhilReddy
2个回答

5
这将涉及一些计算。
基本上,您需要滚动到位置以将元素居中。
int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2);
hScrollView.scrollTo(scrollX,0);

Where the

button -> It is the button you are trying to center.

screenWidth -> Width of the screen.
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

hScrollView -> It is the horizontal scrollview.

但即使使用这种方法,您也无法将结束元素居中,因为滚动视图不会滚动超出其边界。


我想请你再问一件事吗? - NikhilReddy
假设我有10个标签页,如1、2、3、4所示,当单击任何一个标签页时,它应该替换在位置2处。 - NikhilReddy

0
public void onTabChanged(String tabId) {
    for (int i = 0; i < tablist.length; i++) {
        if (tabId.contentEquals(tablist[i])) {
            HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horScrollView);
            scroll.smoothScrollTo(((int)(getResources().getDimensionPixelSize(R.dimen.tab_width) * (i))), 0);
        } 
    }

我使用了:implements OnTabChangeListener。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接