RecyclerView没有连接适配器;跳过布局,数据不显示 [英] RecyclerView No adapter attached; skipping layout, Data not showing

查看:172
本文介绍了RecyclerView没有连接适配器;跳过布局,数据不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个音乐播放器应用程序,我正在使用加载程序将歌曲数据加载到适配器,该适配器将使用RecyclerView显示。但是,我得到这个奇怪的错误,我的适配器方法无法正常工作。只调用适配器的构造函数方法。
我还得到没有适配器连接;跳过布局,尽管在堆栈溢出时通过所有可用的解决方案。

I'm making a music player application where I'm using a loader to load song data to the adapter which is to be shown using a RecyclerView. However, I'm getting this weird error of my adapter methods not working. Only the constructor method of adapter is getting called. I'm also getting "No adapter attached; skipping layout" despite going through all the available solutions here in stack overflow.

很少指向请注意:


  • 我在
    中尝试了所有解决方案无适配器附加;跳过布局 recyclerview没有附加适配器;跳过布局线程和所有相关的重复线程。

  • 我使用的RecyclerView不是常规的,而是 FastScrollRecyclerView ,但由于它从常规的RecyclerView扩展而且github上没有提到相关的问题,所以我确信在这里使用这个库不是问题

  • 我还尝试了所有未通过这个主题,但没有运气。

  • I've tried all the solutions for "No adapter attached; skipping layout" in recyclerview No adapter attached; skipping layout thread and all associated duplicate threads.
  • The RecyclerView I'm using is not the regular one but FastScrollRecyclerView, but since it extends from the regular RecyclerView and there are no relatable issues mentioned on github so I'm convinced that using this library is not an issue here
  • I've also tried all solutions for the adapter methods not being called from this thread but no luck.

以下是代码:

SongsFragment.java

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

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正在扩充此布局)

fragment_songs.xml (SongsFragment is inflating this layout)

<?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(回收商视图中的单个项目)

list_item_song.xml (Individual item in recycler view)

<?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不是那么顺利,但我已经看过预览和回收者视图显示。任何帮助将不胜感激。谢谢!

This has been really frustrating. Please review the code and help me with this. I think I've done everything correctly but I may be wrong. I know scrollview and recyclerview don't go that well but I've seen the preview and the recycler view shows. Any help will be appreciated. Thanks!

推荐答案

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

Ugh! I wasted a lot of time in this but finally came out with the solution. I removed Butterknife binding and used conventional findViewById inside onCreateView() of SongsFragment after capturing the view instance by changing the onCreateView() to this:

@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 并且代码不起作用。

Turns out I was using ButterKnife the wrong way so the instance mRecyclerView was null but later with line mRecyclerView = new FastScrollRecyclerView(getContext()); it wasn't null anymore but it still wasn't connected to the view so I didn't get NullPointerException and the code didn't work.

我知道这是一个noob错误:D

I know it was a noob mistake :D

从官方网站上获取的片段使用ButterKnife的正确方法是:

Correct way to use ButterKnife with fragments as picked up from official website is:

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;
  }
}

这篇关于RecyclerView没有连接适配器;跳过布局,数据不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆