NavigationView 中的 ListView 仅显示 child 个 ListView

ListView inside a NavigationView displays only one child of ListView

我正在尝试使用自定义项目布局显示 NavigationView 中的项目列表,因此我创建了一个内部包含 listView 的布局,并将其作为 headerLayout 提供给 NavigationView .但是我面临的问题是 listView 只显示一个 child 尽管传递给它的 ArrayList 的大小超过一个。

我曾尝试在论坛和 Whosebug 中搜索问题,但到目前为止我找不到任何问题,所以在此发布此问题,在此先感谢。

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>

用于ListViewAdapter定义如下:

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 被调用的次数随着 ArrayList 的大小传递给构造函数,但它仅在位置 0 时被调用。

更改为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>