我尝试实现新的RecyclerView,但未显示任何内容。
我经历了其他类似的问题,并确保我没有犯同样的错误。请帮助我找到我在这里想念的东西。
如果您需要其他文件,也请告诉我。
主要活动
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);
}
}
}
最佳答案
Navigation Drawer ViewAdapter中的navigationDrawerItemList
未引用您从NavigationDrawerFragment传递的列表,请添加以下行this.navigationDrawerItemList =navigationDrawerItemList
到NavigationDrawerViewAdapters构造函数。
关于android - RecyclerView没有显示任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29558295/