我已经尝试创建一个RecyclerView,它从预先填充的ArrayList中显示我手机上的歌曲.这是我的活动代码:
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private RecyclerView songRecyclerView; private RecyclerView.Adapter songRecyclerAdapter; private RecyclerView.LayoutManager recyclerLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //get the songs list for the adapter ArrayList<Audio> songList; StorageUtils storageUtils = new StorageUtils(getApplicationContext()); songList = storageUtils.loadAudio(); //Recycler view setup for songs display songRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); songRecyclerView.setHasFixedSize(true); recyclerLayoutManager = new linearlayoutmanager(this); songRecyclerView.setLayoutManager(recyclerLayoutManager); songRecyclerAdapter = new SongAdapter(songList); songRecyclerView.setAdapter(songRecyclerAdapter); }
Audio类有getTitle()和getArtist()方法,它们可以工作.响亮的音频()也有效,所以Songlist肯定有元素.
这是recyclerview项目的xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/recycler_item" android:layout_width="match_parent" android:layout_height="72dp" android:visibility="visible"> <TextView android:id="@+id/recycler_item_songName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:text="song name" android:textColor="@color/textPrimary" android:textSize="16sp" android:textStyle="normal" android:visibility="visible" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/recycler_item_artistName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:text="song artist" android:textColor="@color/textSecondary" android:textSize="16sp" android:textStyle="normal" android:visibility="visible" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/recycler_item_songName" /> </android.support.constraint.ConstraintLayout>
这是我的适配器实现:
package com.ecebuc.gesmediaplayer; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> { // The dataset private ArrayList<Audio> songList; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView recyclerTitleView,recyclerArtistView; public ViewHolder(View itemView) { super(itemView); this.recyclerTitleView = (TextView) itemView.findViewById(R.id.recycler_item_songName); this.recyclerArtistView = (TextView) itemView.findViewById(R.id.recycler_item_artistName); } } // Constructor public SongAdapter(ArrayList<Audio> songList){ this.songList = songList; } @Override public SongAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { // create a new view LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View listItemView = layoutInflater.inflate(R.layout.song_list_item,parent,false); ViewHolder viewHolder = new ViewHolder(listItemView); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder,int position) { Audio currentSong = songList.get(position); holder.recyclerTitleView.setText(currentSong.getTitle()); holder.recyclerArtistView.setText(currentSong.getArtist()); Log.d("onBind: ",(String)holder.recyclerTitleView.getText() + (String)holder.recyclerArtistView.getText()); } @Override public int getItemCount() { return songList.size(); } }
令人沮丧的是,在我第一次尝试创建整个recycleView时,它确实工作并显示了文本.我尝试将一个imageView作为歌曲的封面添加到列表中每个项目的布局中,以及显示它的代码,它不再起作用了.当试图恢复并只有文本代码时,它完全停止工作.
我正在抨击这个因为如果我在某处做了一些小改动,我现在不知道它会在哪里.但我确实尝试从头开始为适配器和整个回收器功能的布局文件重新创建类,但仍未显示.布局项目必须在那里因为我看到了滚动的阴影.
此外,在适配器的onBindViewHolder中,Log.d正确显示每个歌曲标题和艺术家.它调用新创建的视图’getText().这就像文字是白色的.不,XML中的@ color / text primary和textSecondary的值是#212121和#424242并且总是那样(再次,它确实显示了第一次的值,而我没有触及颜色).
我在StackOverflow和在线上寻找类似的问题,但我似乎没有那种错误.我不知道该怎么办了.哎呀,我甚至会在其中包含具有实际recyclerView的屏幕的XML.在这一点上,我不知道是什么使这些观点不可见:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".HomeActivity" tools:showIn="@layout/app_bar_home"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
最后,我想到了查看手机上的开发人员选项并显示布局轮廓,这就是我所拥有的…好像两个textViews没有被创建,但我能够在我之后获得他们的文本内容设置它,那怎么可能呢?感谢能帮助我的人,我只是想在这里学习……