ListView仅显示第一项 [英] ListView only shows first item

查看:95
本文介绍了ListView仅显示第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ListView上显示ArrayList<String>的所有项目.我为此使用ArrayAdapter<String>.问题在于,仅显示了ArrayList的第一项(在我的示例中为'foo').有人可以告诉我为什么吗?我想念什么吗?

I want to display all items of an ArrayList<String> on a ListView. I use an ArrayAdapter<String> for this. The problem is that only the first item ('foo' in my example) of the ArrayList is shown. Can someone tell me why? Am I missing something?

我已经使用Android Studio 2中生成的代码(带标签的操作栏微调器活动)制作了一个最小的示例.

I've made an minimal example using the generated code (Tabbed Action Bar Spinner Activity) from Android Studio 2.

我的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类.

If it helps I can post the generated activity_foo.xml (I didn't made any changes there) and the complete FooActivity class too.

推荐答案

问题实际上是我的activity_foo.xml,其中有一个NestedScrollView.我添加了android:fillViewport="true",现在它显示了每个项目.

The Problem actually was my activity_foo.xml which had a NestedScrollView. I've added android:fillViewport="true" and now it shows every item.

我的NestedScrollView现在看起来像这样:

My NestedScrollView now looks like this:

<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仅显示第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆