import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;
/**
* CoordinatorLayout Behavior for a quick return footer
*
* When a nested ScrollView is scrolled down, the quick return view will disappear.
* When the ScrollView is scrolled back up, the quick return view will reappear.
*
* @author bherbst
*/
@SuppressWarnings("unused")
public class QuickReturnFooterBehavior extends CoordinatorLayout.Behavior<View> {
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private int mDySinceDirectionChange;
public QuickReturnFooterBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
if (dy > 0 && mDySinceDirectionChange < 0
|| dy < 0 && mDySinceDirectionChange > 0) {
// We detected a direction change- cancel existing animations and reset our cumulative delta Y
child.animate().cancel();
mDySinceDirectionChange = 0;
}
mDySinceDirectionChange += dy;
if (mDySinceDirectionChange > child.getHeight() && child.getVisibility() == View.VISIBLE) {
hide(child);
} else if (mDySinceDirectionChange < 0 && child.getVisibility() == View.GONE) {
show(child);
}
}
/**
* Hide the quick return view.
*
* Animates hiding the view, with the view sliding down and out of the screen.
* After the view has disappeared, its visibility will change to GONE.
*
* @param view The quick return view
*/
private void hide(final View view) {
ViewPropertyAnimator animator = view.animate()
.translationY(view.getHeight())
.setInterpolator(INTERPOLATOR)
.setDuration(200);
animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {}
@Override
public void onAnimationEnd(Animator animator) {
// Prevent drawing the View after it is gone
view.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animator) {
// Canceling a hide should show the view
show(view);
}
@Override
public void onAnimationRepeat(Animator animator) {}
});
animator.start();
}
/**
* Show the quick return view.
*
* Animates showing the view, with the view sliding up from the bottom of the screen.
* After the view has reappeared, its visibility will change to VISIBLE.
*
* @param view The quick return view
*/
private void show(final View view) {
ViewPropertyAnimator animator = view.animate()
.translationY(0)
.setInterpolator(INTERPOLATOR)
.setDuration(200);
animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
view.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animator animator) {}
@Override
public void onAnimationCancel(Animator animator) {
// Canceling a show should hide the view
hide(view);
}
@Override
public void onAnimationRepeat(Animator animator) {}
});
animator.start();
}
}
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_behavior="com.mb.widget.QuickReturnFooterBehavior"
android:background="@color/quickreturn_background"
android:gravity="center"
android:padding="16dp"
android:text="QuickReturn Footer"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@android:color/white" />
</android.support.design.widget.CoordinatorLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(this,Arrays.asList(Cheeses.sCheeseStrings)));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
SwipeDismissBehavior用法及实现原理
http://www.open-open.com/lib/view/open1446609550545.html
https://github.com/hugeterry/CoordinatorTabLayout
- 大小: 38.7 KB
分享到:
相关推荐
CoordinatorLayout与Behavior的实际使用示例
CoordinatorLayout 是 Google 在 Design Support 包中提供的一个十分强大的布局视图,它允许开发者通过制定 Behavior 从而实现各种复杂的 UI 效果。工程导入可以直接运行。。。
Coordinatorlayout嵌套滑动,自定义Behavior的Demo
该项目是为了练习-> 使用CoordinatorLayout.Behavior 实现页面复杂联动效果 代码模仿实现美团商家详情界面内容联动 开发使用知识点顺带涉及到: Scroller+Handler 实现View自动滑动 View属性动画 触摸事件分发机制 ...
博客http://blog.csdn.net/qibin0506/article/details/50290421的是实例代码。
该项目是为了练习-> 使用CoordinatorLayout.Behavior 实现页面复杂联动效果 代码模仿实现美团商家详情界面内容联动 download apk 开发使用知识点顺带涉及到: Scroller Handler 实现View自动滑动 View属性动画 ...
上次简单的说了一下CoordinatorLayout的基本用法(android特性之CoordinatorLayout用法探析实例)。其中CoordinatorLayout给我们提供了一种新的事件的处理方式,Behavior。还记得那一串字符串吗? app:layout_...
CoordinatorLayout.Behavior的示例程序。 惊人的行为 这是CoordinatorLayout行为的示例。 它包含具有自定义行为的可拖动TextView。 自定义行为将使对象无论在何处都遵循可拖动的“ TextView”。 屏幕截图 每个人都在...
通过实现toolbar,image,recyclerview的behavior打造出下拉显示图片,上滑隐藏toolbar的控件
自定义Behavior Demo,可以结合任何View使用,可以自定义动画等!详解请移步严振杰的博客:http://blog.csdn.net/yanzhenjie1003/article/details/52205665
CoordinatorLayout+Behavior自定义和仿系统的协调布局
自定义behavior监听滚动条的滑动事件 ,对应博客文章为:https://blog.csdn.net/gaoxiaoweiandy/article/details/88375509
使用FloatingActionButton和CoordinatorLayout两个自定义Behavior的示例。 缩小和旋转
文章大纲引言一、CoordinatorLayout核心角色1、CoordinatorLayout.LayoutParams2、CoordinatorLayout.Behavior2、CoordinatorLayout.Behavior核心方法2.1、layoutDependsOn方法2.2、onDependentViewChanged方法2.3、...
Simple View Behavior for Android CoordinatorLayout. This library provide you easy ways to config and setup the view behavior without creating subclass of Behavior Installation compile '...
and add the attribute app:layout_behavior="@string/dependent_view_behavior" in the scroll view to let it scroll along header. Note: You can use DependentViewBehavior on other views as well, and...
自定义CoordinatorLayout的Behavior,实现QQ联系人tab和header动画 效果 大概思路 布局 主要分三部分,header TabLayout ViewPager,由于CoordinatorLayout继承自ViewGroup,类似FrameLayout,所以需要将子View定位...
CoordinatorLayout进阶教程-自定义Behavior实例之标题栏滑动抽屉效果 CoordinatorLayout进阶教程-自定义Behavior实例之标题栏透明度渐变效果 CoordinatorLayout+RecyclerView实例 CoordinatorLayout+TabLayout+...