2014-08-27 69 views
4

我很努力調試我的Android應用程序中的列表視圖奇怪的UI故障。 99%的時間一切看起來和像它應該的工作,但時不時地我的列表視圖奇怪。當你重新啓動應用程序時,listview再次看起來很正常。自定義列表視圖奇怪的UI故障

有誰知道這是不是一個已知的android bug?

事實上,它只發生在隨機(我試圖找出一個模式和不能)我嚇壞了。我還沒有找到任何關於類似問題的在線信息。希望我剛剛搜索錯誤的搜索條件。

任何意見/幫助將不勝感激。

在此先感謝。

什麼ListView控件通常是這樣的:

What the listview looks like most of the time

ListView的樣子時不時:

What happens every now and then

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/tile_height_padded" 
      android:descendantFocusability="blocksDescendants" 
      android:layout_margin="0dp" 
      android:padding="@dimen/padding_list"> 

<!--This is the clickable background of the item--> 
<ImageView 
     android:id="@+id/imageview_poi_tile_detail_button_detail" 
     android:background="@color/ca" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="0dp" 
     android:layout_toLeftOf="@+id/imageview_poi_tile_map_button"/> 

<!--This is the Grey vertical line next to the icon on the left--> 
<ImageView 
    android:id="@+id/delimiter_poi_tile" 
    android:layout_width="@dimen/delimiter_size" 
    android:layout_height="@dimen/tile_description_height" 
    android:layout_marginTop="@dimen/margin_list_vertical" 
    android:layout_marginBottom="@dimen/margin_list_vertical" 
    android:background="@color/git" 
    android:layout_toRightOf="@id/imageview_poi_tile_icon"/> 

<!--This is the red map button on the right--> 
<ImageView 
    android:id="@id/imageview_poi_tile_map_button" 
    android:background="@color/lr" 
    android:src="@drawable/map" 
    android:scaleType="fitCenter" 
    android:padding="@dimen/image_button_padding" 
    android:layout_width="@dimen/button_size" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true"/> 

<!--This is the marker Icon on the left--> 
<ImageView 
     android:id="@+id/imageview_poi_tile_icon" 
     android:src="@drawable/poidefaultgit" 
     android:scaleType="fitStart" 
     android:padding="@dimen/image_button_padding" 
     android:background="@color/ca" 
     android:layout_width="@dimen/button_size" 
     android:layout_height="@dimen/tile_description_height" 
     android:layout_margin="0dp"/> 

<!--This is the bold title text, eg. BARONS--> 
<TextView 
     android:id="@+id/textview_poi_tile_type" 
     android:background="@color/ca" 
     android:paddingLeft="@dimen/padding_list" 
     android:layout_margin="0dp" 
     android:gravity="left|top" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textStyle="bold" 
     android:text="Poi Type" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:textColor="@color/git" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/tile_title_height" 
     android:layout_toRightOf="@id/delimiter_poi_tile" 
     android:layout_toLeftOf="@+id/textview_poi_tile_distance"/> 

<!--This is the address that is shown, eg 3 ADDERLEY ST, CAPE TOWN,--> 
<TextView 
     android:id="@+id/textview_poi_tile_description" 
     android:background="@color/ca" 
     android:paddingLeft="@dimen/padding_list" 
     android:layout_margin="0dp" 
     android:gravity="left|top" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Address" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:textColor="@color/git" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/textview_poi_tile_type" 
     android:layout_toRightOf="@id/delimiter_poi_tile" 
     android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/> 

<!--This will display a string when the gps is on, not shown in image as gps was off in screenshot--> 
<TextView 
     android:id="@id/textview_poi_tile_distance" 
     android:background="@color/ca" 
     android:textColor="@color/lr" 
     android:text="" 
     android:paddingRight="@dimen/padding_list" 
     android:layout_margin="0dp" 
     android:gravity="left|top" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/> 

@Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView description; 
     TextView type; 
     TextView distance; 
     ImageView imageView; 
     ImageView mapIcon; 
     ImageView clickableArea; 

     if(convertView == null) 
     { 
      convertView = LayoutInflater.from(context).inflate(R.layout.listitem_poi, parent, false); 

      description = (TextView) convertView.findViewById(R.id.textview_poi_tile_description); 
      type = (TextView) convertView.findViewById(R.id.textview_poi_tile_type); 
      distance = (TextView) convertView.findViewById(R.id.textview_poi_tile_distance); 
      imageView = (ImageView) convertView.findViewById(R.id.imageview_poi_tile_icon); 
      mapIcon = (ImageView) convertView.findViewById(R.id.imageview_poi_tile_map_button); 
      clickableArea = (ImageView) convertView.findViewById(R.id.imageview_poi_tile_detail_button_detail); 

      convertView.setTag(new ViewHolder(description, type, distance, imageView, mapIcon, clickableArea)); 
     } 
     else 
     { 
      ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 

      description = viewHolder.description; 
      type = viewHolder.type; 
      distance = viewHolder.distance; 
      imageView = viewHolder.imageView; 
      mapIcon = viewHolder.mapIcon; 
      clickableArea = viewHolder.clickableArea; 
     } 

     final int finalIndex = position; 
     final PointOfInterest poi = getItem(position); 

     description.setText(poi.getDescription()); 
     type.setText(poi.getName()); 

     imageView.setImageResource(R.drawable.poidefaultgit); 

     distance.setText(poi.getDistance()); 

     return convertView; 
    } 
+0

在xml中使用wrap_content – 2014-08-27 13:32:10

+2

您可以評論每個視圖的XML是什麼?很難看到每張照片在視覺上代表什麼。例如,您有3個TextView,但只能在圖片中看到2個 – 2014-08-27 13:32:58

+2

另外,您應該使用MATCH_PARENT,除非您需要支持API <8 – 2014-08-27 13:35:20

回答

0

感謝您的幫助。我設法清理了很多佈局文件。

UI毛刺發生的原因是由於加載了錯誤的樣式文件。它只發生在我使用該風格之前加載另一個元素時。爲了解決這個問題,我創建了一個專門用於列表視圖的新樣式,並將其分配給listview元素。

0

嘗試每次膨脹新視圖而不是重新使用它們。我知道這不是最好的事情,但在某些時候,我有類似的問題,這是唯一的工作。

2

我認爲有幾件事情正在造成這種情況。從經驗來看,我注意到RelativeLayout有時不會根據可用空間適當調整它的大小。如果一個孩子的某個方面在某個地方沒有適當的界限,它會更快地讓一些東西被剪掉。另外,我認爲TextView的一些尺寸不足以顯示文本。我在這裏做了下面的事情,所以它可能需要一些調整,但它應該畫布局的圖片來嘗試。不幸的是,它不像您當前的XML那麼平坦,但它應該有希望解決您的問題。對不起,目前不在電腦上測試。

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <!--This is the marker Icon on the left--> 
    <!--Use margin if you need more space to divider--> 
    <ImageView 
     android:id="@+id/imageview_poi_tile_icon" 
     android:src="@drawable/poidefaultgit" 
     android:scaleType="fitStart" 
     android:padding="@dimen/image_button_padding" 
     android:background="@color/ca" 
     android:layout_width="@dimen/button_size" 
     android:layout_height="match_parent"/> 

    <!--This is the Grey vertical line next to the icon on the left--> 
    <!--Use margin to determine how close the line is to the top/bottom of item--> 
    <ImageView 
     android:id="@+id/delimiter_poi_tile" 
     android:layout_width="@dimen/delimiter_size" 
     android:layout_height="match_parent" 
     android:layout_marginTop="@dimen/margin_list_vertical" 
     android:layout_marginBottom="@dimen/margin_list_vertical" 
     android:src="@color/git" 
     android:background="@color/ca"/> 

    <RelativeLayout 
     android:gravity="center_vertical" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="@color/ca"> 

     <!--This is the bold title text, eg. BARONS--> 
     <TextView 
      android:id="@+id/textview_poi_tile_type" 
      android:paddingLeft="@dimen/padding_list" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textStyle="bold" 
      android:text="Poi Type" 
      android:ellipsize="end" 
      android:maxLines="1" 
      android:textColor="@color/git" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <!--This is the address that is shown, eg 3 ADDERLEY ST, CAPE TOWN,--> 
     <TextView 
      android:id="@+id/textview_poi_tile_description" 
      android:paddingLeft="@dimen/padding_list" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Address" 
      android:ellipsize="end" 
      android:maxLines="1" 
      android:textColor="@color/git" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/textview_poi_tile_type"/> 

     <!--This will display a string when the gps is on, not shown in image as gps was off in screenshot--> 
     <TextView 
      android:id="@id/textview_poi_tile_distance" 
      android:textColor="@color/lr" 
      android:text="" 
      android:paddingRight="@dimen/padding_list" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/textview_poi_tile_type"/> 
    </RelativeLayout> 

    <!--This is the red map button on the right--> 
    <ImageButton 
     android:id="@id/imageview_poi_tile_map_button" 
     android:background="@color/lr" 
     android:src="@drawable/map" 
     android:scaleType="fitCenter" 
     android:padding="@dimen/image_button_padding" 
     android:layout_width="@dimen/button_size" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

這裏的想法是有一個LinearLayout換到的內容的大小整個項目。依靠它來獲取水平方向的一切。然後只將TextViews包裝在Relativelayout中。只需將TextViews對齊下方和對方。 RelativeLayout有擔心實際上相應地居中。它的重量是確保它延伸以水平填充任何剩餘的空間。

另外,您的地圖按鈕可以是ImageButton。您不需要僅用於查看項目點擊事件的視圖。根佈局本身可以按原樣進行。您可能遇到了此設置的問題。查看this post,它告訴你如何正確地獲取一行,以便在嵌入ImageButton時進行單擊。