0

我正在製作一個音樂播放器應用程序,我正在使用加載程序將歌曲數據加載到將使用RecyclerView顯示的適配器。但是,我收到我的適配器方法無法正常工作的奇怪錯誤。只有適配器的構造函數方法纔會被調用。 儘管在堆棧溢出中經歷了所有可用的解決方案,但我還得到了「沒有附加適配器;跳過佈局」。要注意的RecyclerView沒有附加適配器;跳過佈局,數據不顯示

幾點:

  • 我已經嘗試了所有的解決方案,爲「無任何附加適配器;跳過佈局」中 recyclerview No adapter attached; skipping layout線程和所有相關的重複線程。
  • 我使用的RecyclerView不是正規的一個,但FastScrollRecyclerView,但由於它與常規RecyclerView延伸,並有在GitHub中沒有提到聽上去很像問題,所以我相信,使用這個庫是不是一個問題在這裏
  • 我也嘗試過所有適配器方法的解決方案,而不是從this線程調用,但沒有運氣。

下面的代碼:

SongsFragment.java

public class SongsFragment extends Fragment 
    implements LoaderManager.LoaderCallbacks<List<Song>>{ 

public static final String LOG_TAG = SongsFragment.class.getSimpleName(); 
private static final int LOADER_ID = 1; 
private ContentResolver mContentResolver; 
private SongListAdapter mSongListAdapter; 
private List<Song> mSongs; 
@BindView(R.id.rvSongs) FastScrollRecyclerView mRecyclerView; 

public SongsFragment() {} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ButterKnife.bind(getActivity()); 
    mSongs = new ArrayList<>(); 
    mRecyclerView = new FastScrollRecyclerView(getContext()); 
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    mRecyclerView.setLayoutManager(layoutManager); 
    mRecyclerView.setHasFixedSize(true); 
    mSongListAdapter = new SongListAdapter(getContext(), mSongs); 
    mRecyclerView.setAdapter(mSongListAdapter); 

} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    getLoaderManager().initLoader(LOADER_ID, null, this).forceLoad(); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_songs, container, false); 
} 

@Override 
public Loader<List<Song>> onCreateLoader(int id, Bundle args) { 
    mContentResolver = getActivity().getContentResolver(); 
    return new SongsLoader(getContext(), mContentResolver); 
} 

@Override 
public void onLoadFinished(Loader<List<Song>> loader, List<Song> data) { 
    mSongs = data; 
    mSongListAdapter.setData(mSongs); 
} 

@Override 
public void onLoaderReset(Loader<List<Song>> loader) { 
    mSongListAdapter.setData(new ArrayList<Song>()); 
} 

}

SongsListAdapter.java

public class SongListAdapter 
    extends FastScrollRecyclerView.Adapter<SongListAdapter.SongItemViewHolder> 
    implements FastScrollRecyclerView.SectionedAdapter{ 

public static final String LOG_TAG = SongListAdapter.class.getSimpleName(); 
private Context mContext; 
private List<Song> mSongList = new ArrayList<>(); 

public SongListAdapter(Context context, List<Song> songList) { 
    Log.d(LOG_TAG, "Constructor called"); 
    mContext = context; 
    mSongList = songList; 
} 

@NonNull 
@Override 
public String getSectionName(int position) { 
    return String.valueOf(mSongList.get(position).getTitle().charAt(0)).toUpperCase(); 
} 

@Override 
public SongItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Log.d(LOG_TAG, "onCreateViewHolder called"); 
    View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_song, null); 
    return new SongItemViewHolder(view); 
} 

@Override 
public void onBindViewHolder(SongItemViewHolder holder, int position) { 
    Log.d(LOG_TAG, "onBindViewHolder called"); 
    Uri albumArtUri = mSongList.get(position).getAlbumArtUri(); 
    Glide.with(mContext) 
      .load(albumArtUri) 
      .into(holder.albumArt); 
    holder.titleText.setText(mSongList.get(position).getTitle()); 
    holder.artistText.setText(mSongList.get(position).getArtistName()); 
    Log.d("Data", albumArtUri.toString() + "\n" + mSongList.get(position).getTitle() + "\n" + mSongList.get(position).getArtistName()); 
} 

@Override 
public int getItemCount() { 
    Log.d(LOG_TAG, "getItemCount called"); 
    return (mSongList != null ? mSongList.size() : 0); 
} 

public void setData(List<Song> songs){ 
    mSongList = songs; 
    notifyDataSetChanged(); 
} 

public class SongItemViewHolder extends FastScrollRecyclerView.ViewHolder { 
    ImageView albumArt; 
    TextView titleText; 
    TextView artistText; 

    SongItemViewHolder(View view) { 
     super(view); 
     Log.d(LOG_TAG, "SongItemViewHolder called"); 
     albumArt = (ImageView) view.findViewById(R.id.item_song_image); 
     titleText = (TextView) view.findViewById(R.id.item_song_title); 
     artistText = (TextView) view.findViewById(R.id.item_song_artist_name); 
    } 
} 

}

fragment_songs.xml(SongsFragment被誇大這種佈局)

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 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"> 


    <com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView 
     android:id="@+id/rvSongs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:fastScrollPopupBgColor="@color/colorAccent" 
     app:fastScrollPopupTextColor="@android:color/primary_text_dark" 
     app:fastScrollThumbColor="@color/colorAccent"/> 

</ScrollView> 

list_item_song.xml(以回收查看單個項目)

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

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginStart="12dp" 
    android:layout_marginLeft="12dp" 
    android:layout_marginRight="12dp" 
    android:background="#FFFFFF"/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingBottom="10dp" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:paddingStart="8dp" 
    android:paddingTop="10dp"> 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/item_song_image" 
      android:layout_width="64dp" 
      android:src="@drawable/music_placeholder" 
      android:layout_height="64dp"/> 

    </FrameLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/item_song_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" 
      android:text="Song_Title"/> 

     <TextView 
      android:id="@+id/item_song_artist_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="3dp" 
      android:textSize="12sp" 
      android:text="Song_Artist"/> 

    </LinearLayout> 

</LinearLayout> 

</LinearLayout> 

這已經很無奈。請查看代碼並幫助我。我認爲我做的一切都正確,但我可能是錯的。我知道scrollview和recyclerview沒有那麼好,但我看過預覽和回收站視圖。任何幫助將不勝感激。謝謝!在onLoadFinished()

+0

日誌列表的大小被提供給你的'SongListAdapter .setData()'。 –

+0

使用日誌語句來知道傳遞給'SongListAdapter.setData()'的列表大小。大小爲609.爲我的設備傳遞了609首歌曲。 – Pulak

+0

你會得到Log.d(LOG_TAG,「getItemCount called」);'? –

回答

0

啊!我浪費了很多時間,但終於找到了解決方案。我刪除Butterknife結合並通過改變onCreateView()這種捕捉視圖實例後使用常規findViewById內的SongsFragmentonCreateView()

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
View rootView = inflater.inflate(R.layout.fragment_songs, container, false); 
mRecyclerView = (FastScrollRecyclerView) rootView.findViewById(R.id.rvSongs); 
//Rest of the things 
} 

原來我用ButterKnife錯誤的方式,使實例mRecyclerView爲空,但後來與線 mRecyclerView = new FastScrollRecyclerView(getContext());它不是null了,但它仍然沒有連接到視圖,所以我沒有得到NullPointerException和代碼沒有工作。

我知道這是一個小白錯誤:d

使用ButterKnife與從官方網站拾起碎片正確的方法是:

public class FancyFragment extends Fragment { 
    @BindView(R.id.button1) Button button1; 
    @BindView(R.id.button2) Button button2; 

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fancy_fragment, container, false); 
    ButterKnife.bind(this, view); 
    // TODO Use fields... 
    return view; 
    } 
} 
1

嘗試設置適配器,還可以使用getActivity()上下文在適配器對象

@Override 
public void onLoadFinished(Loader<List<Song>> loader, List<Song> data) { 
    mSongs = data; 
    mSongListAdapter = new SongListAdapter(getActivity(), mSongs); 
    mRecyclerView.setAdapter(mSongListAdapter); 
} 

同樣在這種 mRecyclerView = new FastScrollRecyclerView(getContext());

mRecyclerView = new FastScrollRecyclerView(getActivity()); 

基本上在片段類使用getActivity()上下文

+0

更新我的答案,並嘗試它,並讓我知道 –

+0

已經嘗試在onLoadFinished()中設置適配器,但沒有成功。嘗試使用getActivity(),但沒有工作:( – Pulak

+0

感謝@quicklearner看看!我很長一段時間卡住了,我希望我很快得到解決方案。 – Pulak

相關問題