程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> scrollview-如何給滾動視圖設置成圖像顯示?

scrollview-如何給滾動視圖設置成圖像顯示?

編輯:編程綜合問答
如何給滾動視圖設置成圖像顯示?

我使用水平滾動視圖,並且在視圖上動態的添加項目。如果項目的數量超過能在屏幕上能顯示的數量,我想用如下圖顯示的,用箭頭形式水平滾動來顯示這些項目。怎麼實現呢?
這是我使用的XML代碼:

<HorizontalScrollView 
   android:layout_width="wrap_content"
   android:scrollbars="none"
   android:id="@+id/app_category"
   android:layout_below="@+id/top_layout"   
   android:background="@drawable/minitopbar"
   android:layout_height="30dp">

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

</HorizontalScrollView>

enter image description here

最佳回答:


A.你應該創建自己的類,然後繼承HorizontalScrollView

public class ExtendedHorizontalScrollView extends HorizontalScrollView {

private IScrollStateListener scrollStateListener;

public HorizontalScrollViewForMenu(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public HorizontalScrollViewForMenu(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public HorizontalScrollViewForMenu(Context context) {
    super(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    prepare();
}

private void prepare() {
    if (scrollStateListener != null) {
        View content = this.getChildAt(0);
        if (content.getLeft() >= 0)
            scrollStateListener.onScrollMostLeft();
        if (content.getLeft() < 0)
            scrollStateListener.onScrollFromMostLeft();

        if (content.getRight() <= getWidth())
            scrollStateListener.onScrollMostRight();
        if (content.getLeft() > getWidth())
            scrollStateListener.onScrollFromMostRight();
    }
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (scrollStateListener != null) {
        if (l == 0)
            scrollStateListener.onScrollMostLeft();
        else if (oldl == 0)
            scrollStateListener.onScrollFromMostLeft();
        int mostRightL = this.getChildAt(0).getWidth() - getWidth();
        if (l >= mostRightL)
            scrollStateListener.onScrollMostRight();
        if (oldl >= mostRightL && l < mostRightL)
            scrollStateListener.onScrollFromMostRight();
    }
}

public void setScrollStateListener(IScrollStateListener listener) {
    scrollStateListener = listener;
}

public interface IScrollStateListener {
    void onScrollMostLeft();

    void onScrollFromMostLeft();

    void onScrollMostRight();

    void onScrollFromMostRight();
}
}

B.使用它來定義布局

<LinearLayout
      .....>
    <ImageView
        android:id="@+id/navigation_left"
        ..... />

    <your.custom.view.package.ExtendedHorizontalScrollView
        android:id="@+id/scroller"
        android:layout_width="0px"
        android:layout_weight="1"
        android:fadingEdge="none"
                ....>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </your.custom.view.package.ExtendedHorizontalScrollView>

    <ImageView
        android:id="@+id/navigation_right"
        ..... />

</LinearLayout>

C.當不能水平滾動的時候,給箭頭添加以下邏輯:

((ExtendedHorizontalScrollView)findViewById(R.id.scroller)).setScrollStateListener(new IScrollStateListener() {
        public void onScrollMostRight() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.INVISIBLE);
        }

        public void onScrollMostLeft() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.INVISIBLE);
        }

        public void onScrollFromMostLeft() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.VISIBLE);
        }

        public void onScrollFromMostRight() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.VISIBLE);
        }

    });
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved