【发布时间】:2015-11-25 04:34:39
【问题描述】:
每次向上或向下滚动时都会调用CustomAdapter 中的getView() 方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.projectlist, null);
holder = new ViewHolder();
holder.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
holder.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
holder.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
holder.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (projectItems.size() <= 0) {
[...]
} else {
[...]
}
}
return vi;
}
ListView XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:focusable="false"
android:clickable="false"
android:smoothScrollbar="true" />
</LinearLayout>
为什么每次向上或向下滚动时都会调用它?
假设,我显示了 10 个项目。如果我向下滚动,getView() 会调用一次,它会再显示一个项目。如果我硬滚动并想转到列表的末尾。它会被多次调用,直到数据被加载并显示出来。
如果我想向上滚动,就会发生同样的情况。
我可以做些什么来防止多次调用getView()或者有必要吗?
亲切的问候
【问题讨论】:
-
多次调用getView()方法是ListView适配器的正常行为
-
谢谢!我不知道!
-
在滚动 ListView 时,对于每个项目,都会调用 getView(),它会为您的 ListView 项目返回一个视图。它是一个很棒的默认功能,但是当你滚动时,它必须根据 ListView 中项目的位置来维护值,获取 getView() 方法。
标签: android listview android-listview custom-adapter