简体   繁体   中英

Android - Horizontal List View with arrows

I'm trying to make a Horizontal List View with two arrows, one to move on the left, one to move on the right.

I want those arrows to be displayed only when the scroll is too large to be displayed in one piece.

I've the following layout:

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

My HorizontalListView class is fully working and extends AdapterView<ListAdapter> .

Is there a way to only display the arrow needed and how to move to next item in the list?

Thanks.

Regards.

I finally solved my problem with a little hack:

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

If it can help someone...

Add the code belows into the HorizontalListView class, implement it on your action event; you can scroll your horizontalListView like what viewPager can do

/**
 * 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM