我在回收站视图中显示所有项目时遇到问题.我的代码只显示一个项目(主页.我有3个项目:主页,浏览照片和我的旅行)
fragment_navigation_drawer.xml
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter{ private LayoutInflater inflater; List data = Collections.emptyList(); public RecyclerAdapter(Context context, List data){ inflater= LayoutInflater.from(context); this.data = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=inflater.inflate(R.layout.itemlist,parent,false); MyViewHolder holder= new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { MenuList current=data.get(position); holder.title.setText(current.title); holder.image.setImageResource(current.icon); } @Override public int getItemCount() { return data.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ TextView title; ImageView image; public MyViewHolder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.item_list); image = (ImageView) itemView.findViewById(R.id.icons); } } }
NavigationDrawerFragment.java(仅发布与recyclerview相关的代码)
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.drawerList); adapter = new RecyclerAdapter(getActivity(),getData()); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); return layout; } public static ListgetData(){ List data = new ArrayList<>(); String[] titles={"Home","My Travels","Browse Photos"}; int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image}; for(int i=0;i<3;i++){ MenuList current = new MenuList(); current.title=titles[i]; current.icon=icons[i]; Log.d("Data",titles[i]); Log.d("Data",""+icons[i]); data.add(current); } return data; }
itemlist.xml
我能做的是检查所有传递的数据(一切似乎都没问题).还尝试更改宽度以在我的相对和线性布局中包装内容(fragment_navigation_drawer.xml),但我只是丢失了我的背景颜色,但仍然只显示一个项目.
更改的高度LinearLayout
,以wrap_content
在itemlist.xml
android:layout_