the*_*yro 5 android android-scrollview
我搜遍了所有,但找不到解决方案.
我在scrollview中有一个视图(让我们称之为myView).myView比屏幕大.由于我能够在myView中获取手指的相对 x,y位置,因此当我的手指进入某个上/下阈值时,我想让scrollView自动滚动到顶部/底部.我有一些想法,即将拖动位置转换为屏幕位置,但这并没有解决这个问题.
提前致谢
干杯
the*_*yro 10
好吧我自己弄清楚了.
首先,我必须扩展ScrollView类并添加一个接口OnScrollViewListener.
public class MyScrollView extends ScrollView {
private OnScrollViewListener mListener;
public MyScrollView(Context c, AttributeSet attrs) {
super(c, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mListener != null) {
mListener.onScrollChanged((OnScrollViewListener) this);
}
}
public void setOnScrollViewListener(OnScrollViewListener listener) {
mListener = listener;
}
public static interface OnScrollViewListener {
public void onScrollChanged(OnScrollViewListener listener);
}
}
Run Code Online (Sandbox Code Playgroud)
接下来在我的Activity中插入了一个成员mScrollDistance,它指示用户滚动的像素数量.
public class ScrollActivity extends Activity {
private int mScrollDistance;
@Override
protected void OnCreate(...) {
...
final MyScrollView myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
myScrollView.setOnScrollViewListener(new MyScrollView.OnScrollViewListener() {
public void onScrollChanged(OnScrollViewListener listener) {
mScrollDistance = listener.getScrollY();
}
}
// making an drag and drop in an view that is inside the MyScrollView
final LinearLayout myLayout = (LinearLayout)findViewById(R.id.linear_layout);
myLayout.setOnDragListener(new View.OnDragListener() {
public boolean onDrag (View v, DragEvent event) {
int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED: {
}
case DragEvent.ACTION_DRAG_LOCATION: {
int y = Math.round(event.getY());
int translatedY = y - mScrollDistance;
int threshold = 50;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold) {
// make a scroll up by 30 px
myScrollView.scrollBy(0, -30);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 500) {
// make a scroll down by 30 px
myScrollView.scrollBy(0, 30);
}
// listen for more actions here
// ...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,mScrollDistance总是获得一个新值,拖动位置将被转换为视图位置.我对此进行了测试,它适用于大于屏幕尺寸的布局/视图.
希望有所帮助.
我想出了一个不同的解决方案,对此我感到满意。
我希望能够在ScrollView中拖放视图。然后,当阴影到达滚动视图的边缘时,ScrollView需要自动上下滚动。
最后,我找到了一个解决方案,该解决方案可以检测到拖放区是否在滚动视图内部完全可见(边距为100像素),否则可以调整滚动视图。
@Override
public boolean onDrag(View view, DragEvent event) {
MainWidget dropZoneView = (MainWidget) view;
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
//(... other stuff happens here)
case DragEvent.ACTION_DRAG_LOCATION:
ScrollView mainScrollView = (ScrollView) findViewById(R.id.main_scroll);
int topOfDropZone = dropZoneView.getTop();
int bottomOfDropZone = dropZoneView.getBottom();
int scrollY = mainScrollView.getScrollY();
int scrollViewHeight = mainScrollView.getMeasuredHeight();
Log.d(LOG_TAG,"location: Scroll Y: "+ scrollY + " Scroll Y+Height: "+(scrollY + scrollViewHeight));
Log.d(LOG_TAG," top: "+ topOfDropZone +" bottom: "+bottomOfDropZone);
if (bottomOfDropZone > (scrollY + scrollViewHeight - 100))
mainScrollView.smoothScrollBy(0, 30);
if (topOfDropZone < (scrollY + 100))
mainScrollView.smoothScrollBy(0, -30);
break;
default:
break;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
9863 次 |
| 最近记录: |