public class MainActivity extends Activity {
private TextView tvdong;
private TextView tvjing;
private ScrollView sv;
int[] location = new int[2];
int[] location2 = new int[2];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
//在滚动的一个TextView
tvdong = (TextView) findViewById(R.id.dong);
//一开始是View.GONE的一个静止的TextView
tvjing = (TextView) findViewById(R.id.jing);
sv = (ScrollView) findViewById(R.id.eee);
tvjing.setVisibility(View.GONE);
sv.setOnTouchListener(new OnTouchListener() {
private int lastY = 0;
private int touchEventId = -9983761;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == touchEventId) {
if (lastY != sv.getScrollY()) {
//scrollview一直在滚动,会触发
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, sv), 5);
lastY = sv.getScrollY();
tvdong.getLocationOnScreen(location);
tvjing.getLocationOnScreen(location2);
//动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个
if (location[1] <= location2[1]) {
tvjing.setVisibility(View.VISIBLE);
} else {
//静止的隐藏了
tvjing.setVisibility(View.GONE);
}
}
}
}
};
public boolean onTouch(View v, MotionEvent event) {
//必须两个都搞上,不然会有瑕疵。
//没有这段,手指按住拖动的时候没有效果
if (event.getAction() == MotionEvent.ACTION_MOVE) {
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, v), 5);
}
//没有这段,手指松开scroll继续滚动的时候,没有效果
if (event.getAction() == MotionEvent.ACTION_UP) {
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, v), 5);
}
return false;
}
});
}
}[/mw_shl_code]
[mw_shl_code=xhtml,true]<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/eee"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF8800"
android:text="\n这 \n 是 \n 展 \n 示\n图\n片\n\n" />
<TextView
android:id="@+id/dong"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008833"
android:text="\n\n这 是 滚 动 的 等 一 下 固 定 在 上 面\n\n" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff33"
android:text="~~~~~~\n\n\n\n这\n\n\n\n\n\n是\n\n\n\n\n\n其\n\n\n\n\n他\n\n\n\n\n\n\n的\n\n\n~~~~~~~" />
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/jing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008800"
android:text="\n\n这 是 固 定 的\n\n" />
</FrameLayout>[/mw_shl_code]
private TextView tvdong;
private TextView tvjing;
private ScrollView sv;
int[] location = new int[2];
int[] location2 = new int[2];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
//在滚动的一个TextView
tvdong = (TextView) findViewById(R.id.dong);
//一开始是View.GONE的一个静止的TextView
tvjing = (TextView) findViewById(R.id.jing);
sv = (ScrollView) findViewById(R.id.eee);
tvjing.setVisibility(View.GONE);
sv.setOnTouchListener(new OnTouchListener() {
private int lastY = 0;
private int touchEventId = -9983761;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == touchEventId) {
if (lastY != sv.getScrollY()) {
//scrollview一直在滚动,会触发
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, sv), 5);
lastY = sv.getScrollY();
tvdong.getLocationOnScreen(location);
tvjing.getLocationOnScreen(location2);
//动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个
if (location[1] <= location2[1]) {
tvjing.setVisibility(View.VISIBLE);
} else {
//静止的隐藏了
tvjing.setVisibility(View.GONE);
}
}
}
}
};
public boolean onTouch(View v, MotionEvent event) {
//必须两个都搞上,不然会有瑕疵。
//没有这段,手指按住拖动的时候没有效果
if (event.getAction() == MotionEvent.ACTION_MOVE) {
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, v), 5);
}
//没有这段,手指松开scroll继续滚动的时候,没有效果
if (event.getAction() == MotionEvent.ACTION_UP) {
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, v), 5);
}
return false;
}
});
}
}[/mw_shl_code]
[mw_shl_code=xhtml,true]<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/eee"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF8800"
android:text="\n这 \n 是 \n 展 \n 示\n图\n片\n\n" />
<TextView
android:id="@+id/dong"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008833"
android:text="\n\n这 是 滚 动 的 等 一 下 固 定 在 上 面\n\n" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff33"
android:text="~~~~~~\n\n\n\n这\n\n\n\n\n\n是\n\n\n\n\n\n其\n\n\n\n\n他\n\n\n\n\n\n\n的\n\n\n~~~~~~~" />
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/jing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008800"
android:text="\n\n这 是 固 定 的\n\n" />
</FrameLayout>[/mw_shl_code]