如何解决滚动条滑出屏幕的问题

cnaaa8 2014-11-26 05:57:32
动态移动ScrollView中某个item的位置,如何解决滚动条滑出屏幕的问题




xml文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scrollview.MainActivity" >

<com.example.scrollview.ObservableScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sj" />

<TextView
android:id="@+id/yidong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="移动的标题" />

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sj" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其让啦啦啦" />
</LinearLayout>
</com.example.scrollview.ObservableScrollView>

</LinearLayout>



main文件



package com.example.scrollview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

int[] location = new int[2];
int[] locationSv = new int[2];
private TextView yidong;
private ObservableScrollView sv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = (ObservableScrollView) findViewById(R.id.sv);
yidong = (TextView) findViewById(R.id.yidong);

sv.setScrollViewListener(new ScrollViewListener() {

@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
// TODO Auto-generated method stub
yidong.getLocationOnScreen(location);
sv.getLocationOnScreen(locationSv);
sv.top=(location[1]-locationSv[1]);
//sv.v=yidong;
}
});
}


}



接口文件


package com.example.scrollview;

public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}


自定义 ScrollView文件


package com.example.scrollview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {

private ScrollViewListener scrollViewListener = null;
public int top=Integer.MAX_VALUE;
public View v;

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

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

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

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}

@Override
protected void dispatchDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.dispatchDraw(canvas);

if(top<=0 && v!=null){
int pLeft = getPaddingLeft();
int pTop = getPaddingTop();
canvas.translate(pLeft, pTop - top);
//smoothScrollTo(pLeft, pTop - top);
drawChild(canvas, v, getDrawingTime());
}

}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}

}


...全文
213 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36969860 2017-07-04
  • 打赏
  • 举报
回复
私聊我发你。。。。
cnaaa8 2014-11-27
  • 打赏
  • 举报
回复
引用 1 楼 ouyewang 的回复:
明早发给你呵
可以了吗
ouye猿 2014-11-26
  • 打赏
  • 举报
回复
明早发给你呵

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧