之前因爲項目需要做了一個效果,就是當某個操作按鈕滑動頂部是固定在頂部,不讓其隱藏,讓用戶無時無刻都可以實現這個操作。
這個效果不是很神奇,就個人來說呢,很有用。無形間增加了用戶體驗鳥 源代碼:http://download.csdn.net/detail/hcb1230/6593367
直接上圖吧,效果如下:
效果不錯吧,哈哈
一開始 中間深綠色的一條 可以隨着整一塊拖動。
當它到達頂部的時候,能夠靠着狀態欄停留。
往下拖動回到原來位置的時候 , 又可以向下拖走。
有點仿美團團商品頁面那個價格一欄的效果。
因爲自己做項目需要,查了好久 ,基本沒有找到想要的, 就發出來。
想法很簡單。
兩個TextView換成想要固定的控件,就能看上去實現這個效果。
源代碼:http://download.csdn.net/detail/hcb1230/6593367
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;
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;
}
});
}
}
<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>
可以把上面的TextView換成一個LinearLayout神馬的,然後把你想實現的控件全部貼上去。還尅有改變靜止的TextView實現想要停靠的位置。不只是頂部。比如說 ,購物App,把立即購買放在上面,看起來就很有感覺呀。之前一直在想怎麼實現這個,網上找了好多demo都不是自己想要的。後來,突然靈感來了。很簡單的就能實現這個方法了。現在看來效果也還算不錯的。相信你看了代碼就知道是如何實現的了、