2012-07-12 48 views
2

我目前使用圖庫來顯示項目列表。一切工作正常,除了我不喜歡的中心鎖定功能。我從這裏找到了這個Horizo​​ntalListView的實現,並且想用它來代替Android Gallery。我下載了Horizo​​ntalListView.java,將它放到我的項目中,並將Gallery更改爲Horizo​​ntalListView,但是當我運行該應用程序時,什麼也沒有顯示出來。畫廊的項目(Horizo​​ntalListView現在)通過使用ArrayAdapter進行設置。我有沒有設錯?下面的代碼:Android如何使用這個Horizo​​ntalListView來替換Gallery?

佈局Horizo​​ntalListView:

<com.myapp.HorizontalListView 
     android:id="@+id/related" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#ebebeb" > 
    </com.myapp.HorizontalListView> 

Horizo​​ntalListView項目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#fff" 
android:orientation="vertical" > 


<ImageView 
    android:id="@+id/gameicon" 
    android:layout_width="120dip" 
    android:layout_height="120dip" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:src="@drawable/iconbig" /> 

<TextView 
    android:id="@+id/gamename" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:gravity="center_horizontal|center_vertical" 
    android:text="Title" 
    android:textSize="15sp" 
    android:textColor="#000000" 
    android:textStyle="bold"/> 
</LinearLayout> 

適配器項目:

public class RelatedListAdapter extends ArrayAdapter<GameInfo> { 

private ArrayList<GameInfo> items; 
public static HashMap<String, String> relatedImageMap; 

public RelatedListAdapter(Context context, int textViewResourceId, 
     ArrayList<GameInfo> items) { 
    super(context, textViewResourceId, items); 
    this.items = items; 
    if (relatedImageMap == null) 
     relatedImageMap = new HashMap<String, String>(); 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater mInflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = mInflater.inflate(R.layout.related_items, null); 
    } 

    GameInfo game = items.get(position); 
    if (game != null) { 
     TextView name = (TextView) v.findViewById(R.id.gamename); 
     ImageView icon = (ImageView) v.findViewById(R.id.gameicon); 
     if (name != null) {   
      name.setBackgroundColor(Color.parseColor("#ebebeb")); 
      name.setText(NewDetailActivity.TruncateString(game.getName(), 8)); 
     } 
     if (icon != null) { 
      icon.setBackgroundColor(Color.parseColor("#ebebeb")); 
      if (EfficientAdapter.imageMap.containsKey(game.getId())) { 
       String filePath = EfficientAdapter.imageMap.get(game 
         .getId()); 
       Bitmap bm = BitmapFactory.decodeFile(filePath); 
       icon.setImageBitmap(bm); 
      } else if (relatedImageMap.containsKey(game.getId())) { 
       String filePath = relatedImageMap.get(game.getId()); 
       Bitmap bm = BitmapFactory.decodeFile(filePath); 
       icon.setImageBitmap(bm); 
      } else { 
       String storagePath = Environment 
         .getExternalStorageDirectory().toString() 
         + getContext().getResources().getString(
           R.string.imgCacheFolder); 
       String image = game.getImgUrl().replaceAll(
         "[^a-zA-Z 0-9]+", ""); 
       String filePath = storagePath + image; 
       EfficientAdapter.LoadImageFromWebOperations(game.getImgUrl(), 
         filePath, null); 
       if (relatedImageMap != null) { 
        synchronized (relatedImageMap) { 
         relatedImageMap.put(game.getId(), filePath); 
        } 
       } 
       Bitmap bm = BitmapFactory.decodeFile(filePath); 
       icon.setImageBitmap(bm); 
      } 
     } 
    } 

    return v; 
} 

}

而且在我的活動:

HorizontalListView related = (HorizontalListView) findViewById(R.id.related); 
data = new ArrayList<GameInfo>(); 
adap = new RelatedListAdapter(this, R.layout.related_items, data); 
related.setAdapter(adap); 

如果我將Horizo​​ntalListView更改爲圖庫,一切工作正常,並顯示列表。但是使用Horizo​​ntalListView,沒有任何東西顯示出來。任何人都可以幫我解決這個問題嗎?

編輯:現在我發現了一些不同的東西。 Horizo​​ntalListView確實出現了,但不是第一次打開它。例如,我在標籤3中有3個標籤和Horizo​​ntalListView。當我開始活動時,它顯示標籤1.然後,我單擊標籤3,沒有任何顯示。我更改爲選項卡1或選項卡2,然後返回到選項卡3,列表現在顯示。奇怪吧?有人知道爲什麼嗎?

回答

1

我也在之前使用過Horizo​​ntalListView。但要適應我的特定用例,這太難了。

然後我發現這個類Horizo​​ntalScrollView。它在android SDK中定義 - 更易於使用和多功能。

+0

Horizo​​ntalScrollView的缺點是你不能使用適配器,因此如果你有一個大的項目列表顯示,所有這些都將被加載到內存中,沒有延遲加載。 – 2013-06-29 23:31:49

相關問題