[英]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>
如果有帮助,我可以发布生成的activity_foo.xml
(我没有在那里做任何更改)和完整的FooActivity
类。
问题实际上是我的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_height="wrap_content"
,您只会看到一项。 试试android:layout_height="match_parent"
。 如果ListView
只占用布局的一部分,您将需要带LinearLayout
重的LinearLayout
或带有约束的RelativeLayout
。 但是wrap_content
永远不会对ListView
的高度起作用。
你的 Listview 看起来像这样
<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"
也可以。 (只要最外层的 Layout 允许 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"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.