是否可以检查是否有特异性]标签"在TabWidget是当前视图或不可见? [英] Is it possible to check if a specific "TAB" in TabWidget is visible in current View or not?

查看:163
本文介绍了是否可以检查是否有特异性]标签"在TabWidget是当前视图或不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabWidget 有5个选项卡在里面。 标签:一,二,三,四有,五。该 TabHost 是一个 Horizo​​ntalScrollView 。现在我一箭(小于和>)各在两边的 TabWidget 来表明,在这一方向上的标签(这是目前不在视图中可见)。

I have a TabWidget with 5 tabs in it. Tabs : "One","Two","Three","Four","Five". The TabHost is in a HorizontalScrollView. Now I've one arrow (< and >) each on each side of the TabWidget to indicate that there are more tabs in that direction(which are currently not visible in view).

现在我想要做的是,如果第一个选项卡是不可见的(在当前视图),那么箭头向左(小于)应该是可见的,如果最后一个选项卡是不可见(在当前视图),那么箭头向右应该是可见的,如果两个标签不可见(在当前视图),那么两个箭头应该是可见的。我试着用做它:

Now what I want to do is, if the first tab is not visible(in current view) then the arrow to left (<) should be visible and if the last tab is not visible(in current view) then the arrow to right should be visible and if both tabs are not visible(in current view) then both arrows should be visible. I tried doing it with:

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)
}

但是,这并不正常工作,我的两个标签是不可见的启动:

But this doesn't work, my both tabs are invisible in the start:

我的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>

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

一个解决办法是让你自己的 Horizo​​ntalScrollView 类,并显示 ImageViews 基于滚动(虽然我没有测试过,大部分的code以下应该工作)。您的布局将是这样的:

One solution would be to make you own HorizontalScrollView class and show the ImageViews based on the scroll(although I didn't tested that much the code below should work). Your layout will look like this:

//...
           <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 的接口是:

and the activity's implementation of the PositionListener interface is:

@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);
    }

这篇关于是否可以检查是否有特异性]标签&QUOT;在TabWidget是当前视图或不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆