【问题标题】:How to set images for ScrollView instead-of fading edges?如何为 ScrollView 设置图像而不是淡化边缘?
【发布时间】:2012-02-22 03:36:06
【问题描述】:

我正在使用水平滚动视图,我将动态添加项目。如果不。项目不仅仅是在屏幕上显示项目我想在边缘显示图像(箭头)(如滚动视图显示褪色边缘)。我该怎么做。

看看下面的图片,我想要这样创建。

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

【问题讨论】:

  • 添加图片是什么意思?任何图像代替边缘的绿色?或者喜欢将图像添加到线性布局?
  • 如果滚动视图有要滚动的项目,则在边缘替换绿色的任何图像
  • 具有相同的宽度和高度等于绿色?我的建议是您可以根据需要去除褪色边缘的颜色。
  • 我将删除褪色边缘,现在我想在那个地方设置图像..

标签: android scrollview fading


【解决方案1】:

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

    });

【讨论】:

  • 我正在使用自定义水平列表视图而不是滚动视图..它适合那个..
  • 是的,只需覆盖列表视图的onScrollChanged 并查看是否可以滚动更多 - 如果是 - 显示箭头
  • 之所以使用它是因为ScrollView 不允许很多孩子。如果你使用ListView - 你不需要它
  • 实际上我首先使用了滚动视图,由于 UI 中的某些原因,现在我更改为自定义 Horizo​​ntalListView,现在我想在边缘设置箭头..我该怎么做。如果你想告诉我,我会展示完整的自定义类..
  • 只需使用我的onScrollChangesetScrollStateListener 方法,IScrollStateListener 将其放入您的ListView,并设置逻辑以在ActivityonCreate 中显示箭头,例如(YourCustomListView)findViewById(R.id.list).setScrollStateListener(...)
【解决方案2】:

一个简单的替代方法是制作一个不褪色边缘的滚动视图,然后在滚动视图下方或顶部添加带有箭头的图像。

你的 xml 类似:

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:fadingEdge="none"
        />
        <RelativeLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <View
                android:layout_height="6dp"
                android:layout_width="fill_parent"
                android:layout_alignParentBottom="true"
                android:background="@drawable/background_effect"/>
        </RelativeLayout>
    </FrameLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 2011-02-08
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多