ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 是否可以检查TabWidget中的特定“TAB”是否在当前视图中可见?

2019-07-04 09:13:25  阅读:181  来源: 互联网

标签:android tabs view horizontalscrollview tabwidget


我有一个带有5个标签的TabWidget.标签:“一个”,“两个”,“三个”,“四个”,“五个”. TabHost位于Horizo​​ntalScrollView中.现在,我在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>

任何帮助将不胜感激.

解决方法:

一种解决方案是让你拥有Horizo​​ntalScrollView类并根据滚动显示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);
    }

标签:android,tabs,view,horizontalscrollview,tabwidget
来源: https://codeday.me/bug/20190704/1375500.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有