自定义刷新(refreshview)样式

出自  http://www.cnblogs.com/dingchao823/p/4794442.html


PullToRefresh Demo 浅析:自定义刷新(refreshview)样式

PullToRefresh除了可以自己定制刷新的内容之外,最重要的还是在与当用户下拉和上拉时候的样式问题。

打开library源码,发现refreshview的样式定义在了LoadingLayout类中。

public abstract class LoadingLayout extends FrameLayout implements ILoadingLayout {

构造函数中:

复制代码
switch (scrollDirection) {
            case HORIZONTAL:
                LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header_horizontal, this);
                break;
            case VERTICAL:
            default:
                LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_header_vertical, this);
                break;
        }
复制代码

可以看出根据不同的方向选用不同的布局,所以对于自定义refreshview来说,就是重新选择布局。

参考:http://www.2cto.com/kf/201501/368016.html


附带:PullToRefresh的自定义样式属性:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="PullToRefresh">
    <!-- A drawable to use as the background of the Refreshable View -->
    <!-- 设置整个刷新列表的背景色 -->
    <attr name="ptrRefreshableViewBackground" format="reference|color" />
    <!-- A drawable to use as the background of the Header and Footer Loading Views -->
    <!--  设置下拉Header或者上拉Footer的背景色 -->
    <attr name="ptrHeaderBackground" format="reference|color" />
    <!-- Text Color of the Header and Footer Loading Views -->
    <!-- 用于设置Header与Footer中文本的颜色 -->
    <attr name="ptrHeaderTextColor" format="reference|color" />
    <!-- Text Color of the Header and Footer Loading Views Sub Header -->
    <!-- 用于设置Header与Footer中上次刷新时间的颜色 -->
    <attr name="ptrHeaderSubTextColor" format="reference|color" />
    <!-- Mode of Pull-to-Refresh that should be used -->
    <attr name="ptrMode">
      <flag name="disabled" value="0x0" /><!-- 禁用下拉刷新 -->
      <flag name="pullFromStart" value="0x1" /><!-- 仅支持下拉刷新 -->
      <flag name="pullFromEnd" value="0x2" /><!-- 仅支持上拉刷新 -->
      <flag name="both" value="0x3" /><!-- 上拉刷新和下拉刷新都支持 -->
      <flag name="manualOnly" value="0x4" /><!-- 只允许手动触发 -->

      <!-- These last two are depreacted -->
      <flag name="pullDownFromTop" value="0x1" />
      <flag name="pullUpFromBottom" value="0x2" />
    </attr>
    <!-- Whether the Indicator overlay(s) should be used -->
    <!-- 如果为true会在mPullRefreshListView中出现icon,右上角和右下角,挺有意思的 -->
    <attr name="ptrShowIndicator" format="reference|boolean" />
    <!-- Drawable to use as Loading Indicator. Changes both Header and Footer. -->
    <!-- 同时改变头部和底部的图标 -->
    <attr name="ptrDrawable" format="reference" />
    <!-- Drawable to use as Loading Indicator in the Header View. Overrides value set in ptrDrawable. -->
    <!-- 头部视图的图标-->
    <attr name="ptrDrawableStart" format="reference" />

    <!-- Drawable to use as Loading Indicator in the Footer View. Overrides value set in ptrDrawable. -->
    <!-- 底部视图的图标 -->
    <attr name="ptrDrawableEnd" format="reference" />
    <!-- Whether Android's built-in Over Scroll should be utilised for Pull-to-Refresh. -->
    <attr name="ptrOverScroll" format="reference|boolean" />
    <!-- Base text color, typeface, size, and style for Header and Footer Loading Views -->
     <!-- 分别设置拉Header或者上拉Footer中字体的类型颜色等等 -->
    <attr name="ptrHeaderTextAppearance" format="reference" />
    <!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header -->
    <attr name="ptrSubHeaderTextAppearance" format="reference" />
    <!-- Style of Animation should be used displayed when pulling. -->
    <attr name="ptrAnimationStyle">
      <flag name="rotate" value="0x0" /><!-- flip(翻转动画), rotate(旋转动画)  -->
      <flag name="flip" value="0x1" />
    </attr>
    <!-- Whether the user can scroll while the View is Refreshing -->
    <!-- 刷新的时候,是否允许ListView或GridView滚动 -->
    <attr name="ptrScrollingWhileRefreshingEnabled" format="reference|boolean" />
    <!--
      Whether PullToRefreshListView has it's extras enabled. This allows the user to be 
      able to scroll while refreshing, and behaves better. It acheives this by adding
      Header and/or Footer Views to the ListView.
    -->
    <!-- 决定了Header,Footer以何种方式加入mPullRefreshListView,true为headView方式加入,就是滚动时刷新头部会一起滚动 -->
    <attr name="ptrListViewExtrasEnabled" format="reference|boolean" />
    <!--
      Whether the Drawable should be continually rotated as you pull. This only
      takes effect when using the 'Rotate' Animation Style.
    -->
    <attr name="ptrRotateDrawableWhilePulling" format="reference|boolean" />
    <!-- BELOW HERE ARE DEPRECEATED. DO NOT USE. -->
    <attr name="ptrAdapterViewBackground" format="reference|color" />
    <attr name="ptrDrawableTop" format="reference" />
    <attr name="ptrDrawableBottom" format="reference" />
  </declare-styleable>
</resources>
复制代码

Android自定义刷新可以通过继承ViewViewGroup来实现。以下是一个自定义刷新View的例子: ```java public class CustomRefreshView extends View { private Paint mPaint; private RectF mRect; private float mStartAngle; private float mSweepAngle; private boolean mIsRefreshing; public CustomRefreshView(Context context) { super(context); init(); } public CustomRefreshView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomRefreshView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(8); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.BLUE); mRect = new RectF(); mStartAngle = 0; mSweepAngle = 0; mIsRefreshing = false; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float centerX = getWidth() / 2; float centerY = getHeight() / 2; float radius = Math.min(centerX, centerY) - mPaint.getStrokeWidth() / 2; mRect.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius); canvas.drawArc(mRect, mStartAngle, mSweepAngle, false, mPaint); if (mIsRefreshing) { mStartAngle += 10; if (mStartAngle >= 360) { mStartAngle -= 360; } invalidate(); } } public void startRefresh() { mIsRefreshing = true; mSweepAngle = 300; invalidate(); } public void stopRefresh() { mIsRefreshing = false; mSweepAngle = 0; invalidate(); } } ``` 在这个例子中,我们继承了View类,并实现了自定义的onDraw()方法来绘制一个圆弧。我们还提供了两个公共方法startRefresh()和stopRefresh()来启动和停止刷新。在startRefresh()方法中,我们设置了mIsRefreshing标志为true,并设置了mSweepAngle的值为300度,这将使圆弧从0度开始绘制到300度。然后我们调用invalidate()方法来触发View的重绘。在onDraw()方法中,我们使用mStartAngle来控制圆弧的起始角度,并使用invalidate()方法来持续重绘,直到mIsRefreshing标志被设置为false。 我们可以像使用其他View一样,在布局文件中使用CustomRefreshView: ```xml <com.example.CustomRefreshView android:id="@+id/refreshView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 然后在代码中使用startRefresh()和stopRefresh()方法来启动和停止刷新: ```java CustomRefreshView refreshView = findViewById(R.id.refreshView); refreshView.startRefresh(); // do some refreshing work refreshView.stopRefresh(); ``` 这样,我们就可以通过自定义View来实现Android的刷新功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值