Recyclerview没有显示任何内容?

4

我将尝试在recycler view中展示字符串列表,但出现了一些问题。它没有显示任何内容。

我不知道为什么它不起作用,这不是我第一次使用它,通常我会使用firebase recycler来实现。

以下是我的代码:

   rvTypeMusic = (RecyclerView)findViewById(R.id.rvMusicType);
    ArrayList<String> songs = new ArrayList<String>();
    songs.add("PHP");
    songs.add("C#");
    songs.add("Java");
    songs.add("JavaScript");
    songs.add("Android");

   adapter = new TypeMusicListAdapter(this,songs);
    rvTypeMusic.setAdapter(adapter);
    rvTypeMusic.setLayoutManager(new GridLayoutManager(this,2));
}


     public static class TypeMusicListAdapter extends 
              RecyclerView.Adapter<TypeMusicListViewHolder>{
    private LayoutInflater inflater;
    private Context context;
    private List<String> data;

    public TypeMusicListAdapter(Context context, List<String> data){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public TypeMusicListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.type_music_item,parent,false);
        return new TypeMusicListViewHolder(v);
    }

    @Override
    public void onBindViewHolder(TypeMusicListViewHolder holder, int position) {
        String songType = data.get(position);
        //holder.musicType.setText(songType.getSongName());
        holder.musicType.setText(songType);
    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

public static class TypeMusicListViewHolder extends RecyclerView.ViewHolder {
    private TextView musicType;

    public TypeMusicListViewHolder(View itemView) {
        super(itemView);
        musicType = (TextView) itemView.findViewById(R.id.musicType);
    }
}

XML 文件

      <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="com.world.bolandian.talent.MainActivity"
tools:showIn="@layout/app_bar_main">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvMusicType"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:listitem="@layout/type_music_item">

</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

XML 项目

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/musicType"
        android:padding="25dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
</android.support.v7.widget.CardView>

</LinearLayout> 

我在这里缺少什么?我尝试了几种方法,但都没有起作用。
3个回答

3
答案是 @abdur Rahman 和 @ADM 的结合体。 在设置适配器之前,我们必须将布局管理器设置为 RecyclerView,如下所示。
rvTypeMusic.setLayoutManager(new GridLayoutManager(this,2));
rvTypeMusic.setAdapter(adapter);

由于Recycler View在设置适配器后会立即开始布局视图,因此在那时必须拥有布局管理器。

其次,这个...

 @Override
public int getItemCount() {
    return data.size();
}

只有当返回非零值时,才会调用onCreateViewHolder和onBindViewHolder。


1
问题出在这一行。
 @Override
public int getItemCount() {
    return 0;
}

它应该是

 @Override
public int getItemCount() {
    return data.size();
}

0
在初始化回收视图后添加此行。
rvTypeMusic.setLayourManager(new LinearLayoutManager());

那么它就可以工作了。


我想在网格中显示结果。 - E.Bolandian
哦,还要设置getItemCount()。 - Abdur Rahman
LinearLayoutManager 适用于垂直列表。问题中明确说明它适用于网格项目。 - ADM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接