简单知识

2023-12-12  本文已影响0人  Tyson_Wu

判断是否是新旧包

public boolean isNewUserForVersionCode(Context context) {
    /**
     * 获取指定应用程序  ApplicationInfo
     *  参数一对应应用程序的包名
     *  参数二 应用程序对应的标识  通常为 0
     */
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        long firstInstallTime = packageInfo.firstInstallTime;
        long lastUpdateTime = packageInfo.lastUpdateTime;
        String versionName = packageInfo.versionName;
        if (firstInstallTime == lastUpdateTime && versionName.contains(mVersionName)) {
            return true;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}

RecycleView 做一个上下滑动顶部工具类显示与隐藏的动效

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F0F2F8"
    android:fitsSystemWindows="true"
    tools:context=".activity.FamousAttractionsActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_famous_attractions_title"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/white"
        app:layout_constraintTop_toTopOf="parent">


        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/ayp_8dp"
            android:background="@drawable/ripple_icon_circle"
            android:contentDescription="@null"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_back_black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_page_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="8dp"
            android:ellipsize="end"
            android:fontFamily="@font/kanit_semibold"
            android:gravity="start"
            android:lines="1"
            android:text="@string/famous_attractions"
            android:textColor="@color/black_253A"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="@id/iv_back"
            app:layout_constraintEnd_toStartOf="@id/iv_famous_favorite"
            app:layout_constraintStart_toEndOf="@id/iv_back"
            app:layout_constraintTop_toTopOf="@id/iv_back" />

        <ImageView
            android:id="@+id/iv_famous_favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:background="@drawable/ripple_icon_circle"
            android:src="@drawable/ic_famous_favorite"
            app:layout_constraintBottom_toBottomOf="@id/iv_back"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/iv_back" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@+id/view_famous_line"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="@color/gray_famous_line"
        app:layout_constraintTop_toBottomOf="@id/cl_famous_attractions_title" />



    <RadioGroup
        android:id="@+id/rg_famous_attractions"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_40"
        android:background="@color/white"
        android:orientation="horizontal"
        android:paddingHorizontal="@dimen/dp16"
        android:paddingVertical="5dp"
        app:layout_constraintTop_toBottomOf="@id/view_famous_line">

        <RadioButton
            android:id="@+id/rb_famous_all"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/ripple_white_20r"
            android:button="@null"
            android:gravity="center"
            android:text="@string/all"
            android:textColor="@color/selector_famous_attraction"
            android:textSize="@dimen/sp_14" />

        <RadioButton
            android:id="@+id/rb_famous_seven_wonders"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/ripple_white_20r"
            android:button="@null"
            android:gravity="center"
            android:text="@string/seven_wonders"
            android:textColor="@color/selector_famous_attraction"
            android:textSize="@dimen/sp_14" />

        <RadioButton
            android:id="@+id/rb_famous_regions"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/ripple_white_20r"
            android:button="@null"
            android:gravity="center"
            android:text="@string/regions"
            android:textColor="@color/selector_famous_attraction"
            android:textSize="@dimen/sp_14" />

    </RadioGroup>

    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/view_famous_line"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/vp_famous_attractions"/>




</androidx.constraintlayout.widget.ConstraintLayout>
 private var mAnimatorTitle: Animator? = null
    private var mAnimatorContent: Animator? = null
    private var mAnimatorTitleAlpha: Animator? = null

    private fun showHideTitleBar(tag: Boolean) {
        if (mAnimatorTitle != null && mAnimatorTitle!!.isRunning) {
            mAnimatorTitle!!.cancel()
        }
        if (mAnimatorContent != null && mAnimatorContent!!.isRunning) {
            mAnimatorContent!!.cancel()
        }
        val titleView = (activity as FamousAttractionsActivity?)!!.titleView
        if (tag) {
            mAnimatorTitle =
                ObjectAnimator.ofFloat(titleView, "translationY", titleView.translationY, 0f)
            mAnimatorTitleAlpha = ObjectAnimator.ofFloat(titleView, "alpha", 0f, 1f)
            mAnimatorContent = ObjectAnimator.ofFloat(
                mBinding.rvFamousAttractions,
                "translationY",
                mBinding.rvFamousAttractions.translationY,
                titleView.height.toFloat()
            )
        } else {
            mAnimatorTitle = ObjectAnimator.ofFloat(
                titleView,
                "translationY",
                titleView.translationY,
                -titleView.height.toFloat()
            )
            mAnimatorTitleAlpha = ObjectAnimator.ofFloat(titleView, "alpha", 1f, 0f)
            mAnimatorContent = ObjectAnimator.ofFloat(
                mBinding.rvFamousAttractions,
                "translationY",
                mBinding.rvFamousAttractions.translationY,
                0f
            )

        }
        mAnimatorTitle?.start()
        mAnimatorTitleAlpha?.start()
        mAnimatorContent?.start()
    }

  mBinding.rvFamousAttractions.addOnScrollListener(object :
            RecyclerView.OnScrollListener() {
            private var scrollDist = 0
            private var isVisible = true
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (isVisible && scrollDist > 50) {
                    // 上滑隐藏
                    isVisible = false
                    scrollDist = 0
                    // 执行隐藏动画或其他操作
                    showHideTitleBar(false)
                } else if (!isVisible && scrollDist < -50) {
                    // 下滑显示
                    isVisible = true
                    scrollDist = 0
                    // 执行显示动画或其他操作
                    showHideTitleBar(true)
                }
                if (isVisible && dy > 0 || !isVisible && dy < 0) {
                    scrollDist += dy
                }
            }
        })
showHideTitleBar(true)

 (mBinding.rvFamousAttractions.itemAnimator as? SimpleItemAnimator)?.supportsChangeAnimations = false
fun Context.getDrawableResourceId(name: String) :Int{
   return resources.getIdentifier(name, "drawable", packageName)
}

fun Context.getStringResource(name: String) :Int{
    return resources.getIdentifier(name, "string", packageName)
}
上一篇 下一篇

猜你喜欢

热点阅读