NavigationView中的ListView只显示ListView的一个子节点

时间:2016-02-24 06:17:46

标签: android listview android-navigationview

我正在尝试使用自定义项目布局显示NavigationView中的项目列表,因此我在其中创建了一个包含listView的布局,并将其作为headerLayout提供给NavigationView。但我遇到的问题是listView只显示一个孩子,虽然传递给它的ArrayList大小不止一个。

我已尝试在论坛和stackoverflow中搜索问题,但到目前为止我找不到任何内容,所以在此发布此问题,提前致谢。

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" />

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>

用于Adapter的{​​{1}}定义如下:

ListView

使用日志我所观察到的是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; } } 被调用了多次,因为getView的大小传递给了构造函数,但只调用了位置0。

1 个答案:

答案 0 :(得分:1)

更改为ListView括在NavigationView内,而不是传递到headerLayout内。如下面的代码所示:

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>