【发布时间】:2014-03-22 21:15:33
【问题描述】:
我有一个 ListView,其中每一行都是图像+文本。我已经使用 ViewHolder 将这个列表实现为一个单独的列表来优化滚动。 现在我尝试使用相同的视觉效果,但这次是作为可扩展列表视图的一部分。我这样做了,虽然在视觉上看起来一样,但滚动自然是粘性的……所以问题是:
如何在 Expandable ListView 中使用 ViewHolder 技术?
我认为必须在 getChildView() 方法中完成某些操作,但我对这种技术的经验不足,无法自己弄清楚细节。这是适配器和子布局。任何帮助将不胜感激!
public class MyExpandableListAdapter2 extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public LayoutInflater inflater;
public Activity activity;
public MyExpandableListAdapter2(Activity act, SparseArray<Group> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
convertView = inflater.inflate(R.layout.listrow_details2, null);
text = (TextView) convertView.findViewById(R.id.expandable_list_child_view2);
// Complicated code where I create a bitmap programmatically and set
// it as drawable on the TextView along with the appropriate text.
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group2, null);
}
Group group = (Group) getGroup(groupPosition);
((TextView) convertView).setText(group.string);
((TextView) convertView).setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
//((TextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
子布局:listrow_details2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="40dp" >
<TextView
android:id="@+id/expandable_list_child_view2"
android:clickable="true"
android:background="@layout/transparent_text_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:gravity="top|left"
android:text="@string/hello_world"
android:textSize="14sp" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d3d3d3" />
</LinearLayout>
【问题讨论】:
-
视图持有者对性能的影响很小,我不知道为什么人们真正使用它
-
从我读到的内容来看,它提高了 15% 的性能......除此之外,我发现两种方法之间的滚动平滑度有所不同,所以我想它会产生足够的影响陷入困境......
-
加油,SimpleCursorAdapter 不使用任何支架,但它足够快
标签: android expandablelistview expandablelistadapter