0

我有一个由自定义适配器支持的列表视图。适配器的项目列表中有 17 个项目,但只显示前 2 个项目。我已经跟踪getView()并确认它仅在位置 0 和 1 中被调用。

填充适配器并将其设置在onCreate()parentActivity中。

FavouritesListAdapter adapter = new FavouritesListAdapter(favourites, this);

((ListView) findViewById(R.id.list)).setAdapter(adapter);
adapter.notifyDataSetChanged(); // not needed but put here out of desperation!

适配器签名和getView()方法:

public class FavouritesListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Favourite> entries;

    public FavouritesListAdapter(ArrayList<Favourite> favourites, Context context) {
        this.entries = favourites;
        this.context = context;
    }

public View getView(int position, View convertView, ViewGroup parent) {

    RelativeLayout rl;

    Log.d(LOG_DEBUG_TAG, "Count:" + getCount() ", asking for pos " + position);

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rl = (RelativeLayout) inflater.inflate(R.layout.favourite_tide_table, null);
        convertView = rl;

    } else { // convertView is not null (it's being recycled)

        Favourite thisFavourite = this.getItem(position);

        convertView = thisFavourite.getPortView();

    }

    return convertView;

}

日志输出:

Count:17, asking for pos 0
Count:17, asking for pos 0
Count:17, asking for pos 0
Count:17, asking for pos 1
Count:17, asking for pos 0
Count:17, asking for pos 0

活动 XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <ListView
            android:id="@+id/list"
            android:background="@color/white"
            android:cacheColorHint="#00000000"
            android:divider="#FF0000"
            android:dividerHeight="1px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</ScrollView>

前两项已正确显示。

我错过了什么?谢谢!

4

1 回答 1

2

AListView不能进入 a ScrollView

请参阅Romain Guy 的这个答案。谷歌 I/O 视频中更多细节

如果你真的需要把 a 放进去ListViewScrollView你基本上需要ListView知道它的高度,这可以使用 DougW 回答提到的问题。

于 2013-05-16T11:11:13.947 回答