簡體   English   中英

Recyclerview沒有顯示任何內容?

[英]Recyclerview does not show anything?

我正在嘗試在回收站視圖中顯示字符串列表。 由於某種原因,它無法正常工作。 它沒有顯示任何東西。 我不知道為什么它不起作用,這不是我第一次使用它,通常我用firebase回收器做了。

這是我的代碼

   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> 

我在這里失蹤了什么? 我嘗試了一些東西,但沒有任何效果。

答案是@abdur Ra​​hman和@ ADM的混合。 首先,我們必須在設置適配器之前將布局管理器設置為RecyclerView。

rvTypeMusic.setLayoutManager(new GridLayoutManager(this,2));
rvTypeMusic.setAdapter(adapter);

因為我們設置適配器后,循環器視圖將開始鋪設視圖,因此它必須具有布局管理器。 其次,這個

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

onCreateViewHolder和onBindViewHolder僅在返回非零值時才被調用

問題出在這一行。

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

它應該是

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

初始化循環視圖后添加此行。

rvTypeMusic.setLayourManager(new LinearLayoutManager());

它會工作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM