【发布时间】:2012-02-22 14:13:05
【问题描述】:
我创建了一个自定义 ListView。我尝试优化我的 ListView 适配器的 getView 方法,但我仍然遇到延迟。
我在适配器中做错了什么导致我的 ListView 滞后?
或者如果它看起来不错,可能是什么导致了延迟?
ListView中一行的xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="@color/all_white"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="@color/all_white"
android:gravity="center">
<TextView
android:layout_width="40dip"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
<Button
android:layout_width="197dip"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:background="@drawable/button_300"
android:textColor="@color/all_white"
/>
</LinearLayout>
</LinearLayout>
我的适配器的代码是:
公共类 MultiWorkspaceAdapter2 扩展 BaseAdapter {
/*
* A map of the workspaces of a user, with key == first letter of the workspace title
* and value == the list of WorkspaceInfos for that key.
*/
private LinkedHashMap<String, List<WorkspaceInfo>> _workspacesMap;
/*
* Array of keys in order to get the values in getItem.
*/
private String[] _keys;
private Activity _activity;
public MultiWorkspaceAdapter2(Activity activity, LinkedHashMap<String, List<WorkspaceInfo>> map) {
_workspacesMap = map;
_keys = (String[]) _workspacesMap.keySet().toArray(new String[_workspacesMap.size()]);
_activity = activity;
}
public int getCount() {
return _workspacesMap.size();
}
public Object getItem(int position) {
_keys = (String[]) _workspacesMap.keySet().toArray(new String[_workspacesMap.size()]);
List<WorkspaceInfo> list = new ArrayList<WorkspaceInfo>();
try {
if (_keys[position] != null) {
list = _workspacesMap.get(_keys[position]);
}
} catch (ArrayIndexOutOfBoundsException e) {
list = null;
}
return list;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup viewGroup = null;
int numRows = 0;
if (convertView != null) {
viewGroup = (ViewGroup) convertView;
} else {
LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewGroup = (ViewGroup) inflater.inflate(R.layout.multi_workspace_layout, parent, false);
_keys = (String[]) _workspacesMap.keySet().toArray(new String[_workspacesMap.size()]);
List<WorkspaceInfo> workspaces = new ArrayList<WorkspaceInfo>();
try {
if (_keys[position] != null) {
workspaces = _workspacesMap.get(_keys[position]);
//Collections.sort(workspaces, new InfoNameComparator());
}
} catch (ArrayIndexOutOfBoundsException e) {
//
}
numRows = viewGroup.getChildCount();
//Find how many buttons are in a row.
int buttonsInRow = ((ViewGroup) viewGroup.getChildAt(0)).getChildCount();
int totalRows = 0;
int size = workspaces.size();
if (size > 0) {
totalRows = size / buttonsInRow + 1;
}
if (numRows < totalRows) {
for (int i = numRows; i < totalRows; i++) {
View view;
LayoutInflater inflater2 = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (View) inflater2.inflate(R.layout.list_row_multi_ws, viewGroup, false);
viewGroup.addView(view, i);
}
} else if (numRows > totalRows) {
for (int i = numRows; i > totalRows; i--) {
viewGroup.removeViewAt(totalRows);
}
}
//Iterate over workspaces
Iterator<WorkspaceInfo> iterator = workspaces.iterator();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
//Linear layout child containing buttons and textview.
ViewGroup temp = (ViewGroup) viewGroup.getChildAt(i);
for (int j = 0; j < temp.getChildCount(); j++) {
if (j == 0) {
//The letter these workspaces start with
TextView textview = (TextView) temp.getChildAt(j);
String letter = _keys[position];
textview.setText(_keys[position]);
if (i != 0 && temp.getId() != R.id.workspaces_portrait) {
textview.setVisibility(View.INVISIBLE);
} else if (i != 0) {
textview.setVisibility(View.GONE);
}
} else {
//A workspace button
Button button = (Button) temp.getChildAt(j);
if (iterator.hasNext()) {
//Get the next workspace
final WorkspaceInfo workspace = iterator.next();
//Get the title of the workspace and set as text.
String buttonText = workspace.getTitle();
button.setText(buttonText);
button.setVisibility(View.VISIBLE);
//Add OnClickListener
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//If the workspace is pending, handle the invitation.
if (workspace.getStatus() == StaticFields.PENDING_WORKSPACE) {
((SkootWorkspacesActivity) _activity).handleInvitation(workspace);
} else {
//open the workspace
((SkootWorkspacesActivity) _activity).openWorkspace(workspace);
}
}
});
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
((SkootWorkspacesActivity) _activity).makeDeleteDialog(workspace);
return true;
}
});
} else {
button.setVisibility(View.GONE);
}
}
}
}
}
return viewGroup;
}
我将此适配器添加到 MergeAdapter 并使用 MergeAdapter 在 ListView 上执行 setAdapter。
谢谢
【问题讨论】:
-
作为一个小改进,您可以修改行布局,这样您就没有 2 个
LinearLayout来做基本相同的事情。 -
我把它改成了一行,但我仍然有很多滞后。
-
当我快速向上或向下滑动屏幕时会发生这种情况。如果我慢慢滚动,我看不到滞后。
-
布局更改对您没有太大帮助。尝试改进
getView()方法的代码。如果您的 convertView 为空,您将创建LayoutInflater对象以膨胀布局,但在下面的 for 循环中,您再次创建LayoutInflater每次迭代(为什么不将LayoutInflater作为您调用膨胀的适配器的成员方法? )。即使您不确定是否会使用它,您也可以创建一个空的ArrayList。还尝试实现ViewHolder模式,正确实施此模式将提高性能。 -
感谢您的建议,我会落实的。
标签: android listview optimization adapter