You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Facebook Shimmer中子视图的闪烁效果

解决方案

你不需要拆分多个ShimmerFrameLayout分组,通过自定义ShimmerFrameLayout子类就能实现单个视图排除闪烁效果,步骤如下:

  1. 自定义Shimmer布局类
    重写父类的applyShimmerToView方法,跳过指定视图的闪烁效果应用:
public class CustomShimmerLayout extends ShimmerFrameLayout {
    private View mSkipView;

    public CustomShimmerLayout(Context context) {
        super(context);
    }

    public CustomShimmerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomShimmerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // 设置需要排除闪烁的视图
    public void setSkipShimmerView(View view) {
        mSkipView = view;
    }

    @Override
    protected void applyShimmerToView(View child, float alpha, int tint) {
        // 如果是目标视图,跳过闪烁效果的应用
        if (child == mSkipView) {
            return;
        }
        super.applyShimmerToView(child, alpha, tint);
    }
}
  1. 修改布局文件
    把原来的com.facebook.shimmer.ShimmerFrameLayout替换成你自定义的类(注意替换成你的实际包名):
<com.your.package.name.CustomShimmerLayout
    android:id="@+id/shimmerParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:shimmer_auto_start="true">

    <!-- 保留原有视图结构,包括那个不需要闪烁的TextView -->
    <TextView 
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="@string/zc_past_statements"
        android:textAppearance="@style/SomeTextStyle"
        android:textColor="@color/black_22"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/shimmerNudgeBg" />

</com.your.package.name.CustomShimmerLayout>
  1. 在代码中关联排除视图
    在Activity/Fragment中找到目标TextView,告诉自定义Shimmer布局跳过它:
CustomShimmerLayout shimmerParent = findViewById(R.id.shimmerParent);
TextView titleTv = findViewById(R.id.title);
shimmerParent.setSkipShimmerView(titleTv);

这样就能让指定的TextView不受Shimmer闪烁影响,其他视图正常生效,完全不需要拆分布局分组。另外注意:你给TextView加的app:shimmer_auto_start="false"是无效的,这个属性仅属于ShimmerFrameLayout,直接删掉即可。

内容的提问来源于stack exchange,提问作者n_r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 02:15:17