我有一个安卓游戏,可以加载用户对他朋友的“游戏”列表。类似于“与朋友一起玩游戏”
为了重新创建这种类型的视图,我最初使用的是listview,但由于有不同的元素,即游戏,部分(他们的移动 - 你的移动)等,我认为我不能纯粹地做这种布局列表视图?
有没有人做过类似的事情?我可能只是使用滚动视图并动态构建布局,但我担心的是,如果用户有很多游戏,性能将受到影响。
有人可以提供建议吗?
答案 0 :(得分:1)
您应该覆盖listview适配器中的getView()
方法。
它负责显示每个项目的布局。你将能够做到这样的事情:
if(position == YOUR_MOVE_POSITION) {
// here hide ordinary elements, and set the ones you need to be visible
}
在此查看详细信息:link
示例:(我如何看待解决方案)
此方法位于适配器类中(用于填充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;
}