4

私はTabWidgetそれに5つのタブを持っています。タブ: "One"、"Two"、"Three"、"Four"、"Five"。はTabHostですHorizontalScrollView。の両側に 1 つの矢印 (< と >)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>

どんな助けでも大歓迎です。

4

2 に答える 2

2

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);
    }
于 2012-06-16T14:48:28.693 に答える
0

これは洗練された解決策ではありませんが、左端のタブの x 座標が 0 未満の場合は左矢印を表示し、右端のタブの x とその幅の和が画面の幅よりも大きい場合は右のタブを表示できます。

于 2012-06-15T13:55:10.570 に答える