I am trying to show a list of item in NavigationView
with custom item layout so I have created a layout with listView
inside it and provided it as a headerLayout to NavigationView
. But the problem I am facing is that listView is only showing one child although ArrayList
passed to it has size more than one.
I have tried searching for the problem in forums and stackoverflow but I couldn't find any so far so posting this question here, Thanks in advance.
Code for NavigationView:
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@drawable/ty_bg_texture"
app:headerLayout="@layout/menu_frame_recent_search"
app:menu="@null" />
Code for menu_frame_recent_search.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame_recent_search"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/menu_header"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Recent Bus Searches"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/White"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/topHeaderShadow"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@+id/menu_header"
android:background="@drawable/ty_header_shadow" />
<FrameLayout
android:id="@+id/menu_frame_search_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/menu_header"
android:cacheColorHint="#00000000">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</FrameLayout>
</RelativeLayout>
And the Adapter
used for the ListView
is defined like below:
public class BusSearchHistoryAdapter extends BaseAdapter {
private final Logger log = LoggerFactory.getLogger(BusSearchHistoryAdapter.class);
ArrayList<Search> searchArrayList;
Context context;
public BusSearchHistoryAdapter(Context context, ArrayList<Search> searchArrayList) {
this.searchArrayList = searchArrayList;
this.context = context;
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Search getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
convertView = LayoutInflater.from(this.context).inflate(
R.layout.row_bus_search_history, null);
viewHolder = new ViewHolder();
viewHolder.cities = (TextView) (convertView
.findViewById(R.id.cities));
viewHolder.date = (TextView) convertView.findViewById(R.id.date);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Log.i("View For ", ""+position);
log.debug(convertView.toString());
if ((convertView instanceof LinearLayout)
|| (convertView instanceof RelativeLayout)) {
String fromCity = getItem(position).getFromCity();
String toCity = getItem(position).getToCity();
if (fromCity.length() > 1) {
fromCity = fromCity.substring(0, 1).toUpperCase()
+ fromCity.substring(1);
} else {
fromCity = fromCity.substring(0, 1).toUpperCase();
}
if (toCity.length() > 1) {
toCity = toCity.substring(0, 1).toUpperCase()
+ toCity.substring(1);
} else {
toCity = toCity.substring(0, 1).toUpperCase();
}
// cities.setText(fromCity+" - "+toCity);
viewHolder.cities.setText(fromCity + " " + Statics.THIN_ARROW_RIGHT + " "
+ toCity);
viewHolder.cities.setTextColor(Color.WHITE);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd MMMM");
viewHolder.date.setText(sdf.format(getItem(position).getDate())
);
viewHolder.date.setTextColor(Color.WHITE);
}
return convertView;
}
public class ViewHolder{
TextView cities;
TextView date;
}
}
Using the log what I have observed is that getView
is getting called for number of times as the size of ArrayList
passed to the constructor but it is only called for position 0.
Change To: ListView
enclosed within NavigationView
instead of passing inside the headerLayout
. Like given in below code:
In activity_main.xml:-
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/menu"
app:menu="@null" >
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</android.support.design.widget.NavigationView>