51.Android 简单的RecyclerView滑动,通过动画显示隐藏toolbar和悬浮按钮

导读:本篇文章讲解 51.Android 简单的RecyclerView滑动,通过动画显示隐藏toolbar和悬浮按钮,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

//图片不是动态的凑合看吧 

51.Android 简单的RecyclerView滑动,通过动画显示隐藏toolbar和悬浮按钮

 

//介绍 两种方式来滑动隐藏悬浮按钮

//————————————-第一种————————————-

//首先初始化

private int distance;
private boolean visible = true;

//第二步 直接代码中使用

mRe = (RecyclerView) findViewById(R.id.mRe);
imageButton = (ImageButton) findViewById(R.id.imageButton);
mRe.setLayoutManager(new LinearLayoutManager(this));
FabRecyclerAdapter adapter = new FabRecyclerAdapter(mList);
mRe.setAdapter(adapter);
mRe.setOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if(distance < -ViewConfiguration.getTouchSlop() && !visible){
            //显示
//这里传你的悬浮按钮,我用的imagebutton
            showFABAnimation(imageButton);
            distance = 0;
            visible = true;
        }else if(distance > ViewConfiguration.getTouchSlop() && visible){
            //隐藏 
//这里传你的悬浮按钮,我用的imagebutton
            hideFABAnimation(imageButton);
            distance = 0;
            visible = false;
        }
        if ((dy > 0 && visible) || (dy < 0 && !visible))//向下滑并且可见  或者  向上滑并且不可见
            distance += dy;
    }
});

 

//第三步 显示隐藏的动画

//显示的动画
public void showFABAnimation(View view)
{
    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f);
    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f);
    PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f);
    ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(400).start();
}

//隐藏的动画

public void hideFABAnimation(View view)
{
    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 0f);
    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0f);
    PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0f);
    ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(400).start();
}

 

 

//————————————第二种————————————–

//第一步 写布局(我的布局)RecyclerView,toolbar ,ImageView做悬浮按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hasee.a823ceshi.Main4Activity">
    
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRe"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="曹二少"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    
    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="17dp"
        android:layout_marginEnd="26dp"
        android:layout_marginRight="26dp"
        android:background="#00ffffff"
        app:srcCompat="?android:attr/textSelectHandle" />
</RelativeLayout>

//注意,我的这个RecyclerView布局里的属性必须加,不然会出现,滑动后,toolbar虽然消失,但是,数据列表并不能顶到最上面,必须加这三句:

android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize"

//第二步写接口 放一个显示的方法,一个隐藏的方法

public interface HideScrollListener {
    void onHide();

    void onShow();
}

//第三步 写个FabScrollListener类继承RecyclerView.OnScrollListener,重写滑动监听方法

public class FabScrollListener extends  RecyclerView.OnScrollListener{
    private HideScrollListener listener;
    private static final int THRESHOLD = 20;
    private int distance = 0;
    private boolean visible = true;//是否可见

    public FabScrollListener(HideScrollListener listener) {
        this.listener = listener;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (distance > THRESHOLD && visible) {
            //隐藏动画
            visible = false;
            listener.onHide();
            distance = 0;
        } else if (distance < -20 && !visible) {
            //显示动画
            visible = true;
            listener.onShow();
            distance = 0;
        }

        if (visible && dy > 0 || (!visible && dy < 0)) {
            distance += dy;
        }
    }
}

 

//第四步 在Activity里使用,注意toolbar要用v7包,

//import android.support.v7.widget.Toolbar;
public class Main4Activity extends AppCompatActivity implements HideScrollListener {

    private RecyclerView mRe;
    private ImageButton imageButton;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        initView();
    }

    private void initView() {
        mRe = (RecyclerView) findViewById(R.id.mRe);
        imageButton = (ImageButton) findViewById(R.id.imageButton);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

//添加数据
        ArrayList mList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            mList.add("曹二少" + i);
        }

        mRe.setLayoutManager(new LinearLayoutManager(this));
        FabRecyclerAdapter adapter = new FabRecyclerAdapter(mList);
        mRe.setAdapter(adapter);
        //监听
        mRe.addOnScrollListener(new FabScrollListener(this));
        

    }


    @Override
    public void onHide() {
        //隐藏动画
        toolbar.animate().translationY(-toolbar.getHeight()).setInterpolator(new AccelerateInterpolator(3));
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageButton.getLayoutParams();
        imageButton.animate().translationY(imageButton.getHeight() + layoutParams.bottomMargin).setInterpolator(new AccelerateInterpolator(3));
    }


    @Override
    public void onShow() {
         // 显示动画
        
        //这个方法用来  判断你的RecyclerView是否滑到第一条,就是顶部,只有到顶部才让toolbar显示出来
        mRe.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.i("TAG", "--------------------------------------");
                if(mRe.canScrollVertically(1)){
                    Log.i("TAG", "direction 1: true");
                }else {
                    Log.i("TAG", "direction 1: false");//滑动到底部
                }
                if(mRe.canScrollVertically(-1)){
                    Log.i("TAG", "direction -1: true");
                }else {
                    //显示toolbar
                    toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));
                    Log.i("TAG", "direction -1: false");//滑动到顶部
                }
            }
        });
        //也可以设置只要一往下滑就显示出来toolbar  看情况而定吧
//        toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageButton.getLayoutParams();
        imageButton.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3));
    }

}

//———————————————————————完——————————————————————————

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118343.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!