如何移除Facebook Shimmer中子视图的闪烁效果
解决方案
你不需要拆分多个ShimmerFrameLayout分组,通过自定义ShimmerFrameLayout子类就能实现单个视图排除闪烁效果,步骤如下:
- 自定义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); } }
- 修改布局文件
把原来的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>
- 在代码中关联排除视图
在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
相关产品推荐
相关产品推荐

