转载请标明出处:
http://blog.csdn.net/xuehuayous/article/details/50394640
本文出自:【Kevin.zhou的博客】
/** * Created by zhouwk on 2015/12/24 0024. */ public class JingDongHeaderLayout extends LoadingLayoutBase{ public JingDongHeaderLayout(Context context) { super(context); } /** * get the LoadingLayout height or width * * @return size */ @Override public int getContentSize() { return 0; } /** * Call when the widget begins to slide */ @Override public void pullToRefresh() { } /** * Call when the LoadingLayout is fully displayed */ @Override public void releaseToRefresh() { } /** * Call when the LoadingLayout is sliding * * @param scaleOfLayout scaleOfLayout */ @Override public void onPull(float scaleOfLayout) { } /** * Call when the LoadingLayout is fully displayed and the widget has released. * Used to prompt the user loading data */ @Override public void refreshing() { } /** * Call when the data has loaded */ @Override public void reset() { } /** * Set Text to show when the Widget is being Pulled * <code>setPullLabel(releaseLabel, Mode.BOTH)</code> * * @param pullLabel - CharSequence to display */ @Override public void setPullLabel(CharSequence pullLabel) { } /** * Set Text to show when the Widget is refreshing * <code>setRefreshingLabel(releaseLabel, Mode.BOTH)</code> * * @param refreshingLabel - CharSequence to display */ @Override public void setRefreshingLabel(CharSequence refreshingLabel) { } /** * Set Text to show when the Widget is being pulled, and will refresh when * released. This is the same as calling * <code>setReleaseLabel(releaseLabel, Mode.BOTH)</code> * * @param releaseLabel - CharSequence to display */ @Override public void setReleaseLabel(CharSequence releaseLabel) { } }看起来一大坨还是不少的,其实就如下几个方法,然后给大家写成人话:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <FrameLayout android:id="@+id/fl_inner" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/header_footer_top_bottom_padding" android:paddingLeft="@dimen/header_footer_left_right_padding" android:paddingRight="@dimen/header_footer_left_right_padding" android:paddingTop="@dimen/header_footer_top_bottom_padding" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/pull_to_refresh_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="让购物更便捷" android:textColor="#5b5b5b" android:textAppearance="?android:attr/textAppearance" /> <TextView android:id="@+id/pull_to_refresh_sub_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="下拉刷新" android:textColor="#5b5b5b" android:textAppearance="?android:attr/textAppearanceSmall"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/pull_to_refresh_people" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:src="@mipmap/app_refresh_people_0" /> <ImageView android:id="@+id/pull_to_refresh_goods" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/pull_to_refresh_people" android:layout_centerVertical="true" android:src="@mipmap/app_refresh_goods_0" /> </RelativeLayout> </FrameLayout> </merge>布局比较简单,效果是这样的:
public JingDongHeaderLayout(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.jingdong_header_loadinglayout, this); mInnerLayout = (FrameLayout) findViewById(R.id.fl_inner); mHeaderText = (TextView) mInnerLayout.findViewById(R.id.pull_to_refresh_text); mSubHeaderText = (TextView) mInnerLayout.findViewById(R.id.pull_to_refresh_sub_text); mGoodsImage = (ImageView) mInnerLayout.findViewById(R.id.pull_to_refresh_goods); mPersonImage = (ImageView) mInnerLayout.findViewById(R.id.pull_to_refresh_people); LayoutParams lp = (LayoutParams) mInnerLayout.getLayoutParams(); lp.gravity = Gravity.BOTTOM; // Load in labels mPullLabel = context.getString(R.string.jingdong_pull_label); mRefreshingLabel = context.getString(R.string.jingdong_refreshing_label); mReleaseLabel = context.getString(R.string.jingdong_release_label); reset(); }大家要注意的是初始化布局,"刷新头部"的时候要加上这个:
LayoutParams lp = (LayoutParams) mInnerLayout.getLayoutParams(); lp.gravity = Gravity.BOTTOM;如果是"加载尾部",就是这样的:
LayoutParams lp = (LayoutParams) mInnerLayout.getLayoutParams(); lp.gravity = Gravity.TOP;
// 获取"加载头部"高度 @Override public int getContentSize() { return mInnerLayout.getHeight(); }
// 开始下拉时的回调 @Override public void pullToRefresh() { mSubHeaderText.setText(mPullLabel); }
// 下拉拖动时的回调 @Override public void onPull(float scaleOfLayout) { scaleOfLayout = scaleOfLayout > 1.0f ? 1.0f : scaleOfLayout; if (mGoodsImage.getVisibility() != View.VISIBLE) { mGoodsImage.setVisibility(View.VISIBLE); } //透明度动画 ObjectAnimator animAlphaP = ObjectAnimator.ofFloat(mPersonImage, "alpha", -1, 1).setDuration(300); animAlphaP.setCurrentPlayTime((long) (scaleOfLayout * 300)); ObjectAnimator animAlphaG = ObjectAnimator.ofFloat(mGoodsImage, "alpha", -1, 1).setDuration(300); animAlphaG.setCurrentPlayTime((long) (scaleOfLayout * 300)); //缩放动画 ViewHelper.setPivotX(mPersonImage, 0); // 设置中心点 ViewHelper.setPivotY(mPersonImage, 0); ObjectAnimator animPX = ObjectAnimator.ofFloat(mPersonImage, "scaleX", 0, 1).setDuration(300); animPX.setCurrentPlayTime((long) (scaleOfLayout * 300)); ObjectAnimator animPY = ObjectAnimator.ofFloat(mPersonImage, "scaleY", 0, 1).setDuration(300); animPY.setCurrentPlayTime((long) (scaleOfLayout * 300)); ViewHelper.setPivotX(mGoodsImage, mGoodsImage.getMeasuredWidth()); ObjectAnimator animGX = ObjectAnimator.ofFloat(mGoodsImage, "scaleX", 0, 1).setDuration(300); animGX.setCurrentPlayTime((long) (scaleOfLayout * 300)); ObjectAnimator animGY = ObjectAnimator.ofFloat(mGoodsImage, "scaleY", 0, 1).setDuration(300); animGY.setCurrentPlayTime((long) (scaleOfLayout * 300)); }
// "加载头部"完全显示时的回调 @Override public void releaseToRefresh() { mSubHeaderText.setText(mReleaseLabel); }
// 释放后刷新时的回调 @Override public void refreshing() { mSubHeaderText.setText(mRefreshingLabel); if (animP == null) { mPersonImage.setImageResource(R.drawable.refreshing_anim); animP = (AnimationDrawable) mPersonImage.getDrawable(); } animP.start(); if (mGoodsImage.getVisibility() == View.VISIBLE) { mGoodsImage.setVisibility(View.INVISIBLE); } }
// 初始化到未刷新状态 @Override public void reset() { if (animP != null) { animP.stop(); animP = null; } mPersonImage.setImageResource(R.mipmap.app_refresh_people_0); if (mGoodsImage.getVisibility() == View.VISIBLE) { mGoodsImage.setVisibility(View.INVISIBLE); } }
@Override public void setPullLabel(CharSequence pullLabel) { mPullLabel = pullLabel; } @Override public void setRefreshingLabel(CharSequence refreshingLabel) { mRefreshingLabel = refreshingLabel; } @Override public void setReleaseLabel(CharSequence releaseLabel) { mReleaseLabel = releaseLabel; }这三个方法不实现也可以,因为我们是通过以下方式初始提示的
mPullLabel = context.getString(R.string.jingdong_pull_label); mRefreshingLabel = context.getString(R.string.jingdong_refreshing_label); mReleaseLabel = context.getString(R.string.jingdong_release_label);
mPullToRefreshRecyclerView.setHeaderLayout(new JingDongHeaderLayout(this));当然也可以设置底部样式:
mPullToRefreshRecyclerView.setFooterLayout(xxx);