0

我有一个 android 游戏,它加载用户对他的朋友的“游戏”列表。类似于“与朋友的游戏单词”

为了重新创建这种类型的视图,我最初使用了一个列表视图,但是因为有不同的元素,即游戏、部分(他们的移动 - 你的移动)等,我认为我不能用纯粹的列表视图来做这种布局?

有没有人做过类似的事情?我可能只是使用滚动视图并动态构建布局,但我担心如果用户有很多游戏,性能会受到影响。

任何人都可以建议吗?

4

1 回答 1

1

您应该覆盖getView()列表视图适配器中的方法。它负责显示每个项目的布局。您将可以在那里执行以下操作:

if(position == YOUR_MOVE_POSITION) {

     // here hide ordinary elements, and set the ones you need to be visible
}

在这里查看详细信息:链接

示例:(我如何看待解决方案)

此方法在您的适配器类中(用于填充ListView,因此您需要使用如下代码覆盖它:

public View getView(int position, View convertView, ViewGroup parent) {

  final View v;

  if(position == 1) {  // here come instructions for 'header' no.1
    v = createViewFromResource(position, convertView, parent, mResource);

    // the widget you want to show in header:
    ImageView yourMoveImage = (ImageView) v.findViewById(R.id.your_move);

    // and here come widgets you don't want to show in headers:
    ImageView otherWidget = (ImageView) v.findViewById(R.id.other_widget);

    // then you set the visibility:
    yourMoveImage.setVisibility(View.VISIBLE);    // here is the key
    otherWidget.setVisibility(View.GONE);       // it may also be View.INVISIBLE (look up the official docs for details)   


  } else {

    if(position == 5){  // here come instructions for 'header' no.1


      v = createViewFromResource(position, convertView, parent, mResource);

      // the widget you want to show in header:
      ImageView theirMoveImage = (ImageView) v.findViewById(R.id.their_move);

      // and here come widgets you don't want to show in headers:
      ImageView otherWidget = (ImageView) v.findViewById(R.id.other_widget); 

      // then you set the visibility:
      yourMoveImage.setVisibility(View.VISIBLE);    // here is the key
      otherWidget.setVisibility(View.GONE);       // it may also be View.INVISIBLE (look up the official docs for details)   

    } else {

      // if it is the regular item, just show it as desribed in your XML:
      v = createViewFromResource(position, convertView, parent, mResource);
    }
  }

  return v;
}
于 2012-09-12T15:29:37.837 回答