ListView 只显示第一项
ListView only shows first item
我想在 ListView
上显示 ArrayList<String>
的所有项目。我为此使用 ArrayAdapter<String>
。问题是只显示了 ArrayList
的第一项(在我的示例中为 'foo')。有人能告诉我为什么吗?我错过了什么吗?
我使用来自 Android Studio 2 的生成代码(Tabbed Action Bar Spinner Activity)制作了一个最小示例。
我的FooActivity
:
public class FooActivity extends AppCompatActivity {
...
public static class PlaceholderFragment extends Fragment {
private ListView fooListView;
private ArrayAdapter<String> mAdapter;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_foo, container, false);
fooListView = (ListView) rootView.findViewById(R.id.foo_list);
updateList();
return rootView;
}
private void updateList() {
ArrayList<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getContext(),
R.layout.item_foo,
R.id.foo_string,
strings);
fooListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
}
我的fragment_foo.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.foo.activities.FooActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/foo_list"
android:layout_below="@+id/total_activities"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
和 item_foo.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/foo_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_string"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
如果有帮助,我可以 post 生成的 activity_foo.xml
(我没有在那里做任何更改)和完整的 FooActivity
class。
在任何 ListView
上,如果您使用 android:layout_height="wrap_content"
,您将只会看到一项。尝试 android:layout_height="match_parent"
。如果 ListView
仅占据布局的一部分,则需要带权重的 LinearLayout
或带约束的 RelativeLayout
。但是 wrap_content
永远不会在 ListView
上工作。
问题实际上是我的 activity_foo.xml
有一个 NestedScrollView
。我添加了 android:fillViewport="true"
,现在它显示了每个项目。
我的 NestedScrollView
现在看起来像这样:
<android.support.v4.widget.NestedScrollView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"/>
您的列表视图看起来像这样
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/foo_list"
android:layout_marginTop="10dp"/>
我已从您的列表视图中删除android:layout_below="@+id/total_activities"
和
android:layout_alignParentBottom="true"
对于其他人:可以将 ListView 嵌套在另一个布局中,也可以在 ListView 周围的布局上使用 android:layout_height="wrap_content"
。 (只要 outer-most 布局允许 match_parent)
但是,当用于填充 ListView 的 TextView 是用 layout_height="match_parent"
定义时,情况不佳。它将覆盖第一行之后的所有后续行。
差:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
好:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
我想在 ListView
上显示 ArrayList<String>
的所有项目。我为此使用 ArrayAdapter<String>
。问题是只显示了 ArrayList
的第一项(在我的示例中为 'foo')。有人能告诉我为什么吗?我错过了什么吗?
我使用来自 Android Studio 2 的生成代码(Tabbed Action Bar Spinner Activity)制作了一个最小示例。
我的FooActivity
:
public class FooActivity extends AppCompatActivity {
...
public static class PlaceholderFragment extends Fragment {
private ListView fooListView;
private ArrayAdapter<String> mAdapter;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_foo, container, false);
fooListView = (ListView) rootView.findViewById(R.id.foo_list);
updateList();
return rootView;
}
private void updateList() {
ArrayList<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getContext(),
R.layout.item_foo,
R.id.foo_string,
strings);
fooListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
}
我的fragment_foo.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.foo.activities.FooActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/foo_list"
android:layout_below="@+id/total_activities"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
和 item_foo.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/foo_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_string"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
如果有帮助,我可以 post 生成的 activity_foo.xml
(我没有在那里做任何更改)和完整的 FooActivity
class。
在任何 ListView
上,如果您使用 android:layout_height="wrap_content"
,您将只会看到一项。尝试 android:layout_height="match_parent"
。如果 ListView
仅占据布局的一部分,则需要带权重的 LinearLayout
或带约束的 RelativeLayout
。但是 wrap_content
永远不会在 ListView
上工作。
问题实际上是我的 activity_foo.xml
有一个 NestedScrollView
。我添加了 android:fillViewport="true"
,现在它显示了每个项目。
我的 NestedScrollView
现在看起来像这样:
<android.support.v4.widget.NestedScrollView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true"/>
您的列表视图看起来像这样
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/foo_list"
android:layout_marginTop="10dp"/>
我已从您的列表视图中删除android:layout_below="@+id/total_activities"
和
android:layout_alignParentBottom="true"
对于其他人:可以将 ListView 嵌套在另一个布局中,也可以在 ListView 周围的布局上使用 android:layout_height="wrap_content"
。 (只要 outer-most 布局允许 match_parent)
但是,当用于填充 ListView 的 TextView 是用 layout_height="match_parent"
定义时,情况不佳。它将覆盖第一行之后的所有后续行。
差:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
好:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>