Shariq Ahmed Shah Shariq Ahmed Shah -4 years ago 263
Android Question

Recycler View in navigation drawer is not showing more than one item

I am trying to make navigation drawer using recycler view but main problem is that when i run this, it shows only the first row with its icon and which has home as its title and not any other one. I tried with Camera icon but it is not showing anything other than the first row.

Screenshot of output:

enter image description here

Here are all the java files

MainActivity.java

public class MainActivity extends AppCompatActivity {

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().setDisplayShowTitleEnabled(true);

Nav_drawer nav_drawer=(Nav_drawer)getSupportFragmentManager().findFragmentById(R.id.f1);
nav_drawer.setUp(R.id.f1,(DrawerLayout)findViewById(R.id.activity_main),toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}


Sadapter.java

public class Sadapter extends RecyclerView.Adapter<Sadapter.MyViewHolder> {

private LayoutInflater inflater;
List<Information> data= Collections.emptyList();

public Sadapter(Context context,List<Information> data)
{
inflater=LayoutInflater.from(context);
this.data=data;
}


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

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

Information current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.iconid);

}

@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;
}


class MyViewHolder extends RecyclerView.ViewHolder{

TextView title;
ImageView image;

public MyViewHolder(View itemView){
super(itemView);
title=(TextView) itemView.findViewById(R.id.tv);
image=(ImageView) itemView.findViewById(R.id.im);
}


}
}


NavigationDrawer.java

public class Nav_drawer extends Fragment {

private RecyclerView recyclerView;
public static final String PREF_NAME="test_pref";
public static final String KEY_USER_LERNED_DRAWER="user_learned_drawer";
private ActionBarDrawerToggle mactionBarDrawerToggle;
private DrawerLayout mdrawerLayout;
private Sadapter sadapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstantState;
public View view;


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

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LERNED_DRAWER,"false"));

if (savedInstanceState!=null)
{
mFromSavedInstantState=true;
}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View layout=inflater.inflate(R.layout.fragment_nav_drawer, container, false);
recyclerView=(RecyclerView) layout.findViewById(R.id.drawer_list);
sadapter=new Sadapter(getActivity(),getData());
recyclerView.setAdapter(sadapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

return layout;
}
public static List<Information> getData(){
List<Information> data=new ArrayList<>();
int[] icons={R.drawable.ic_home_black_18dp,R.drawable.ic_camera_alt_black_18dp};
String[] titles={"Home","Camera"};

for (int i=0;i<titles.length && i<icons.length; i++)
{
Information current= new Information();
current.iconid=icons[i];
current.title=titles[i];
data.add(current);

}
return data;
}

public void setUp(int fragId, DrawerLayout drawerLayout, Toolbar toolbar)
{
view=getActivity().findViewById(fragId);
mdrawerLayout=drawerLayout;

mactionBarDrawerToggle=new ActionBarDrawerToggle(getActivity(),mdrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){


@Override
public void onDrawerOpened(View drawerView) {
if (!mUserLearnedDrawer)
{
mUserLearnedDrawer=true;
saveToPreferences(getActivity(),KEY_USER_LERNED_DRAWER,mUserLearnedDrawer+"");


}

getActivity().invalidateOptionsMenu();
}

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


if (!mUserLearnedDrawer&&!mFromSavedInstantState)
{

mdrawerLayout.openDrawer(view);
}

mdrawerLayout.addDrawerListener(mactionBarDrawerToggle);
mdrawerLayout.post(new Runnable() {
@Override
public void run() {
mactionBarDrawerToggle.syncState();
}
});







}



public void saveToPreferences(Context context,String preferenceName,String preferenceValue)

{


SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(preferenceName,preferenceValue);
editor.apply();
}


public static String readFromPreferences(Context context,String preferenceName,String defaultValue)

{
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName,defaultValue);

}



}


Information.java

public class Information {

int iconid;
String title;
}


And here are all the xml files

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"

>
<RelativeLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alshariq.materia.MainActivity">

<include
layout="@layout/app_bar"
android:id="@+id/app_bar"/>
</RelativeLayout>

<fragment
android:layout_width="286dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/f1"
app:layout="@layout/fragment_nav_drawer"
android:name="com.example.alshariq.materia.Nav_drawer"
tools:layout="@layout/fragment_nav_drawer"/>


</android.support.v4.widget.DrawerLayout>


appbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/colorPrimary"
android:elevation="4dp"

>

</android.support.v7.widget.Toolbar>


customrow.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:id="@+id/im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:padding="8dp"
android:layout_gravity="center_vertical"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Dummy Text"
android:padding="8dp"
android:id="@+id/tv"
android:layout_gravity="center_vertical"/>

</LinearLayout>


fragmentnavdrawer.xml`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alshariq.materia.Nav_drawer"
android:background="#ffffff"

android:elevation="16dp">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@color/colorPrimary">

<ImageView

android:layout_width="match_parent"
android:layout_height="77dp"
android:src="@mipmap/ic_launcher"
android:layout_marginTop="26dp"/>
</LinearLayout>

<android.support.v7.widget.RecyclerView

android:id="@+id/drawer_list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/container"
android:layout_alignParentStart="true">



</android.support.v7.widget.RecyclerView>


</RelativeLayout>

Answer Source

In your customrow.xml Please specify the layout_height="wrap_content" or layout_height="100dp"

Change your customrow.xml like this,

<?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="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/im"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:padding="8dp"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:padding="8dp"
    android:text="Dummy Text" />
 </LinearLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download