【安卓学习之常见问题】自定义组件-刷新后跳到第一行

█ 【安卓学习之常见问题】自定义组件-刷新后跳到第一行


█ 系列文章目录

提示:这里是收集了和文件分享有关的文章

█ 文章目录


█ 读前说明

  • 本文通过学习别人写demo,学习一些课件,参考一些博客,’学习相关知识,如果涉及侵权请告知
  • 本文只简单罗列相关的代码实现过程
  • 涉及到的逻辑以及说明也只是简单介绍,主要当做笔记,了解过程而已    

█ 问题

  • 数据会不断从服务器读取更新数据,一共有10行seekbar,不是用listview,而是直接复制了10个

在这里插入图片描述
>>>>可以正常上下滚动,以下是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fadeScrollbars="false"
        android:scrollbars="vertical">
        <!--已开抢-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <!--1-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingLeft="1dp"
                android:paddingRight="1dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <SeekBar
                        android:id="@+id/my_seekBar"
                        style="@style/MySeekBarStyle"
                        android:layout_width="0dp"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:max="2200"
                        android:minWidth="150dp"
                        android:progress="10" />

                    <TextView
                        android:id="@+id/my_text_view"
                        android:layout_width="100dp"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical"
                        android:maxWidth="100dp"
                        android:minWidth="30dp"
                        android:textColor="@color/text_white"
                        android:textSize="16sp" />

                </LinearLayout>
				<!--分割线-->
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#4D000000" />

            </LinearLayout>

            <!--2-->
			。。。。。。
            <!--10-->
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</LinearLayout>
  • 新版本,使用了TabLayout+ViewPager2,共有3个Fragment,每个里面都有10行 seekbar

在这里插入图片描述

>>>>可以正常上下滚动,以下是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        app:tabBackground="@drawable/tab_background"
        app:tabIndicatorColor="@color/transparent"
        app:tabIndicatorHeight="0dp"
        app:tabMinWidth="100dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/blue"
        app:tabTextColor="@color/white" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

█ 使用过程

  • 文件路径的传输过程(数据有点出入,因为刚开始测试是这样,后面测试又没有这个问题)
依赖库获取值方法结果
-Filenew File()/storage/emulated/0/
Android/data/com.bx.share/files/share/xxx.mp3
androidx.core:
core:1.5.0
UriFileProvider.getUriForFile(this, authority, file)content://com.bx.share.fileprovider/external_files/
Android/data/com.bx.share/files/share/xxx.mp3
androidx.core:
core:1.6.0
UriFileProvider.getUriForFile(this, authority, file)content://com.bx.share.fileprovider/files_root/
Android/data/com.bx.share/files/share/xxx.mp3
  • 代码实现:
		File shareFile = new File(mDirName, mFileName);// /storage/emulated/0/Android/data/com.bx.share/files/
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // content://com.bx.share.fileprovider/external_files/Android/data/com.bx.share/files/share/xxx.mp3
            uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", shareFile);
        } else {
            uri = Uri.fromFile(shareFile);
        }
        Intent intent = getChooserIntent(uri);
        
        Intent pickIntent = new Intent();
        pickIntent.setAction(Intent.ACTION_PICK_ACTIVITY);
        pickIntent.putExtra(Intent.EXTRA_TITLE, "4.分享文件 Pick Activity");
        pickIntent.putExtra(Intent.EXTRA_INTENT, intent);
        startActivityForResult(pickIntent, RESULT_SHARE);
    public Intent getChooserIntent(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_SEND);// 共享文件,部分app需要手机联网
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 传递权限
        intent.putExtra(Intent.EXTRA_TEXT, "分享测试123");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.setType("application/*");// 二进制文件 "application/octet-stream" "*/*"
        return intent;
    }

█ 正常使用

  • 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fadeScrollbars="false"
        android:scrollbars="vertical">
        <!--已开抢-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <!--1-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingLeft="1dp"
                android:paddingRight="1dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <SeekBar
                        android:id="@+id/my_seekBar"
                        style="@style/MySeekBarStyle"
                        android:layout_width="0dp"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:max="2200"
                        android:minWidth="150dp"
                        android:progress="10" />

                    <TextView
                        android:id="@+id/my_text_view"
                        android:layout_width="100dp" <!--修改后的-->
                        android:layout_height="match_parent"
                         android:editable="false" <!--修改后的-->
                        android:gravity="center_vertical"
                        android:maxWidth="100dp"
                        android:minWidth="30dp"
                        android:textColor="@color/text_white"
                        android:textSize="16sp" />

                </LinearLayout>
				<!--分割线-->
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#4D000000" />

            </LinearLayout>

            <!--2-->
			。。。。。。
            <!--10-->
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</LinearLayout>

█ 相关资料

提示:这里是参考的相关文章

  1. Android SVG to VectorDrawable >>>>svg转Vector
  2. 2016-12-23 Android Vector详解_yw1688的专栏-CSDN博客_android vector

█ 免责声明

博主分享的所有文章内容,部分参考网上教程,引用大神高论,部分亲身实践,记下笔录,内容可能存在诸多不实之处,还望海涵,本内容仅供学习研究使用,切勿用于商业用途,若您是部分内容的作者,不喜欢此内容被分享出来,可联系博主说明相关情况通知删除,感谢您的理解与支持!

转载请注明出处:
https://blog.csdn.net/ljb568838953/article/details/110383353

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值