2

在這裏尋找一些答案之後,我發現自己處於令人不安的狀況,在這種情況下,Listview真的讓我感到很緊張。如何在滾動後保存和維護ListView項目狀態?

下面是我找的問題:

Maintain ListView Item State

How to save state of CheckBox while scrolling in ListView?

我使用的是自定義的適配器爲以下自定義行。

我的列表視圖是簡單,因爲它正在顯示由三個元件的一個自定義的行:

1)ImageView顯示聯繫人的圖片在一個圓圈裁剪;

2)TextView顯示聯繫人姓名作爲純文本;

3)最後是ImageView,其持有CheckBox的嘌呤。

請集中的最後一個元素上。 ImageView CheckBox就像點擊一樣改變其src。 當用戶點擊時,ImageView將根據其以前的狀態在檢查標誌和未檢查標誌之間切換。可能的狀態是:{checked | unchecked}

到目前爲止好。 但是,只要我滾動ListView,任何上述更改將消失,因爲Android回收未使用的視圖。 這就是所謂的ViewHolder模式。不幸的是,這種模式failling我在兩個問題上:

首先,滾動時,我的組織,在-AN-字母順序列表視圖被雜亂無章。 例如不知何故,沒有任何理由,第一個顯示的聯繫人名稱將在滾動後的ListView中再次顯示。這可以發生在任何行!因此,似乎未使用的視圖被錯誤地重複使用。

其次,按照第一個問題,檢查的狀態似乎留下來,但並非總是如此,如果它留下來,它很可能停留在錯誤的行...並能隨機發生的,課程。因此ViewHoder不是一個可行的解決方案。

之前discouvering的ViewHolder模式,我一直在使用一個HashMap存儲在點擊物品位置如下:

ContactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public final void onItemClick(final AdapterView<?> adapterView, final View view, 
              final int position, final long id) { 

      final ImageView check = (ImageView) view.findViewById(R.id.checkImage); 
      final TextView name = (TextView) view.findViewById(R.id.contactName); 
      final Boolean isChecked = Boolean.valueOf(checkedContactsList.isChecked(position)); 

      if (isChecked != null && isChecked.booleanValue() == true) { 
       check.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.unchecked_sign)); 
       checkedContactsList.(position); 
      } else { 
       check.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.checked_sign)); 
       checkedContactsList.add(position, true); 
      } 
     } 
    }); 

我嘗試添加一個不同的值,而不是position。 我試過ContactsListView.getPositionForView(view) 我也嘗試過使用視圖的ID,但它仍然不起作用。 我希望我可以使用ContactsListView.getSelectedItemPosition()但沒有選擇的情況下,因爲我中處理觸摸它返回-1/Click事件。

這是我Custom Adapter看起來像:

public final View getView(final int position, 
          final View convertView, final ViewGroup parent) { 
    final LayoutInflater inflater = (LayoutInflater) this.context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View contactRowView = inflater.inflate(R.layout.contact_row, parent, false); 

    final ImageView contactPic = (ImageView) contactRowView.findViewById(R.id.contactPic); 
    final TextView contactName = (TextView) contactRowView.findViewById(R.id.contactName); 
    final ImageView checkImage = (ImageView) contactRowView.findViewById(R.id.checkImage); 
    // the list is the same as above and therefore contains the exact same entries 
    if (this.checkedContactsList.isChecked(position)) 
     checkImage.setImageDrawable(this.context.getResources().getDrawable(R.drawable.checked_sign)); 

    contactPic.setImageBitmap(cropePictureInCircle(this.contacts.get(position).getPicture())); 
    contactName.setText(this.contacts.get(position).getName()); 
    return contactRowView; 
} 

也可把選中的行檢查,未選中的行中給定的字母順序排列選中的好辦法?

謝謝!

回答

1

對於列表位置的變化,我知道解決方案,但對於第二個問題,我仍然在尋找一個解決方案,無論如何,先製作一個viewHolder類;

public class ViewHolder{ 
     //put all of your textviews and image views and 
     //all views here like this 
     TextView contactName; 
     ImageView checkImage; 
     ImageView contactImage; 
} 

然後編輯您的適配器:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    final View contactRowView = convertView; 
    ViewHolder holder; 
    if (contactRowView == null) { 
     final LayoutInflater inflater = (LayoutInflater) 
       this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE 
       ); 

     contactRowView = 
       inflater.inflate(R.layout.contact_row, parent, 
         false); 
     holder = new ViewHolder(): 
     holder.contactPic = (ImageView) 
       contactRowView.findViewById(R.id.contactPic); 
     holder.contactName = (TextView) 
       contactRowView.findViewById(R.id.contactName); 
     holder.checkImage = (ImageView) 
       contactRowView.findViewById(R.id.checkImage); 
     contactRowView.setTag(holder); 
    } else { 
     holder = contactRowView.getTag(); 

     // the list is the same as above and therefore  contains the exact same entries 
     if (this.checkedContactsList.isChecked(position)) 

      holder.checkImage.setImageDrawable(this.context.get. 
        Resources().getDrawable(R.drawable.checked_sign)); 


     holder.contactPic.setImageBitmap(cropePictureInCircle(this.contacts.get(position).getPicture())); 

     holder.contactName.setText(this.contacts.get(position).getName()); 
     return contactRowView; 
    } 
} 

希望這有助於和遺憾,因爲從我的電話寫代碼是完全尷尬。