如何在导航抽屉的第一个片段中调用 RecyclerView?我遵循了 slidenerd 的教程,但遇到了一个错误

how to call RecyclerView in the first fragment of the navigation drawer? I've followed a tutorial by slidenerd but getting one error

这是我要插入回收站视图的片段的 Java 文件。请不要担心我因为某些原因删除的包名。

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class Dashboard extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static Dashboard newInstance() {
        Dashboard fragment = new Dashboard();
        return fragment;
    }

    public Dashboard () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_dashboard, container,
                false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.drawerlist);
        adapter=new RecyclerAdapter(getActivity().getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return rootView;
    }

    public static List<Information> getData(){
        List<Information> data=new ArrayList<>();
        String[] titles={"Rooms Occupied","RoomsVacant","Check-In","Check-Out","Extensions","Confirmations","Cancellations"};
        for(int i=0;i<titles.length;i++)
        {
            Information current =new Information();
            current.title=titles[i];
            data.add(current);
        }
        return data;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }






}

这是我的适配器class

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by gowtham on 6/13/2015.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    List<Information> data= Collections.emptyList();
    public RecyclerAdapter(Context context, List<Information> data){
        inflater=LayoutInflater.from(context);
        this.data=data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.custom_row, parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title;


        public MyViewHolder(View itemView) {
            super(itemView);
            title= (TextView) itemView.findViewById(R.id.viewText);

        }
    }
}

这是我要在其中使用回收器视图的片段的布局文件

<?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="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewImage"
        android:background="@drawable/ic_dashboard"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/viewText"
        android:text="Dummy Text"
        android:textStyle="bold"
        android:layout_gravity="left"
        android:gravity="center" />
</LinearLayout>

因为我是 Whosebug 的新手,所以我没有足够的声誉来添加图像。所以我会在下面的评论中附上截图。请看一下它以了解这里到底发生了什么。谢谢!

为什么叫getActivity().getData(),就叫getData().

并且在您的适配器中 class:

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

它不能只是 return 零,你应该 return 数据大小。