我有一个 HorizontalScrollView 的子类,我在其中添加多个图像。 我必须在屏幕的左端和右端显示某种指示器,显示左侧和右侧是否还有剩余的项目。当我们到达相关终点时,相应的指示器也应该消失。
有人可以告诉我可用的选项吗?
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) takeparent().getChildAt(takeparent().getChildCount()-1); //this is the last view
int diff = (view.getRight()-(getRight()+getScrollX()));// Calculate the scrolldiff
if( diff == 0 ){ // if diff is zero, then the rightmost end has been reached
right.setVisibility(View.GONE); //setting the right button invisible
}
View viewleft = (View) takeparent().getChildAt(0); taking the first View
int diffleft2 = (viewleft.getLeft()-(getLeft()+getScrollX()));// Calculate the scrolldiff
if( diffleft2 == 0 ){ // if diff is zero, then the bottom has been reached
left.setVisibility(View.GONE);
}
super.onScrollChanged(l, t, oldl, oldt);
}
最佳答案
这是您现在修改的问题的答案:
@Override
public boolean canScrollHorizontally(int direction)
{
final int offset = computeHorizontalScrollOffset();
final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
if(range == 0){
return false;
}
return (direction < 0) ? (offset > 0) : (offset < range - 1);
}
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
left .setVisibility(canScrollHorizontally(-1) ? View.VISIBLE : View.GONE);
right.setVisibility(canScrollHorizontally( 1) ? View.VISIBLE : View.GONE);
}
我建议使用 Drawables 而不是使用 Views。
因此,我扩展了 HorizontalScrollView
并重写了 draw()
方法,以将其自己的 Drawable
绘制到 canvas
作为指标。其余部分只是使用 setBounds
管理它们的位置,现在是按下状态。
代码:https://gist.github.com/slightfoot/4c017f776663bf6dd5d8#file-indicatedhorizontalscrollview-java
关于java - 当我们到达 HorizontalScrollView 中的最后一项时指示器消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393365/