2012-10-18 51 views
0

我有一個佈局,如下所示。它被代碼誇大了,並被添加到了一個Horizo​​ntalScrollView,有時幾百次,並導致內存問題。我的佈局可以提高效率嗎?

我想知道是否有任何可以做到使其更有效?最初我使用了LinearLayouts,用RelativeLayout替換它對滾動有很大的影響。現在我想知道它是否可以進一步改進?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="156dp" 
    android:layout_height="254dp" 
    android:paddingLeft="7dip" 
    android:paddingRight="7dip"> 

    <FrameLayout android:id="@+id/button_frame" 
    android:layout_width="156dp" 
    android:layout_height="218dp"> 

    <ImageView android:id="@+id/button_bg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/image_bg" 
     /> 

     <ImageView android:id="@+id/button_image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom|right" 
     /> 

     <TextView android:id="@+id/button_select" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="2dip" 
     android:layout_marginRight="2dip" 
     android:background="@drawable/btn_selector_bg_selected" 
     /> 

     <TextView android:id="@+id/button_selected" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:layout_marginLeft="2dip" 
     android:layout_marginRight="2dip" 
     android:background="@drawable/title_bg_selected"/> 

    </FrameLayout> 

    <TextView 
    android:id="@+id/button_title" 
    android:layout_width="156dp" 
    android:layout_height="36dp" 
    android:textColor="#ffffff" 
    android:singleLine="true" 
    android:textSize="13sp" 
    android:textStyle="bold" 
    android:gravity="center_vertical|left" 
    android:ellipsize="end" 
    android:paddingLeft="30dip" 
    android:paddingRight="5dip" 
    android:layout_below="@id/button_frame" 
    android:background="@drawable/title_bg"/> 

</RelativeLayout> 

的ImageView的button_image使用AQuery圖像緩存填充,所以我不知道是否有更多我可以做,以提高圖像的處理方式。但任何有關改進的提示都非常感謝。

+0

您是否嘗試過使用[merge](http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html)? – toadzky

+0

有趣,我不知道合併!除了下面的建議佈局更改之外,我一定會嘗試一下。 – androidneil

回答

1

一般來說,優化Android中的佈局的一個好方法是儘量減少彼此的容器嵌套量。嵌套佈局容器越深入,框架必須對視圖層次結構的度量,佈局和處理事件做的工作越多。

但我想你可能會問錯誤的問題。

HorizontalScrollViewVerticalScollView不適用於您要使用的用途。它們意味着保持大多數靜態佈局,並根據需要根據正在運行的屏幕大小進行滾動。

你想要一個大多數相同的視圖的重複列表,用戶可以滾動。正確的Android視圖容器使用的是ListView,或AdapterView的其他後代之一。

ListView只是小心地創建/膨脹必要的子視圖來填充屏幕上的空間,並在用戶滾動時重用它們。這解決了您遇到的內存問題。它通過要求您將它與Adapter配對 - 一個包裝顯示的實際數據的對象,並根據需要創建給定數據項的正確視圖。

由於您正在嘗試進行水平滾動,因此您還可以查看Gallery(現已在Android中棄用)或較新的ViewPager類,這兩個類都支持通過大量數據的水平移動。

1

如果您可以使用ListView而不是Horizo​​ntalScrollView,則可以創建一個使用viewHolder模式的數組適配器,它基本上允許您在列表視圖中重複使用每個項目的視圖。看看這個更多細節:http://www.jmanzano.es/blog/?p=166

+0

謝謝,但我可以水平使用它嗎? – androidneil

+0

這裏有一個關於如何水平實現ListView的源碼(如果答案上的鏈接仍然有效):http://stackoverflow.com/questions/3240331/horizo​​ntal-listview-in-android –

+0

這裏是github回購,當我試圖谷歌它:https://github.com/vieux/Android-Horizo​​ntal-ListView –