Android - 带箭头的水平列表视图

时间:2012-03-30 07:36:32

标签: java android listview

我正在尝试使用两个箭头创建一个水平列表视图,一个在左侧移动,一个在右侧移动。

我希望这些箭头仅在滚动太大而无法一体显示时显示。

我有以下布局:

    <LinearLayout 
        android:id="@+id/availability_ctx_right_col"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout 
            android:id="@+id/availability_ctx_level1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#706969"
            android:orientation="horizontal">

            <ImageView 
                android:id="@+id/availability_ctx_level1_left"
                android:src="@drawable/arrow_left"
                android:contentDescription="@string/left"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:visibility="invisible" />

            <ImageView 
                android:id="@+id/availability_ctx_level1_right"
                android:src="@drawable/arrow_right"
                android:contentDescription="@string/right"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:visibility="invisible" />

            <com.my.package.HorizontalListView
                android:id="@+id/availability_ctx_level1_list"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_toRightOf="@id/availability_ctx_level1_left"
                android:layout_toLeftOf="@id/availability_ctx_level1_right" />

        </RelativeLayout >

        <RelativeLayout 
            android:id="@+id/availability_ctx_level2"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#5a4f4f"
            android:orientation="horizontal">

            <ImageView 
                android:id="@+id/availability_ctx_level2_left"
                android:src="@drawable/arrow_left"
                android:contentDescription="@string/left"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:visibility="invisible" />

            <ImageView 
                android:id="@+id/availability_ctx_level2_right"
                android:src="@drawable/arrow_right"
                android:contentDescription="@string/right"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:visibility="invisible" />

            <com.my.package.HorizontalListView
                android:id="@+id/availability_ctx_level2_list"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_toRightOf="@id/availability_ctx_level2_left"
                android:layout_toLeftOf="@id/availability_ctx_level2_right" />

        </RelativeLayout >

        <RelativeLayout 
            android:id="@+id/availability_ctx_level3"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#706969"
            android:orientation="horizontal">

            <ImageView 
                android:id="@+id/availability_ctx_level3_left"
                android:src="@drawable/arrow_left"
                android:contentDescription="@string/left"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:visibility="invisible" />

            <ImageView 
                android:id="@+id/availability_ctx_level3_right"
                android:src="@drawable/arrow_right"
                android:contentDescription="@string/right"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:visibility="invisible" />

            <com.my.package.HorizontalListView
                android:id="@+id/availability_ctx_level3_list"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_toRightOf="@id/availability_ctx_level3_left"
                android:layout_toLeftOf="@id/availability_ctx_level3_right" />

        </RelativeLayout >

    </LinearLayout>

我的Horizo​​ntalListView类完全正常工作并扩展了AdapterView<ListAdapter>

有没有办法只显示所需的箭头以及如何移动到列表中的下一个项目?

感谢。

问候。

2 个答案:

答案 0 :(得分:1)

我终于用一点点黑客解决了我的问题:

    private int item_width = 70; // Width in pixel

    public void scrollLeft()
    {
        mNextX -= item_width;
        mScroller.fling(mNextX, 0, -50, 0, 0, mMaxX, 0, 0);
        requestLayout();
    }

    public void scrollRight()
    {
        mNextX += item_width;
        mScroller.fling(mNextX, 0, -50, 0, 0, mMaxX, 0, 0);
        requestLayout();
    }

如果它可以帮助某人......

答案 1 :(得分:0)

将下面的代码添加到Horizo​​ntalListView类中,在动作事件上实现它;你可以滚动你的horizo​​ntalListView,就像viewPager可以做的那样

/**
 * scrollLeft : scroll the list to left
 */
public void scrollToLeft() {
    mNextX -= getMeasuredWidth();
    mScroller.fling(mNextX, 0, -50, 0, 0, mMaxX, 0, 0);
    requestLayout();
}

/**
 * scrollRight : scroll the list to right
 */
public void scrollToRight() {
    mNextX += getMeasuredWidth();
    mScroller.fling(mNextX, 0, -50, 0, 0, mMaxX, 0, 0);
    requestLayout();
}