2

我目前正在使用图库来显示项目列表。除了我不喜欢的中心锁定功能外,一切都很好。我从这里http://www.dev-smart.com/archives/34找到了这个 Horizo​​ntalListView 实现,并希望用它来替换 Android Gallery。我下载了Horizo​​ntalListView.java,将其放入我的项目并将Gallery更改为Horizo​​ntalListView,但是当我运行该应用程序时,什么也没有出现。Gallery 的项目(现在是Horizo​​ntalListView)是使用ArrayAdapter 设置的。我是不是设置错了什么?这是代码:

布局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 更改为 Gallery,一切正常并显示列表。但是使用 Horizo​​ntalListView,什么也没有出现。谁能帮我解决这个问题?

编辑:我现在发现了一些不同的东西。Horizo​​ntalListView 确实出现了,但不是我第一次打开它。例如,我在选项卡 3 中有 3 个选项卡和 Horizo​​ntalListView。当我开始活动时,它显示选项卡 1。然后我单击选项卡 3,什么都没有显示。我更改为选项卡 1 或选项卡 2,然后返回选项卡 3,现在显示列表。奇怪吧?有人知道为什么吗?

4

1 回答 1

1

之前我也用过Horizo​​ntalListView。但是很难适应我的特定用例。

然后我找到了这个类Horizo​​ntalScrollView。它在 android SDK 中定义 - 更易于使用和通用。

于 2012-07-12T04:05:35.737 回答