Android---不注意的小细节

string.xml中数组的创建

    <string-array name="transitions">
        <item>Fade</item>
        <item>Slide</item>
        <item>Explode</item>
    </string-array>
  • Activity中的调用
    String[] Array = getResources().getStringArray(R.array.transitions);

getPosition \ getLayoutPosition \ getAdapterPosition

  • getPosition(); 查阅资料后发现[已弃用]
  • getLayoutPosition(); [条目在最新布局计算中的位置]
  • getAdapterPosition(); [条目在是适配器中的位置]

getLayoutPosition vs getAdapterPosition

这两种类型的位置是等同的,除非在分发adapter.notify*事件和更新布局时。

adapter和layout的位置会有时间差(<16ms), 如果你改变了Adapter的数据然后刷新视图, layout需要过一段时间才会更新视图, 在这段时间里面, 这两个方法返回的position会不一样.

在notifyDataSetChanged之后并不能马上获取Adapter中的position, 要等布局结束之后才能获取到.

而对于Layout的position, 在notifyItemInserted之后, Layout不能马上获取到新的position, 因为布局还没更新(需要<16ms的时间刷新视图), 所以只能获取到旧的, 但是Adapter中的position就可以马上获取到最新的position.

返回布局位置的方法使用最近一次布局运算后的位置,如getLayoutPosition()findViewHolderForLayoutPosition(int)。这些位置包含了最近一次布局运算后的变化。你可以根据这些位置来与用户正在屏幕上看到的保持一致。比如,你有一个条目列表,当用户请求第5个条目时,你可以使用这些方法来匹配用户看到的。

另外一系列方法与AdapterPosition关联,比如getAdapterPosition()findViewHolderForAdapterPosition(int)。当你想获得条目在更新后的适配器中的位置使用这些方法,即使这些位置变化还没反映到布局中。比如,你想访问适配器中条目的位置时,就应该使用getAdapterPosition()。注意,如果notifyDataSetChanged()已经被调用而且还没计算新布局,这些方法或许不能够计算适配器位置。所以,你要小心处理这些方法返回NO_POSITION和null的情况。

总之,当使用RecycleView.LayoutManager时使用布局位置。与此同时,使用RecycleView.Adapter时,使用适配器位置。

RecyclerView.ViewHolder - getLayoutPosition vs getAdapterPosition

AnimatedVectorDrawable

<vector>元素的矢量资源,在res/drawable/(文件夹)

<animated-vector>元素的矢量资源动画,在res/drawable/(文件夹)

< objectAnimator>元素的一个或多个对象动画器,在res/anim/(文件夹)

矢量资源动画能创建<group><path>元素属性的动画。<group>元素定义了一组路径或子组,并且<path>元素定义了要被绘制的路径。

例子

<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_remove">

    <target
        android:name="@string/remove"
        android:animation="@animator/remove_to_add" />

    <target
        android:name="@string/groupAddRemove"
        android:animation="@animator/rotate_remove_to_add" />

</animated-vector>
  • ic_remove.xml
<?xml version="1.0" encoding="UTF-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="@dimen/add_remove_width"
        android:height="@dimen/add_remove_height"
        android:viewportHeight="@integer/add_remove_viewport_height"
        android:viewportWidth="@integer/add_remove_viewport_width">
    <group
        android:name="@string/groupAddRemove"
        android:pivotX="@integer/add_remove_pivot_x"
        android:pivotY="@integer/add_remove_pivot_y">
        <path
            android:fillColor="@color/add_remove_stroke_color"
            android:pathData="@string/path_remove"/>
    </group>
</vector>
  • mAddToRemoveDrawable = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_add_to_remove);
  • mRemoveToAddDrawable = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_remove_to_add);
AnimatedVectorDrawable drawable =
                mIsAddState ? mRemoveToAddDrawable : mAddToRemoveDrawable;
        mAddRemoveImage.setImageDrawable(drawable);
        drawable.start();

sendEmptyMessageDelayed \ sendEmptyMessage \ sendMessage

  • sendEmptyMessageDelayed
 public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageDelayed(msg, delayMillis);
    }
  • sendEmptyMessage
    public final boolean sendEmptyMessage(int what)
    {
        return sendEmptyMessageDelayed(what, 0);
    }
  • sendMessage
   public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }
  • sendEmptyMessageDelayed==>sendMessageDelayed==>sendMessageAtTime==>enqueueMessage

  • sendEmptyMessage==>sendEmptyMessageDelayed==>等同于sendEmptyMessageDelayed

  • sendMessage==>sendMessageDelayed==>sendMessageAtTime==>enqueueMessage

invalidate \ postInvalidate

  • invalidate 在主线程中进行调用
  • postInvadlidate在子线程中进行调用
    • postInvalidate==>postInvalidateDelayed==>dispatchInvalidateDelayed==>sendMessageDelayed
      加入到looper之后在handleMessage中再调用对应View的invalidate()方法,请求View重绘。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值