说一下代码在用的时候注意事项以及在运行的时候可能遇到的问题:
首先代码可以在创建相应文件后直接复制,这个案例用到了RecyclerView,所以需要先添加依赖。添加下面两个:
implementation ‘com.android.support:recyclerview-v7:27.1.1′
implementation ‘com.android.support:cardview-v7:27.1.1′
具体版本看自己的软件。
一定要在AndroidManifest.xml加上:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
下面代码部分也给出了。
接下来说一下运行时的问题:
(1)开发本项目最大的问题在于API版本问题,这也是Android开发中最头疼的问题,在遇到问题的时候一定要查看下自己的版本。
(2)在模拟器上打开项目时为空白列表,原因在于模拟器没有音乐文件,可以先利用模拟器下载音乐文件,之后再打开就能显示了 。下载文件的时候不要直接将文件从电脑拖入模拟器,虽然有文件,但是不被应用识别,我在运行的时候就遇到了这个问题,音乐文件最好从网上下载。
(3)如果运行时模拟器不能打开app,报下面错误:
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=13081, uid=10084 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
一定要确定加入了我上面所说的那行代码,然后打开模拟器设置(settings)找到应用管理(apps¬ifications),选择应用权限(APP permissions )找到自己的软件,打开文件读取权限。看下面第一张图,附上两张app运行的截图:
下面的的源代码来自:https://gitee.com/happyanimee/localmusic
这里面有整个项目的文件,本文的主要任务是总结使用时候所遇到的问题以及主要代码供大家学习。里面所用到的图片在文末下载。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:id="@+id/local_music_bottomlayout"
android:background="#33EEEEEE">
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#9933FA"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/icon_song"
android:layout_centerVertical="true"
android:background="@mipmap/a1"
android:layout_marginLeft="10dp"
android:id="@+id/local_music_bottom_iv_icon"/>
<TextView
android:id="@+id/local_music_bottom_tv_song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="@id/local_music_bottom_iv_icon"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/local_music_bottom_tv_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:layout_below="@id/local_music_bottom_tv_song"
android:layout_alignLeft="@id/local_music_bottom_tv_song"
android:layout_marginTop="10dp"/>
<ImageView
android:id="@+id/local_music_bottom_iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@mipmap/icon_next"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"/>
<ImageView
android:id="@+id/local_music_bottom_iv_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@mipmap/icon_play"
android:layout_toLeftOf="@id/local_music_bottom_iv_next"
android:layout_marginRight="20dp"/>
<ImageView
android:id="@+id/local_music_bottom_iv_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@mipmap/icon_last"
android:layout_toLeftOf="@id/local_music_bottom_iv_play"
android:layout_marginRight="20dp"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/local_music_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/local_music_bottomlayout">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
item_local_music.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:contentPadding="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="1dp"
app:cardBackgroundColor="@color/colorPink">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_local_music_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_centerVertical="true"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/item_local_music_song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"