安卓属性动画,listview上滑隐藏头部布局



package text.htss.com.demo_shuxing_anim;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewPropertyAnimator;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * 属性动画
 */
public class MainActivity extends AppCompatActivity {

    private boolean ismove = true;
    private ViewPropertyAnimator animatebottom, animatetop;
    private ListView mListView;
    private LinearLayout tabbottom;
    private LinearLayout tabtop;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        //属性动画
        animatebottom = tabbottom.animate();
        animatetop = tabtop.animate();
        ArrayAdapter<String> adapter = initDatas();
        mListView.setAdapter(adapter);
        setListener();
    }

    private void setListener() {
        mListView.setOnTouchListener(new OnTouchListener() {

            private float y;
            private boolean down = true;
            private float lasty;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        y = event.getY();
                        if (down) {
                            lasty = y;
                        }
                        down = false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        down = true;
                        if (lasty - event.getY() < -5) {
                            onScrollReset();
                        } else if (lasty - event.getY() > 5) {
                            onScroll();
                        }
                        break;
                }

                return false;
            }
        });
    }

    @NonNull
    private ArrayAdapter<String> initDatas() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1);
        for (int i = 0; i < 100; i++) {
            adapter.add(i + "");
        }
        return adapter;
    }

    private void initViews() {
        mListView = (ListView) findViewById(R.id.my_listview);
        tabbottom = (LinearLayout) findViewById(R.id.tabbottom);
        tabtop = (LinearLayout) findViewById(R.id.tabtop);
    }


    @SuppressLint("NewApi")
    public void onScroll() {
        if (ismove) {
            animatebottom.setDuration(500).translationY(200);
            animatetop.setDuration(500).translationY(-200);
            ismove = false;
        }

    }

    @SuppressLint("NewApi")
    public void onScrollReset() {
        if (!ismove) {
            animatebottom.setDuration(500).translationY(0);
            animatetop.setDuration(500).translationY(0);
            ismove = true;
        }
    }


}



activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+id/my_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/tabbottom"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_alignParentBottom="true"
            android:background="#880090D9"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="标题"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tabtop"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:background="#880090D9"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="标题"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>

</FrameLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值