在arrayadapter中堆叠framelayout顶部的视图
我有一个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();
}
....
}
所以..为什么列表中毒本身时,我向上和向下滚动,以及如何更改代码,以便其正确运行,并且只增加了“新”的一次,每个需要的位图被标记为新的?
通过彻底改变这样做的方式解决..而不是尝试添加和删除列表中的图层,我创建了透明的“新”层作为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));
}
因为我从不添加或删除图层,列表工作得更快,不会中毒。