2014-10-29 75 views
0

我有一個arrayadapter,它顯示從服務器獲取的位圖數組。屏幕上最多可同時顯示四個位圖,並使用通常的「持有者」模式在列表中循環顯示。位圖包含在對象中,其中一個屬性是日期 - 如果位圖對象的日期大於X,我希望能夠在列表中使用單詞「new 「具有50%的透明度。在arrayadapter中堆疊framelayout頂部的視圖

爲此,我在現有的位圖佈局中添加了一個framelayout,並對此執行了一個addView,添加了「new」圖層(它位於一個單獨的佈局文件中),並且在位圖上更改了布爾標誌目的是表明我在某個時候將其標記爲「新」。如果位圖是舊的,我發現它在過去的某個時候被標記爲「新的」,我將該標誌更改爲舊的並刪除新圖層。

問題是,當我循環查看列表並遇到一個應該是「新」的位圖時,它會正確顯示,但是當我循環備份列表並返回時,列表會漸進地自我毒化,以至於所有位圖顯示爲「新」。此外,「新」圖層的背景開始爲50%透明,並且在列表上下一些移動之後,它們全部是黑色的 - 表示它將「新」圖層一遍又一遍地添加到列表中每一個位圖。爲了使這更清楚地說明了一點 - 這裏是有關的代碼:

爲 「新建」 圖層佈局文件(R.layout.new_over_layer):

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/half_transparent" 
android:gravity="top|center_horizontal" 
> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:src="@drawable/new_icon" 
    android:background="@color/transparent" 
    /> 

版式文件每一個位圖

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout 
android:id="@+id/newlistview_framelayout" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white" 
android:foregroundGravity="top|center_horizontal" 
> 

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="180dp" 
android:layout_marginBottom="4dp" 
android:background="@color/white" > 



<ImageView 
    android:id="@+id/some_bitmap" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:layout_marginBottom="4dp" 
    android:contentDescription="@string/some_bitmap_desc" 
    android:scaleType="fitXY" 
    android:src="@color/black" /> 

</RelativeLayout> </FrameLayout> 

相關代碼段用於適配器

public class myCustomAdapter extends ArrayAdapter<BitmapObject> 
{ 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    View currentView = convertView; 

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    View newBitmapLayer = inflater.inflate(R.layout.new_over_layer,parent,false); 

    getCurrentTime(); 

    if (isBitmapNew(bitmapList.get(position))) { 

     if (!bitmapList.get(position).getNew()) { 
      bitmapList.get(position).setNew(true); 
      ((FrameLayout)  currentView.findViewById(R.id.newlistiview_framelayout)).addView(newBitmapLayer); 
     } 
    } else { 

     if (bitmapList.get(position).getNew()) { //check if marked new before and reset to old 
      bitmapList.get(position).setNew(false); 
      ((FrameLayout)  currentView.findViewById(R.id.newlistiview_framelayout)).removeView(newBitmapLayer); 
     } 
    } 

    if (null == currentView) { 

     currentView = inflater.inflate(R.layout.new_listiview_item, parent, false); 

     holder = new ViewHolder(); 
     holder.bitmapName = (TextView) currentView.findViewById(R.id.bitmap_name); 
     currentView.setTag(holder); 

    } else { 
     holder = (ViewHolder) currentView.getTag(); 
    } 

.... 
} 

所以..爲什麼列表中毒本身時,我向上和向下滾動,以及如何更改代碼,以便其正確運行,並且只增加了「新」的一次,每個需要的位圖被標記爲新的?

回答

1

通過徹底改變這樣做的方式解決..而不是嘗試添加和刪除列表中的圖層,我創建了透明的「新」層作爲framelayout中的永久子圖層。然後,無論何時位圖是新的,我在「新」圖層上使用了bringchildtofront,並且每當位圖不是新的,我在位圖圖層上使用了bringchildtofront(它具有100%的不透明度並因此隱藏了「新」圖層)。

 if (isBitmapNew(bitmapList.get(position))) { 

     ((FrameLayout) currentView.findViewById(R.id.newlistiview_framelayout)).bringChildToFront(currentView.findViewById(R.id.bitmap_new)); 

    } else { 

     ((FrameLayout) currentView.findViewById(R.id.newlistiview_framelayout)).bringChildToFront(currentView.findViewById(R.id.bitmap_old)); 
    } 

因爲我從不添加或刪除圖層,列表工作得更快,不會中毒。