Android Toolbar跟隨ListView滑動隱藏和現實

來源:互聯網
上載者:User

標籤:android   listview   android studio   google play   動畫   

使用過Google Play Store應用或者Google+應用的人都知道,其ActionBar能隨著ListView的滑動而相應的隱藏或者顯示。效果看起來很不錯,為此,我笨拙的模仿了一個類似的效果,不知道有沒有更好的辦法。

先上主布局activity_main:

<RelativeLayout 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.beak.music.ui.MainActivity">    <ListView        android:id="@+id/main_list_view"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        />    <android.support.v7.widget.Toolbar        android:id="@+id/main_bar"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@color/std_color_A"        /></RelativeLayout>
注意這裡的Toolbar,我們將會用這個Toolbar來替換原來的的actionBar。

現在是MainActivity.java裡的代碼:

public class MainActivity extends BaseActivity{    private static final String TAG = MainActivity.class.getSimpleName();    private Toolbar mMainToolbar = null;    private ListView mMainListView = null;    private float mStartY = 0, mLastY = 0, mLastDeltaY;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mMainToolbar = (Toolbar)this.findViewById(R.id.main_bar);        this.setSupportActionBar(mMainToolbar);        mMainListView = (ListView)this.findViewById(R.id.main_list_view);        final View header = LayoutInflater.from(this).inflate(R.layout.layout_header, null);        mMainListView.addHeaderView(header);        mMainListView.setAdapter(new AudioAdapter(this));        mMainListView.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                final float y = event.getY();                float translationY = mMainToolbar.getTranslationY();                switch (event.getAction()) {                    case MotionEvent.ACTION_DOWN://                        Log.v(TAG, "Down");                        mStartY = y;                        mLastY = mStartY;                        break;                    case MotionEvent.ACTION_MOVE:                        float mDeltaY = y - mLastY;                        float newTansY = translationY + mDeltaY;                        if (newTansY <= 0 && newTansY >= -mMainToolbar.getHeight()) {                            mMainToolbar.setTranslationY(newTansY);                        }                        mLastY = y;                        mLastDeltaY = mDeltaY;//                        Log.v(TAG, "Move");                        break;                    case MotionEvent.ACTION_UP:                        ObjectAnimator animator = null;                        Log.d(TAG, "mLastDeltaY=" + mLastDeltaY);                        if (mLastDeltaY < 0 && mMainListView.getFirstVisiblePosition() > 1) {                            Log.v(TAG, "listView.first=" + mMainListView.getFirstVisiblePosition());                            animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), -mMainToolbar.getHeight());                        } else {                            animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), 0);                        }                        animator.setDuration(100);                        animator.start();                        animator.setInterpolator(AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.linear));//                        Log.v(TAG, "Up");                        break;                }                return false;            }        });    }}
主要的問題是ListView滑動手勢檢測和Toolbar裡的動畫。

一開始,先用我們自己的Toolbar替換原來的ActionBar,注意,在你的AppTheme中,windowActionbar這一項要設定為false才能用我們自己的去替換原來的,不然運行會報錯,然後給Listview一個與Toolbar等高的headerView。然後再設定Touch事件的監聽,

在onTouch方法的ACTION_MOVE分支中,我們計算出本次觸發move事件與上次觸發move或者down事件時候,我們的觸發點的位置變化量-mDeltaY,然後計算出一個相應的translationY,經過與Toolbar高度比較,判斷出新的translationY是否合法,合法,則用setTranslationY方法,給Toolbar賦值。

觸發UP事件:

當觸發了UP事件後,就要,我們就要用一個動畫,來過度一下。先判斷滑動方向,方向向上,則向上滑動,向下,則向下滑動。

android studio project code 代碼在這裡,不過是Android studio代碼,沒有寫ADT代碼



Android Toolbar跟隨ListView滑動隱藏和現實

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.