Display only one item in recycler mode

I'm having trouble displaying all the elements in my recyclerview. My code shows only one element (I have 3 objects: "Home", "Browse photos and my travels").

fragment_navigation_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment_navigation_drawer" android:background="#EEE" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment"> <!-- TODO: Update blank fragment layout --> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="220dp" android:background="#16a085" android:id="@+id/linearLayout"> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/drawerList" android:layout_width="wrap_content" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </RelativeLayout> 

RecyclerAdapter.java

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> { private LayoutInflater inflater; List<MenuList> data = Collections.emptyList(); public RecyclerAdapter(Context context, List<MenuList> 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 (hosted code related to recyclerview only)

 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 List<MenuList> getData(){ List<MenuList> 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

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:padding="8dp" android:id="@+id/icons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_menu_camera" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/item_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dummy Text" android:layout_gravity="center_vertical" android:padding="8dp" /> </LinearLayout> 

What I was able to do was check all the transmitted data (everything seems to be all right). I also tried changing the width for wrapping the content (fragment_navigation_drawer.xml) in both my relative and linear layout, but instead I lose my background color and still only one element appears.

+5
source share
2 answers

Change LinearLayout height to wrap_content in itemlist.xml

 android:layout_height="wrap_content" 
+12
source

1) if your recyclerview is vertical then set the recyclerview height match_parent and row_item.xml height also match_parent

2) if your recyclerview is horizontal then set the width of the recyclerview match_parent and row_item.xml Width also match_parent

for ex: - Horizontal RecyclerView

 <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:layout_marginRight="60dp" /> 

row_item.xml

 <TextView android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:background="@drawable/rect" android:gravity="center" android:maxLines="2" android:padding="10dp" android:textColor="@color/white" android:textSize="25sp" /> 
0
source

All Articles