美文网首页
简单知识

简单知识

作者: Tyson_Wu | 来源:发表于2023-12-12 15:52 被阅读0次

判断是否是新旧包

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 做一个上下滑动顶部工具类显示与隐藏的动效

  • 如下为效果图 GIF_20231214_101247.gif
  • 1.首先布局文件的RecycleView和顶部的条目要顶端对齐,我这里就是RecycleView和顶部的三个选项的顶端对齐,也就是RecycleView和RadioGroup对齐了
<?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>
  • 2.写一个显示与隐藏的动效,其中,title就是你的title的高度,我这里,就是RadioGroup的高度
 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()
    }

  • 3.在写一个RecycleView的滑动监听
  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
                }
            }
        })
  • 4.在初始化的时候,需要先调用一次显示动效
showHideTitleBar(true)

    1. RecycleView 这个解决notifyItemChanged闪一下的问题
 (mBinding.rvFamousAttractions.itemAnimator as? SimpleItemAnimator)?.supportsChangeAnimations = false
  • 6.如何通过文件名称获取对应的Drawable或者String
fun Context.getDrawableResourceId(name: String) :Int{
   return resources.getIdentifier(name, "drawable", packageName)
}

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

相关文章

  • 简单知识

    1.var关键词的作用域是最近的函数作用域(如果在函数体的外部就是全局作用域), let 关键词的作用域是最接近的...

  • 简单知识

    在学习spring的时候需要配置一个xml文件。大概是这样的。 以前没怎么学习过xml文件,学过之后做个笔记。 参...

  • mongoDB简单知识

    Windows 平台安装 MongoDB 下载链接 一步一步next即可。 将mongo加入系统服务,在shell...

  • 简单冷知识

    1 自然地打断别人说话 2 初次见面显得更加友好 3 如何判断谁喜欢你 4 怎么判断别人是不是在盯着你看 或者 5...

  • 知识蒸馏-简单

    参考文献: https://github.com/DA-southampton/NLP_ability/blob/...

  • 勤学

    姬滢灏 勤学,说来简单,说来难。简单于知识学会就好,难于想学会知识并没有那么简单...

  • 2021-2022曲靖市九年级上册期末试卷

    简单,超级简单!知识点也超多!

  • 线程知识 和 RunLoop简单知识

    我的博客: 博客链接 上面有关于 线程 和 RunLoop的知识,请大家多多指点。

  • 写给小学生及家长的眼健康常识

    简单明了的眼知识:

  • 2019-04-24

    JavaScript简单知识点总结

网友评论

      本文标题:简单知识

      本文链接:https://www.haomeiwen.com/subject/vuuvgdtx.html