我正在开发一个包含一些片段和适配器的音乐播放器应用。该应用程序的主屏幕有一个回收器视图,在适配器和片段的帮助下显示歌曲列表。但是当我运行我的应用程序时,我得到一个空白屏幕。我错过了什么?以下是应用程序的代码:(请评论,如果需要附加任何其他文件)。
我的MainActivity.kt文件:
class MainActivity : AppCompatActivity(){
var navigationDrawerIconList : ArrayList<String> = arrayListOf()
var images_for_navdrawer = intArrayOf(R.drawable.navigation_allsongs, R.drawable.navigation_favorites,
R.drawable.navigation_settings, R.drawable.navigation_aboutus)
object Statified{
var drawerLayout : DrawerLayout?=null
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
MainActivity.Statified.drawerLayout=findViewById(R.id.drawer_layout)
// drawerLayout=findViewById<DrawerLayout>(R.id.drawer_layout) as DrawerLayout
navigationDrawerIconList.add("All Songs")
navigationDrawerIconList.add("Favorites")
navigationDrawerIconList.add("Settings")
navigationDrawerIconList.add("About Us")
val toggle = ActionBarDrawerToggle(this@MainActivity, MainActivity.Statified.drawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close)
MainActivity.Statified.drawerLayout?.setDrawerListener(toggle)
toggle.syncState()
val mainScreenFragment = MainScreenFragment()
this.supportFragmentManager
.beginTransaction()
.add(R.id.details_fragment, mainScreenFragment, "MainScreenFragment")
.commit()
//Navigation Drawer
var _navigationAdapter = NavigationDrawerAdapter(navigationDrawerIconList, images_for_navdrawer, this@MainActivity)
_navigationAdapter.notifyDataSetChanged()
var navigation_recycler_view = findViewById<RecyclerView>(R.id.navigation_recycler_view)
navigation_recycler_view.layoutManager = LinearLayoutManager(this@MainActivity)
navigation_recycler_view.itemAnimator = DefaultItemAnimator()
navigation_recycler_view.adapter = _navigationAdapter
navigation_recycler_view.setHasFixedSize(true)
// var navigation_recycler_view = findViewById<RecyclerView>(R.id.navigation_recycler_view)
//navigation_recycler_view.layoutManager = LinearLayoutManager(this)
//navigation_recycler_view.itemAnimator = DefaultItemAnimator()
}
override fun onStart() {
super.onStart()
}
}
我的MainScreenFragment.kt文件:
class MainScreenAdapter(_songDetails:ArrayList<Songs>, _context: Context) : RecyclerView.Adapter<MainScreenAdapter.MyViewHolder>(){
var songDetail: ArrayList<Songs>?=null
var mContext: Context?=null
init {
this.songDetail = _songDetails
this.mContext = _context
}
override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {
val songObject = songDetail?.get(position)
holder?.trackTitle?.text = songObject?.songTitle
holder?.trackArtist?.text = songObject?.artist
holder?.contentHolder?.setOnClickListener({
val songPlayingFragment = SongPlayingFragment()
var args = Bundle()
args.putString("songArtist",songObject?.artist)
args.putString("path", songObject?.songData)
args.putString("songTitle", songObject?.songTitle)
args.putInt("SongId", songObject?.songId?.toInt() as Int)
args.putInt("songPosition", position)
args.putParcelableArrayList("songData", songDetail)
songPlayingFragment.arguments = args
(mContext as FragmentActivity).supportFragmentManager
.beginTransaction()
.replace(R.id.details_fragment,songPlayingFragment)
.commit()
})
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent?.context)
.inflate(R.layout.row_custom_mainscreen_adapter,parent,false)
return MyViewHolder(itemView)
}
override fun getItemCount(): Int {
if(songDetail == null){
return 0
} else{
return (songDetail as ArrayList<Songs>).size
}
}
class MyViewHolder(view: View): RecyclerView.ViewHolder(view) {
var trackTitle: TextView? = null
var trackArtist: TextView? = null
var contentHolder: RelativeLayout? = null
init {
trackTitle = view.findViewById<TextView>(R.id.trackTitle)
trackArtist = view.findViewById<TextView>(R.id.trackArtist)
contentHolder = view.findViewById<RelativeLayout>(R.id.contentRow)
}
}
}
和我的MainScreenFragment.kt文件:
class MainScreenFragment : Fragment() {
var getSongList : ArrayList<Songs>? = null
var nowPlayingBottomBar: RelativeLayout?=null
var playPauseButton: ImageView?=null
var songTitle: TextView?=null
var visibleLayout: RelativeLayout?=null
var noSongs: RelativeLayout?=null
var recyclerView: RecyclerView?= null
var myActivity:Activity?=null
var _mainScreenAdapter : MainScreenAdapter?=null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.content_main, container, false)
setHasOptionsMenu(true)
activity.title = "All songs"
visibleLayout = view?.findViewById<RelativeLayout>(R.id.visibleLayout)
noSongs = view?.findViewById<RelativeLayout>(R.id.noSongs)
nowPlayingBottomBar = view?.findViewById<RelativeLayout>(R.id.hiddenBarMainScreen)
songTitle = view?.findViewById<TextView>(R.id.songTitleMainScreen)
playPauseButton = view?.findViewById<ImageButton>(R.id.playpauseButton)
(nowPlayingBottomBar as RelativeLayout).isClickable = false
recyclerView = view?.findViewById<RecyclerView>(R.id.contentMain)
if (getSongList == null) {
getSongList = getSongsFromPhone()
if (getSongList == null) {
visibleLayout?.visibility = View.INVISIBLE
noSongs?.visibility = View.VISIBLE
}
} else {
Log.d(MainScreenFragment::class.java.simpleName, " Data already there")
}
getSongList = getSongsFromPhone()
_mainScreenAdapter = MainScreenAdapter(getSongList as ArrayList<Songs>, activity)
val mLayoutManager = LinearLayoutManager(activity)
(recyclerView as RecyclerView).layoutManager = mLayoutManager
(recyclerView as RecyclerView).itemAnimator = DefaultItemAnimator()
(recyclerView as RecyclerView).adapter = _mainScreenAdapter
return view
// Inflate the layout for this fragment
/* val view = inflater!!.inflate(R.layout.fragment_main_screen, container, false)
visibleLayout = view?.findViewById<RelativeLayout>(R.id.visibleLayout)
noSongs = view?.findViewById<RelativeLayout>(R.id.noSongs)
nowPlayingBottomBar = view?.findViewById<RelativeLayout>(R.id.hiddenBarMainScreen)
songTitle = view?.findViewById<TextView>(R.id.songTitleMainScreen)
playPauseButton = view?.findViewById<ImageButton>(R.id.playPauseButton)
recyclerView = view?.findViewById<RecyclerView>(R.id.contentMain)
return view*/
}
fun getSongsFromPhone(): ArrayList<Songs>{
var arrayList = ArrayList<Songs>()
var contentResolver = myActivity?.contentResolver
var songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
var songCursor = contentResolver?.query(songUri, null, null, null, null)
if(songCursor!=null && songCursor.moveToFirst()){
val songId = songCursor.getColumnIndex(MediaStore.Audio.Media._ID)
val SongTitle = songCursor.getColumnIndex((MediaStore.Audio.Media.TITLE))
val songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)
val songData = songCursor.getColumnIndex(MediaStore.Audio.Media.DATA)
val dateIndex = songCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED)
while(songCursor.moveToNext()){
var currentId = songCursor.getLong(songId)
var currentTitle = songCursor.getString(SongTitle)
var currentArtist = songCursor.getString(songArtist)
var currentData = songCursor.getString(songData)
var currentDate = songCursor.getString(dateIndex)
}
}
return arrayList
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
getSongList = getSongsFromPhone()
_mainScreenAdapter = MainScreenAdapter(getSongList as ArrayList<Songs>, myActivity as Context)
val mLayoutManager = LinearLayoutManager(myActivity)
recyclerView?.layoutManager = mLayoutManager
recyclerView?.itemAnimator = DefaultItemAnimator()
recyclerView?.adapter = _mainScreenAdapter
}
override fun onAttach(context: Context?) {
super.onAttach(context)
myActivity = context as Activity
}
override fun onAttach(activity: Activity?) {
super.onAttach(activity)
myActivity = activity
}
}
FragmentMainScreen.xml文件:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_main"
android:clickable="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/visibleLayout">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contentMain">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/hiddenBarMainScreen"
android:visibility="visible"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playPauseButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="31dp"
android:background="@drawable/pause_icon"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/defaultMusic"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/now_playing_bar_eq_image"
android:layout_marginLeft="13dp"
android:minHeight="0dp"
android:minWidth="0dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Now Playing"
android:id="@+id/nowPlaying"
android:layout_toRightOf="@id/defaultMusic"
android:textColor="#bdbdbd"
android:layout_marginLeft="15dp"
android:layout_marginTop="11dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/songTitleMainScreen"
android:layout_below="@id/nowPlaying"
android:ellipsize="end"
android:maxLines="1"
android:maxWidth="160dp"
android:singleLine="true"
android:text="..."
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignLeft="@id/nowPlaying"
android:layout_alignStart="@id/nowPlaying"
android:layout_marginTop="5dp"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:visibility="invisible"
android:id="@+id/noSongs">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="You do not have any Songs"
android:textColor="#000000"/>
</RelativeLayout>
Activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/header">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"/>
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/echo_logo"
android:layout_centerInParent="true"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_recycler_view"
android:layout_below="@+id/header">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>