天天看點

短視訊APP制作開發,滑動顯示按鈕,點選按鈕置頂

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ScrollView
        android:id="@+id/my_scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Hello 程式員小冰"
                android:textSize="20dp" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Android Dev Team"
                android:textSize="20dp" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="http://weibo.com/mcxiaobing"
                android:textSize="20dp" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="qq986945193"
                android:textSize="20dp" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Hello IOS" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="java開發者"
                android:textSize="20dp" />
 
            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="Android開發者"
                android:textSize="20dp" />
        </LinearLayout>
    </ScrollView>
 
    <Button
        android:id="@+id/top_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="6dp"
        android:layout_marginRight="6dp"
        android:background="@mipmap/top_btn_bg"
        android:gravity="center"
        android:text="頂"
        android:visibility="gone" />
</RelativeLayout>
           

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ScrollView scrollView;// scrollView資料清單
    private Button toTopBtn;// 傳回頂部的按鈕
 
 
    private int scrollY = 0;// 标記上次滑動位置
 
    private View contentView;
 
    private final String TAG = "qq986945193";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
 
    /**
     * 初始化視圖
     */
    private void initView() {
        scrollView = (ScrollView) findViewById(R.id.my_scrollView);
        if (contentView == null) {
            contentView = scrollView.getChildAt(0);
        }
 
        toTopBtn = (Button) findViewById(R.id.top_btn);
        toTopBtn.setOnClickListener(this);
 
        /******************** 監聽ScrollView滑動停止 *****************************/
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            private int lastY = 0;
            private int touchEventId = -9983761;
            Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    View scroller = (View) msg.obj;
                    if (msg.what == touchEventId) {
                        if (lastY == scroller.getScrollY()) {
                            handleStop(scroller);
                        } else {
                            handler.sendMessageDelayed(handler.obtainMessage(
                                    touchEventId, scroller), 5);
                            lastY = scroller.getScrollY();
                        }
                    }
                }
            };
 
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    handler.sendMessageDelayed(
                            handler.obtainMessage(touchEventId, v), 5);
                }
                return false;
            }
 
            /**
             * ScrollView 停止
             *
             * @param view
             */
            private void handleStop(Object view) {
 
                Log.i(TAG, "handleStop");
                ScrollView scroller = (ScrollView) view;
                scrollY = scroller.getScrollY();
 
                doOnBorderListener();
            }
        });
        /***********************************************************/
 
    }
 
    /**
     * ScrollView 的頂部,底部判斷:
     * <p/>
     * 其中getChildAt表示得到ScrollView的child View, 因為ScrollView隻允許一個child
     * view,是以contentView.getMeasuredHeight()表示得到子View的高度,
     * getScrollY()表示得到y軸的滾動距離,getHeight()為scrollView的高度。
     * 當getScrollY()達到最大時加上scrollView的高度就的就等于它内容的高度了啊~
     *
     * @param
     */
    private void doOnBorderListener() {
        // 底部判斷
        if (contentView != null
                && contentView.getMeasuredHeight() <= scrollView.getScrollY()
                + scrollView.getHeight()) {
            toTopBtn.setVisibility(View.VISIBLE);
            Log.i(TAG, "bottom");
        }
        // 頂部判斷
        else if (scrollView.getScrollY() == 0) {
 
            Log.i(TAG, "top");
        } else if (scrollView.getScrollY() > 30) {
            toTopBtn.setVisibility(View.VISIBLE);
            Log.i(TAG, "test");
        }
 
    }
 
    /**
     * 下面我們看一下這個函數: scrollView.fullScroll(ScrollView.FOCUS_DOWN);滾動到底部
     * scrollView.fullScroll(ScrollView.FOCUS_UP);滾動到頂部
     * <p/>
     * <p/>
     * 需要注意的是,該方法不能直接被調用 因為Android很多函數都是基于消息隊列來同步,是以需要一部操作,
     * addView完之後,不等于馬上就會顯示,而是在隊列中等待處理,雖然很快, 但是如果立即調用fullScroll,
     * view可能還沒有顯示出來,是以會失敗 應該通過handler在新線程中更新
     * <p/>
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.top_btn:
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
//                        scrollView.fullScroll(ScrollView.FOCUS_DOWN);滾動到底部
//                        scrollView.fullScroll(ScrollView.FOCUS_UP);滾動到頂部
//
//                        需要注意的是,該方法不能直接被調用
//                        因為Android很多函數都是基于消息隊列來同步,是以需要一部操作,
//                        addView完之後,不等于馬上就會顯示,而是在隊列中等待處理,雖然很快,但是如果立即調用fullScroll, view可能還沒有顯示出來,是以會失敗
//                                應該通過handler在新線程中更新
                        scrollView.fullScroll(ScrollView.FOCUS_UP);
                    }
                });
                toTopBtn.setVisibility(View.GONE);
                break;
        }
    }
 
}           

繼續閱讀