Android開發經驗分享-GridView、ListView內容錯亂
在使用GridView、ListView的過程中遇到內容錯亂的問題,費了較長時間才找到問題的根源,特地總結一下。
1、在自定義adapter中沒有給每一項都設定內容導致內容錯亂:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if( null == convertView ){
mGridHolder = new GridHolder( );
convertView = mLayoutInflater.inflate( R.layout.view_log_word_item_layout, null );
mGridHolder.mBackTopLayout = (LinearLayout)convertView.findViewById( R.id.logItemTopLayoutId);
mGridHolder.mBackBottomLayout = ( RelativeLayout )convertView.findViewById( R.id.logItemBottomLayoutId );
mGridHolder.mRightWordTxt = ( TextView )convertView.findViewById( R.id.rightWordTxtId );
mGridHolder.mUserWriteImg = ( ImageView )convertView.findViewById( R.id.userWordImgId );
mGridHolder.mWriteResultImg = ( ImageView )convertView.findViewById( R.id.writeResultImgId );
mGridHolder.mReasonTxt = ( TextView )convertView.findViewById( R.id.reasonTxtId );
mGridHolder.mRightWordTxt.setTypeface( mTypeface );
convertView.setTag( mGridHolder );
}else{
mGridHolder = ( GridHolder )convertView.getTag( );
}
showContent( mDictationInfoList.get( position ) );
return convertView;
}
@SuppressWarnings("deprecation")
private void showContent( DictationInfo dictationInfo ){
mGridHolder.mRightWordTxt.setText( dictationInfo.getmWord( ) );
if( null != dictationInfo.getmDrawable( ) ){
mGridHolder.mUserWriteImg.setBackgroundDrawable( dictationInfo.getmDrawable( ) );
if( !TextUtils.isEmpty( dictationInfo.getDetailReason( ) ) ){
mGridHolder.mReasonTxt.setText( dictationInfo.getDetailReason( ) );
}else{
mGridHolder.mReasonTxt.setText( "" );
}
setWriteResult( dictationInfo.getmWriteResult( ) );
}else{
// 沒有為內容項為空的項設定內容
}
}
上面是一個自定義adapter中的一段程式碼,在getView中為每一項設定內容時只處理了有內容的項,沒有內容的項沒有賦值,導致在滑動內容時內容錯亂,分析發現這是由於使用了adapter的快取機制導致的,正確的處理方式是需要為內容為空的項也要賦值,修改後的程式碼如下:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if( null == convertView ){
mGridHolder = new GridHolder( );
convertView = mLayoutInflater.inflate( R.layout.view_log_word_item_layout, null );
mGridHolder.mBackTopLayout = (LinearLayout)convertView.findViewById( R.id.logItemTopLayoutId);
mGridHolder.mBackBottomLayout = ( RelativeLayout )convertView.findViewById( R.id.logItemBottomLayoutId );
mGridHolder.mRightWordTxt = ( TextView )convertView.findViewById( R.id.rightWordTxtId );
mGridHolder.mUserWriteImg = ( ImageView )convertView.findViewById( R.id.userWordImgId );
mGridHolder.mWriteResultImg = ( ImageView )convertView.findViewById( R.id.writeResultImgId );
mGridHolder.mReasonTxt = ( TextView )convertView.findViewById( R.id.reasonTxtId );
mGridHolder.mRightWordTxt.setTypeface( mTypeface );
convertView.setTag( mGridHolder );
}else{
mGridHolder = ( GridHolder )convertView.getTag( );
}
showContent( mDictationInfoList.get( position ) );
return convertView;
}
@SuppressWarnings("deprecation")
private void showContent( DictationInfo dictationInfo ){
mGridHolder.mRightWordTxt.setText( dictationInfo.getmWord( ) );
if( null != dictationInfo.getmDrawable( ) ){
mGridHolder.mUserWriteImg.setBackgroundDrawable( dictationInfo.getmDrawable( ) );
if( !TextUtils.isEmpty( dictationInfo.getDetailReason( ) ) ){
mGridHolder.mReasonTxt.setText( dictationInfo.getDetailReason( ) );
}else{
mGridHolder.mReasonTxt.setText( "" );
}
setWriteResult( dictationInfo.getmWriteResult( ) );
}else{
mGridHolder.mUserWriteImg.setBackgroundResource( Color.TRANSPARENT );
mGridHolder.mReasonTxt.setText( "" );
mGridHolder.mWriteResultImg.setBackgroundResource( R.drawable.log_unknown );
mGridHolder.mBackTopLayout.setBackgroundResource( R.drawable.log_item_wrong_top_background );
mGridHolder.mBackBottomLayout.setBackgroundResource( R.drawable.log_item_wrong_bottom_background );
}
}
2、在自定義adapter的getview方法中,每一項的內容載入時間過長導致內容錯亂。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mViewHolder = new ViewHolder( );
convertView = mLayoutInflater.inflate( R.layout.adapter_score_list_layout, null );
mViewHolder.mScoreLayout = ( RelativeLayout )convertView.findViewById( R.id.scoreLayoutId );
mViewHolder.mScoreLayout.setClickable( false );
mViewHolder.mPositionTxt = ( TextView )convertView.findViewById( R.id.positionTxtId );
mViewHolder.mUserNameTxt = ( TextView )convertView.findViewById( R.id.userNameTxtId );
mViewHolder.mUserNameTxt.setTypeface( mTypeface );
mViewHolder.mUserIconImg = ( ImageView )convertView.findViewById( R.id.userIconImgId );
mViewHolder.mUserScoreTxt = ( TextView )convertView.findViewById( R.id.userScoreTxtId );
showContent( position, mUserInfoList.get( position ) );
return convertView;
}
private void showContent( int index, UserScoreInfo userInfo ){
// 從網路獲取頭像
handleUserIcon( mImageLoader, userInfo, mUserId );
mViewHolder.mPositionTxt.setVisibility(TextView.VISIBLE);
mViewHolder.mPositionTxt.setText( "第" + userInfo.getScoreRank( ) + "名" );
if( mUserId.equals( userInfo.getUserId( ) ) ){
mMyPosition = index;
mViewHolder.mScoreLayout.setBackgroundResource( R.drawable.chinese_dictation_score_list_select );
}else{
mViewHolder.mScoreLayout.setBackgroundResource( R.drawable.chinese_dictation_score_list_normal );
}
String userName = userInfo.getNickName( );
if( !TextUtils.isEmpty( userName ) ){
userName = ( userName.length( ) > 10 )?( userName.substring(0, 7) + "..." ):userName;
}else{
userName = "佚名";
}
if( mUserId.equals( userInfo.getUserId( ) ) ){
String localUserName = PersonalInfo.getCurrentUserName( mContext );
mMyPosition = index;
mViewHolder.mScoreLayout.setBackgroundResource( R.drawable.chinese_dictation_score_list_select );
if( TextUtils.isEmpty( userName ) || ( !localUserName.equals( userName ) ) ){
mViewHolder.mUserNameTxt.setText( localUserName );
}else{
mViewHolder.mUserNameTxt.setText( userName );
}
}else{
mViewHolder.mScoreLayout.setBackgroundResource( R.drawable.chinese_dictation_score_list_normal );
mViewHolder.mUserNameTxt.setText( userName );
}
mViewHolder.mUserScoreTxt.setText( StringUtils.getSplitByComma( userInfo.getScore( ) + "" ) );
}
上面是一個獲取使用者排名列表所自定義adapter中的一段程式碼,handleUserIcon方法是從網路獲取使用者頭像,在滑動排名列表的時候發現使用者頭像會錯亂,最後跟蹤找到具體的原因是由於從網路獲取使用者頭像耗時,正確的做法應該是在獲取完成使用者頭像之後再通知adapter更新。
在使用gridview和listview的過程中,由於需要載入大量的內容,android建議我們採用viewholder的快取機制,這樣能夠提高列表載入的效率,但是在使用的過程中一定要注意上面這兩個問題,否則可能會讓你很頭疼。
相關文章
- Android開發者峰會:Android應用效能優化經驗分享Android優化
- Android開發經驗總結Android
- Android SDK 開發經驗淺談Android
- 直播app開發,Android ListView好友列表展示APPAndroidView
- 規則引擎開發經驗分享 - reddit
- PagerDuty的API開發經驗分享 – IncrementAPIREM
- 小程式·雲開發 專案開發經驗分享
- Android大廠面試經驗分享Android面試
- Flutter入門進階之旅(十四)ListView&GridViewFlutterView
- Flutter基礎(七)Scrolling Widget之ListView、GridView、PageViewFlutterView
- Flutter 基礎(七)Scrolling Widget 之 ListView、GridView、PageViewFlutterView
- UWP 取消GridView、ListView滑鼠選中、懸停效果View
- 【經驗】學習android開發的步驟Android
- 頭條Android客戶端開發面經分享Android客戶端
- Laplace分佈運算元開發經驗分享
- Nuxt開發經驗分享,讓你踩少點坑!UX
- 經驗分享
- android開發(3):列表listview的實現 | 下拉重新整理AndroidView
- 工程師經驗分享2 --- 嵌入式大牛開發經驗心得之學習方向工程師
- android中的ListViewAndroidView
- 開發者分享優化技巧,將PC VR內容移植到Quest優化VR
- Android 截圖與 WebView 長圖分享經驗總結AndroidWebView
- android開啟軟鍵盤部分內容上移Android
- 初嘗微信小程式開發與實踐經驗分享微信小程式
- 「Vue實戰」武裝你的專案 - 開發經驗分享Vue
- android 設定listview高度AndroidView
- 報錯內容解決
- android開發中如何動態獲取listview中的item的值AndroidView
- 膜拜大牛!3年Android開發工程師面試經驗分享,最全的BAT大廠面試題整理Android工程師BAT面試題
- 開發者每日精選內容
- Android開發錯誤集錦Android
- 自媒體運營經驗:教你如何打造好的內容?
- 安卓應用效能除錯和優化經驗分享安卓除錯優化
- 分享 15 個 Vue3 全家桶開發的避坑經驗Vue
- 系統學習大資料需要學習哪些內容,五年大資料工程師經驗分享大資料工程師
- Polymer使用經驗分享
- 【高中經驗分享】2021.11.29
- 分享swoole/go底層內容Go
- requests請求返回內容 中文亂碼問題