1

我有一个定制的布局,如图所示。图片 激活是通过短信完成的。因此,一旦 SMS 激活到来,“尚未激活文本”消失,绿色箭头与灰色箭头切换。我的布局 xml 文件如下所示。我有两个图像视图"@+id/list_arrow""@+id/green_list_arrow". 在SMSReceiver中,我更新为

lvObjects = (ListView)myView.findViewById(android.R.id.list);       
lvObjects.post(new Runnable() { public void run() { MyListFragment.adapter.notifyDataSetChanged(); } } );

在自定义的 ListView 中getView(),我更新为好像“激活”我使用@+id/list_arrow和“未激活”我使用@+id/green_list_arrow。测试时,“未激活文本”消失,但绿色箭头从未更新。可能缺少什么?谢谢

Edit1:一旦激活,我也想停止setOnClickListener(). 我怎样才能做到这一点?

public View getView(int position, View convertView, ViewGroup parent) { 
        BitmapWorkerTask task = null;
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){

            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.activationStatus = (TextView)vi.findViewById(R.id.activated);
            if(position > maxRowNumber)
                  maxRowNumber = position;

            vi.setTag(viewHolder);            
        }else{
            viewHolder=(ViewHolder)vi.getTag();               
        }
        viewHolder.thumbnailImage.setOnClickListener(new onMyClick(position)); 
        viewHolder.id.setText(listIDs.get(position));

         if(activationState.get(position).equals("Activated")) {             
             viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
             viewHolder.activationStatus.setText(""); 

         }
         else{
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.green_list_arrow);
            viewHolder.activationStatus.setText(activationState.get(position));
                viewHolder.activationStatus.setTextColor(android.graphics.Color.GRAY);
                viewHolder.arrow.setOnClickListener(new onMyArrowClick(position));
         }


         String hasnoimageyet = "no";
         if(position > maxRowNumber && maxRowNumber > 3 ){//it is for sure

             hasnoimageyet = this.listRowhasnoImageyet.get(position); 
         }

             String id = listIDs.get(position);
             task.execute(id);   

         }      
        return vi;
    }

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_background_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip" 
            android:focusable="false"
            android:clickable="true"
            android:onClick="imageViewisClicked"    
            android:contentDescription="@string/image_view_description"
            android:layout_height="50dip"
            android:src="@drawable/icons_preview"
            />

    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15sp"
        android:textStyle="bold"/>

     <!-- Rightend Arrow -->
     <ImageView 
        android:id="@+id/list_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/arrow"
        android:layout_alignParentRight="true"
        android:src="@drawable/arrow"

        android:layout_centerVertical="true"/>

      <ImageView 
        android:id="@+id/green_list_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:contentDescription="@string/arrow"
        android:layout_alignParentRight="true"
        android:src="@drawable/green_arrow"

        android:layout_centerVertical="true"/>

     <TextView
        android:id="@+id/activated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/title"
        android:paddingLeft="20dp"
        android:typeface="sans"
        android:textStyle="italic"
        android:textSize="15sp"/>

</RelativeLayout>
4

1 回答 1

1

无论如何,使用您的布局,您将在 grez 上绘制绿色箭头。(因为它低于 xml 中的另一个)

您可以根据状态切换两者的可见性,或者只为箭头设置一个 ImageView 并更改位图。(下面的代码)可以通过将 OnClickListener 设置为来禁用它null

if(activationState.get(position).equals("Activated")) {             
             viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
             viewHolder.arrow.setImageResource(R.drawable.arrow);
             viewHolder.activationStatus.setText(""); 
             viewHolder.thumbnailImage.setOnClickListener(null); 

} else{
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.green_list);
            viewHolder.arrow.setImageResource(R.drawable.green_arrow);
            viewHolder.activationStatus.setText(activationState.get(position));
            viewHolder.activationStatus.setTextColor(android.graphics.Color.GRAY);
            viewHolder.arrow.setOnClickListener(new onMyArrowClick(position));
            viewHolder.thumbnailImage.setOnClickListener(new onMyClick(position)); 
}

一个更平滑的解决方案是在图像视图上使用StateListDrawable或者甚至更好的颜色过滤器。

于 2013-08-25T05:49:32.087 回答