RecyclerView没有连接适配器;跳过布局,数据未显示

时间:2017-05-28 08:23:44

标签: java android android-layout android-recyclerview android-adapter

我正在创建一个音乐播放器应用程序,我正在使用加载程序将歌曲数据加载到适配器,该适配器将使用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不是那么顺利,但我看过预览和回收者视图显示。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

尝试在onLoadFinished()中设置适配器,并在适配器对象

中使用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()作为片段类

中的上下文

答案 1 :(得分:1)

唉!我浪费了很多时间,但终于拿出了解决方案。我删除了Butterknife绑定,并在通过将findViewById更改为此来捕获视图实例后使用了onCreateView() SongsFragment内的常规onCreateView()

@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为null但稍后使用了行 mRecyclerView = new FastScrollRecyclerView(getContext());它不再是null,但它仍然没有连接到视图,所以我没有得到NullPointerException而且代码没有工作。

我知道这是一个noob错误: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;
  }
}