【问题标题】:ListView only shows first itemListView 只显示第一项
【发布时间】:2016-09-13 21:25:09
【问题描述】:

我想在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 类。

【问题讨论】:

    标签: java android listview android-arrayadapter


    【解决方案1】:

    问题实际上是我的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"/>
    

    【讨论】:

      【解决方案2】:

      在任何ListView 上,如果您使用android:layout_height="wrap_content",您只会看到一项。试试android:layout_height="match_parent"。如果ListView 只占布局的一部分,您将需要一个带有权重的LinearLayout 或一个带有约束的RelativeLayout。但是wrap_content 永远不会在ListView 上工作。

      【讨论】:

        【解决方案3】:

        您的列表视图看起来像这样

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

        【讨论】:

          【解决方案4】:

          对于其他人:可以将 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"
              />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-22
            • 2017-01-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多