Android Toolbar slides along with ListView to hide and reality, androidlistview

Source: Internet
Author: User

Android Toolbar slides along with ListView to hide and reality, androidlistview

Anyone who has used the Google Play Store or Google + app knows that its ActionBar can be hidden or displayed as the ListView slides. The effect looks very good. For this reason, I awkwardly imitated a similar effect and I don't know if there is any better way.

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>
Note that the Toolbar here will be used to replace the original actionBar.

The code in MainActivity. java is as follows:

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;            }        });    }}
The main problems are ListView sliding gesture detection and animations in the Toolbar.

First, replace the original ActionBar with our own Toolbar. Note that in your AppTheme, windowActionbar must be set to false to replace the original one with our own one, otherwise, an error is reported, and a headerView such as Listview and Toolbar is given. Then set the Touch event listening,

In the ACTION_MOVE branch of the onTouch method, we calculate the changes in the position of the trigger move event and the previous trigger move or down event-mDeltaY, then calculate a corresponding translationY. After comparison with the Toolbar height, determine whether the new translationY is valid and valid. Then, use the setTranslationY method to assign a value to the Toolbar.

Trigger UP event:

When the UP event is triggered, we need to use an animation to overdo it. First, judge the direction of the slide. If the direction is upward, the slide is upward. If the direction is downward, the slide is downward.

The android studio project code is here, but it is the Android studio code and does not write the ADT code.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.