nana4ever 2012-11-26 06:42 采纳率: 95.8%
浏览 3564
已采纳

如何给滚动视图设置成图像显示?

我使用水平滚动视图,并且在视图上动态的添加项目。如果项目的数量超过能在屏幕上能显示的数量,我想用如下图显示的,用箭头形式水平滚动来显示这些项目。怎么实现呢?
这是我使用的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

  • 写回答

2条回答

  • 问答小助手 2012-11-30 02:09
    关注

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

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考