【问题标题】:Is it possible to check if a specific "TAB" in TabWidget is visible in current View or not?是否可以检查 TabWidget 中的特定“TAB”在当前视图中是否可见?
【发布时间】:2025-12-11 07:00:01
【问题描述】:

我有一个TabWidget,里面有 5 个标签。 标签:“一”、“二”、“三”、“四”、“五”。 TabHostHorizontalScrollView 中。现在我在TabWidget 的每一侧各有一个箭头(),表示该方向上有更多选项卡(当前在视图中不可见)。

现在我想做的是,如果第一个选项卡不可见(在当前视图中),那么向左的箭头(在当前视图中),那么向右的箭头应该是可见的,如果两个选项卡都不可见(在当前视图中),那么两个箭头应该是可见的。我试过这样做:

if (horizontablscrollview.getLeft == 0)
{
   arrowRight.setVisiblity(View.VISIBLE)
   arrowLeft.setVisiblity(View.INVISIBLE)
}
else if( horizontablscrollview.getRight == 0)
{
   arrowLeft.setVisiblity(View.VISIBLE)
   arrowRight.setVisiblity(View.INVISIBLE)
}

else if ( horizontablscrollview.getRight == 0 && horizontablscrollview.getLeft == 0)
{
    arrowRight.setVisiblity(View.VISIBLE)
    arrowLeft.setVisiblity(View.VISIBLE)
}

但这不起作用,我的两个标签在开始时都是不可见的:

我的 xml 看起来像这样..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

    <RelativeLayout
        android:id="@+id/RL_Header"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_marginBottom="3dip"
        android:background="@drawable/gradient" >

        <ImageButton
            android:id="@+id/btn_Home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="3dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/description_home"
            android:onClick="onHomeButtonClick"
            android:src="@drawable/title_home" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:background="@android:color/transparent"
            android:contentDescription="@string/description_about"
            android:onClick="onClickAbout"
            android:src="@drawable/title_about" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/FL_Tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/RL_Header" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/FL_Tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/RL_Header" >

        <TabHost
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff" >

                    <ImageView android:id="@+id/arrow_left"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/i_button"
                        android:layout_alignParentLeft="true"/>            

            <RelativeLayout
                android:id="@+id/rl_tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                    <ImageView android:id="@+id/arrow_left"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/i_button"
                        android:layout_alignParentLeft="true"
                        android:visibility="invisible"/>

                    <ImageView android:id="@+id/arrow_Right"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/i_button"
                        android:layout_alignParentRight="true"
                        android:visibility="invisible"/>                    

                <HorizontalScrollView
                    android:id="@+id/my_scrollView"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:fillViewport="true"
                    android:scrollbars="none" 
                    android:layout_toRightOf="@+id/arrow_left"
                    android:layout_toLeftOf="@+id/arrow_Right">

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

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="65dip" >

                <RelativeLayout
                    android:id="@+id/myTabContent"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dip" >

                    <TableLayout
                        android:id="@+id/tabSubs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#ffffff"
                        android:shrinkColumns="*"
                        android:stretchColumns="*" >

                        <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="20dip"
                            android:layout_marginTop="20dip"
                            android:gravity="center_horizontal" >

                            <Button
                                android:id="@+id/btntestno1"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 1"
                                android:textColor="#000000" />

                            <Button
                                android:id="@+id/btntestno2"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 2"
                                android:textColor="#000000" />
                        </TableRow>

                        <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="20dip"
                            android:gravity="center_horizontal" >

                            <Button
                                android:id="@+id/btntestno3"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 3"
                                android:textColor="#000000" />

                            <Button
                                android:id="@+id/btntestno4"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 4"
                                android:textColor="#000000" />
                        </TableRow>

                        <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="20dip"
                            android:gravity="center_horizontal" >

                            <Button
                                android:id="@+id/btntestno5"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 5"
                                android:textColor="#000000" />

                            <Button
                                android:id="@+id/btntestno6"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:padding="20dip"
                                android:text="Test 6"
                                android:textColor="#000000" />
                        </TableRow>
                    </TableLayout>
                </RelativeLayout>
            </FrameLayout>
        </TabHost>
    </RelativeLayout>

</RelativeLayout>

任何帮助将不胜感激。

【问题讨论】:

    标签: android view tabs tabwidget horizontalscrollview


    【解决方案1】:

    一种解决方案是让您拥有HorizontalScrollView 类并根据滚动显示ImageViews(尽管我没有测试太多,下面的代码应该可以工作)。您的布局将如下所示:

    //...
               <RelativeLayout
                    android:id="@+id/rl_tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
    
                        <ImageView android:id="@+id/arrow_left"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/i_button"
                            android:layout_alignParentLeft="true"
                            android:visibility="gone"/>
    
                        <ImageView android:id="@+id/arrow_Right"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/i_button"
                            android:layout_alignParentRight="true"
                            />                    
    
                    <com.your.package.here.SpecialScroll
                        android:id="@+id/my_scrollView"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:fillViewport="true"
                        android:scrollbars="none" >
    
                        <TabWidget
                            android:id="@android:id/tabs"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:tabStripEnabled="true"/>
                    </com.your.package.here.SpecialScroll>
                </RelativeLayout>
    
    //...
    

    SpecialScroll 类将如下所示:

    public class SpecialScroll extends HorizontalScrollView {
    
        public interface PositionListener {
    
            public void onLeftArrowRequired(boolean required);
    
            public void onRightArrowRequired(boolean required);
    
            public View implementScrolledView();
        }
    
        private PositionListener listener;
    
        public void setPositionListener(PositionListener listener) {
            this.listener = listener;
        }
    
        public SpecialScroll(Context context) {
            super(context);
        }
    
        public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public SpecialScroll(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            if (l == 0) {
                listener.onLeftArrowRequired(false);
            } else {
                listener.onLeftArrowRequired(true);
            }
            View v = listener.implementScrolledView();
            Rect r = new Rect();
            v.getDrawingRect(r);
            if ((r.width() - l) == getWidth()) {
                listener.onRightArrowRequired(false);
            } else {
                listener.onRightArrowRequired(true);
            }
    
        }
    
    }
    

    然后在您的活动的onCreate 方法中,您将执行以下操作:

    SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
    hsv.setPositionListener(this); // I made the activity implement the SpecialScroll.PositionListener interface
    

    而活动对PositionListener接口的实现是:

    @Override
        public void onLeftArrowRequired(boolean required) {
            if (required) {
                ((ImageView) findViewById(R.id.arrow_left))
                        .setVisibility(View.VISIBLE);
            } else {
                ((ImageView) findViewById(R.id.arrow_left))
                        .setVisibility(View.GONE);
            }
        }
    
        @Override
        public void onRightArrowRequired(boolean required) {
            if (required) {
                ((ImageView) findViewById(R.id.arrow_right))
                        .setVisibility(View.VISIBLE);
            } else {
                ((ImageView) findViewById(R.id.arrow_right))
                        .setVisibility(View.GONE);
            }
        }
    
        @Override
        public View implementScrolledView() {
            return findViewById(android.R.id.tabs);
        }
    

    【讨论】:

    • 嘿伙计,您在此处发布的代码与我想要的很接近,但它并没有按预期工作。我对此有点陌生,不知道如何操作代码以使其正常工作。如果您能检查一下以使其正常工作,这是我的要求。我将不胜感激。很抱歉给您带来麻烦。
    • @user1395649 你应该提到我的代码现在是如何工作的,以及你想如何工作。我无法读心:)
    • lol 对此感到抱歉,发生的情况是,当我向右滚动时,左箭头(应该变得可见)变得不可见,而右箭头(应该消失)停留在那里。中心部分(两个箭头都应该可见)完美地工作。现在,当我将视图从 RIGHT 滚动到极左时,左右两个箭头都停留在那里(当我们在极左时,左边应该消失)。我希望我解释正确,如果不是我在这里。感谢您的快速回复(:
    • @user1395649 我已经重新测试了我的代码并且它可以工作。也许您没有正确实现SpecialScroll.PositionListener 接口?无论如何,我已经把我用于测试的项目放在这里(gist.github.com/2952671),看看它是如何工作的,看看它是否有效,以及它是否对你的代码进行了适当的调整。
    • 哦,好的,我会检查一下,看看我做错了什么,非常感谢,抱歉给您带来的麻烦
    【解决方案2】:

    这不是一个优雅的解决方案,但如果最左侧选项卡的 x 坐标小于零,您可以显示左箭头,如果最右侧选项卡的 x 加上其宽度大于屏幕宽度,则显示右侧选项卡。

    【讨论】:

    • 问题是,如果我有 3 个选项卡(第 4 个、第 5 个和第 6 个),那么箭头应该在第 4 个选项卡旁边可见。我只是想知道是否可以知道特定选项卡是否在视图中可见,因此我们可以相应地设置箭头的可见性。
    • 我不知道我是否明白你在问什么。如果第 4、第 5 和第 6 个选项卡可见,那么我的建议会奏效。你会知道需要一个左箭头。您是将箭头绑在屏幕边缘,还是专门绑在按钮上?如果它们被绑在屏幕上,那么我看不出有什么问题。
    • 您能否在下面查看我的答案,无法在此处发布我的代码和我的评论。很抱歉。
    最近更新 更多