怎么优化ListView?
来源:互联网 发布:淘宝好的女装店 编辑:程序博客网 时间:2024/04/29 19:47
public View getView( int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ConversationActivity. this).inflate(R.layout.layout_conversation_item,
null);
ConversationItemTag tag = new ConversationItemTag();
tag. portaitView = (PortraitView) convertView.findViewById(R.id.conv_portrait );
tag. nameView = (TextView) convertView.findViewById(R.id.conv_name );
tag. noteView = (TextView) convertView.findViewById(R.id.conv_note );
tag. dateView = (TextView) convertView.findViewById(R.id.conv_date );
tag. countView = (TextView) convertView.findViewById(R.id.conv_count );
tag. newmsgView = (TextView) convertView.findViewById(R.id.public_account_new_msg );
convertView.setTag(tag);
}
ConversationItemTag tag = (ConversationItemTag) convertView.getTag();
...
}
1. 复用convertView
2. 若一个convertView新生成,将它的子View对象存起来,不用每次在控件树里再找一遍
先看一下复用机制:
http://blog.csdn.net/dxswzj/article/details/10701783
例子:
在回收池里(mScrapViews)木有view的时候,输入参数convertView是null,否则,输入参数是回收池里的某view。
然后复用之。
public View getView (int position, View convertView, ViewGroup parent) {
View v;
if (convertView != null)
v = convertView;
else {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.agenda_conf_hall_item ,
null);
}
。。。
}
@param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
@return A View corresponding to the data at the specified position.
View getView(int position, View convertView, ViewGroup parent);
private View[] mActiveViews = new View[0];
private ArrayList<View>[] mScrapViews;
void fillActiveViews(int childCount, int firstActivePosition) {
if (mActiveViews.length < childCount) {
mActiveViews = new View[childCount];
}
mFirstActivePosition = firstActivePosition;
final View[] activeViews = mActiveViews;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
AbsListView.LayoutParams lp = (AbsListView.LayoutParams) child.getLayoutParams();
// Don't put header or footer views into the scrap heap
if (lp != null && lp.viewType != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
// Note: We do place AdapterView.ITEM_VIEW_TYPE_IGNORE in active views.
// However, we will NOT place them into scrap views.
activeViews[i] = child;
}
}
}
//回收机制:
View obtainView(int position, boolean[] isScrap) {
isScrap[0] = false;
View scrapView;
scrapView = mRecycler.getTransientStateView(position);
if (scrapView != null) {
return scrapView;
}
//在mScrapViews拿对应位置的view
scrapView = mRecycler.getScrapView(position);
View child;
//若能拿到
if (scrapView != null) {
child = mAdapter.getView(position, scrapView, this);
if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
//若新建了一个子View
if (child != scrapView) {
//把老View放到mScrapViews里面,返回新View
mRecycler.addScrapView(scrapView, position);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
//若没有新建子View
} else {
isScrap[0] = true;
child.dispatchFinishTemporaryDetach();
}
//若不能拿到,新建一个子View
} else {
child = mAdapter.getView(position, null, this);
if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
}
if (mAdapterHasStableIds) {
final ViewGroup.LayoutParams vlp = child.getLayoutParams();
LayoutParams lp;
if (vlp == null) {
lp = (LayoutParams) generateDefaultLayoutParams();
} else if (!checkLayoutParams(vlp)) {
lp = (LayoutParams) generateLayoutParams(vlp);
} else {
lp = (LayoutParams) vlp;
}
lp.itemId = mAdapter.getItemId(position);
child.setLayoutParams(lp);
}
if (AccessibilityManager.getInstance(mContext).isEnabled()) {
if (mAccessibilityDelegate == null) {
mAccessibilityDelegate = new ListItemAccessibilityDelegate();
}
if (child.getAccessibilityDelegate() == null) {
child.setAccessibilityDelegate(mAccessibilityDelegate);
}
}
return child;
}
http://blog.csdn.net/dxswzj/article/details/10701783
例子:
在回收池里(mScrapViews)木有view的时候,输入参数convertView是null,否则,输入参数是回收池里的某view。
然后复用之。
public View getView (int position, View convertView, ViewGroup parent) {
View v;
if (convertView != null)
v = convertView;
else {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.agenda_conf_hall_item ,
null);
}
。。。
}
@param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
@return A View corresponding to the data at the specified position.
View getView(int position, View convertView, ViewGroup parent);
private View[] mActiveViews = new View[0];
private ArrayList<View>[] mScrapViews;
void fillActiveViews(int childCount, int firstActivePosition) {
if (mActiveViews.length < childCount) {
mActiveViews = new View[childCount];
}
mFirstActivePosition = firstActivePosition;
final View[] activeViews = mActiveViews;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
AbsListView.LayoutParams lp = (AbsListView.LayoutParams) child.getLayoutParams();
// Don't put header or footer views into the scrap heap
if (lp != null && lp.viewType != ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
// Note: We do place AdapterView.ITEM_VIEW_TYPE_IGNORE in active views.
// However, we will NOT place them into scrap views.
activeViews[i] = child;
}
}
}
//回收机制:
View obtainView(int position, boolean[] isScrap) {
isScrap[0] = false;
View scrapView;
scrapView = mRecycler.getTransientStateView(position);
if (scrapView != null) {
return scrapView;
}
//在mScrapViews拿对应位置的view
scrapView = mRecycler.getScrapView(position);
View child;
//若能拿到
if (scrapView != null) {
child = mAdapter.getView(position, scrapView, this);
if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
//若新建了一个子View
if (child != scrapView) {
//把老View放到mScrapViews里面,返回新View
mRecycler.addScrapView(scrapView, position);
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
//若没有新建子View
} else {
isScrap[0] = true;
child.dispatchFinishTemporaryDetach();
}
//若不能拿到,新建一个子View
} else {
child = mAdapter.getView(position, null, this);
if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
if (mCacheColorHint != 0) {
child.setDrawingCacheBackgroundColor(mCacheColorHint);
}
}
if (mAdapterHasStableIds) {
final ViewGroup.LayoutParams vlp = child.getLayoutParams();
LayoutParams lp;
if (vlp == null) {
lp = (LayoutParams) generateDefaultLayoutParams();
} else if (!checkLayoutParams(vlp)) {
lp = (LayoutParams) generateLayoutParams(vlp);
} else {
lp = (LayoutParams) vlp;
}
lp.itemId = mAdapter.getItemId(position);
child.setLayoutParams(lp);
}
if (AccessibilityManager.getInstance(mContext).isEnabled()) {
if (mAccessibilityDelegate == null) {
mAccessibilityDelegate = new ListItemAccessibilityDelegate();
}
if (child.getAccessibilityDelegate() == null) {
child.setAccessibilityDelegate(mAccessibilityDelegate);
}
}
return child;
}
0 0
- 怎么优化ListView?
- ListView优化
- listView优化
- ListView优化
- ListView优化
- ListView优化
- ListView优化
- listView优化
- listview 优化
- ListView优化
- listView优化
- ListView 优化
- ListVIew优化
- ListView优化
- ListView优化
- listview优化
- Listview 优化
- ListView优化
- Android组件在代码中修改高度
- 判断空间上三个点是否共线问题【找bug篇】
- debian linux 系统安装镜像 ISO 文件下载集合
- 基于MINA框架快速开发网络应用程序
- NSArray利用Cocoa框架进行汉字排序
- 怎么优化ListView?
- 图片随机小角度堆叠效果,可用于相册缩略图叠加样式【长宽最优化】
- [算法系列之三十二]1的数目
- My First Day
- 4种方法让SpringMVC接收多个对象(转:http://blog.csdn.net/lutinghuan/article/details/46820023)
- 银行系统
- Ant高级-path和fileset
- SQL%NOTFOUND
- 这是前端最好的时代——论前端的“三化”建设