RecyclerView没有显示任何内容

时间:2015-04-10 09:48:44

标签: android android-layout android-recyclerview

我试图实施新的RecyclerView,但它没有显示任何内容。 我已经经历了其他类似的问题,并确保我没有犯同样的错误。请帮我找到我在这里缺少的东西。

如果您需要任何其他文件,也请告诉我。

MainActivity

public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        NavigationDrawerFragment navigationDrawerFragment = (NavigationDrawerFragment)     getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        navigationDrawerFragment.setup((DrawerLayout)findViewById(R.id.drawer_layout), toolbar);

    }

}

NavigationDrawerFragment

public class NavigationDrawerFragment extends Fragment {

private RecyclerView recyclerView;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationDrawerViewAdapter navigationDrawerViewAdapter;

public NavigationDrawerFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.rvDrawerList);
    navigationDrawerViewAdapter = new NavigationDrawerViewAdapter(getActivity(), getNavigationDrawerData());
    recyclerView.setAdapter(navigationDrawerViewAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
}

public List<NavigationDrawerItem> getNavigationDrawerData() {

    List<NavigationDrawerItem> navigationDrawerItemList = new ArrayList<>();
    int[] imageIds = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    String[] titles = {"Title 1", "Title 2", "Title 3", "Title 4", "Title 5"};

    for (int i = 0; i < titles.length ; i++){
        NavigationDrawerItem navigationDrawerItem = new NavigationDrawerItem(imageIds[i], titles[i], titles[i]);
        navigationDrawerItemList.add(navigationDrawerItem);
    }

    return navigationDrawerItemList;
}


public void setup(DrawerLayout drawerLayout, final Toolbar toolbar) {

    this.drawerLayout = drawerLayout;
    this.actionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

    };
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            actionBarDrawerToggle.syncState();
        }
    });
}
}

NavigationDrawerViewAdapter

public class NavigationDrawerViewAdapter extends RecyclerView.Adapter<NavigationDrawerViewAdapter.NavigationDrawerViewHolder> {

// Declaring Variable to Understand which View is being worked on, IF the view under inflation and population is header or Item
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

private LayoutInflater layoutInflater;
List<NavigationDrawerItem> navigationDrawerItemList = new ArrayList<NavigationDrawerItem>();

public NavigationDrawerViewAdapter(Context context, List<NavigationDrawerItem> navigationDrawerItemList) {
    layoutInflater = LayoutInflater.from(context);
}

@Override
public NavigationDrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.item_navigation_drawer, parent, false);
    NavigationDrawerViewHolder navigationDrawerViewHolder = new NavigationDrawerViewHolder(view);
    return navigationDrawerViewHolder;
}

@Override
public void onBindViewHolder(NavigationDrawerViewHolder holder, int position) {
    NavigationDrawerItem navigationDrawerItem = navigationDrawerItemList.get(position);
    holder.ivImage.setImageResource(navigationDrawerItem.getImageId());
    holder.tvTitle.setText(navigationDrawerItem.getTitle());
    holder.tvDescription.setText(navigationDrawerItem.getDescription());
}

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

class NavigationDrawerViewHolder extends RecyclerView.ViewHolder {

    ImageView ivImage;
    TextView tvTitle;
    TextView tvDescription;

    public NavigationDrawerViewHolder(View itemView) {
        super(itemView);

        ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
        tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        tvDescription = (TextView) itemView.findViewById(R.id.tvDescription);

    }
}
}

1 个答案:

答案 0 :(得分:2)

NavigationDrawerViewAdapter中的navigationDrawerItemList未引用您从NavigationDrawerFragment传递的列表,添加行
this.navigationDrawerItemList =navigationDrawerItemList
到NavigationDrawerViewAdapters构造函数。