我正在尝试在我的Android应用中创建以下屏幕:
除非我可能需要列出最多9个或更多团队。我一直在看GridView,但我在GridView docs或ListAdapter docs中找不到任何东西来证明我可以做到这一点。这是可能的,还是我在错误的地方?
答案 0 :(得分:1)
这可能,还是我在错误的地方?
可以使用GridView
,View
,BaseAdapter
和LayoutInflater
。您可以膨胀单元格的视图(这里的每个单元格都是View
)。例如:
main.xml
:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- add additional properties as needed --!>
</GridView>
player.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/grid_item_team_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/teamName"
android:layout_marginTop="10dp"
android:textSize="20dp" >
<!-- add or modify the properties as needed --!>
</TextView>
</LinearLayout>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] teamValues;
public LayoutInflater inflater;
public LinearLayout ll;
public ImageAdapter(Context context, String[] teamValues) {
this.context = context;
this.teamValues = teamValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from player.xml
gridView = inflater.inflate(R.layout.player, null);
// set value into textview
ll = (LinearLayout)gridView.findViewById(R.id.linear1);
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_team_name);
textView.setText(teamValues[position]);
String team = teamValues[position];
// add team members here
//
//
//
} else {
gridView = (View) convertView;
}
return gridView;
}
}
GridViewActivity.java
public class GridViewActivity extends Activity {
GridView gridView;
ImageAdapter mImageAdapter;
String[] teamName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
teamName = getTeamName();
mImageAdapter = new ImageAdapter(this, teamName);
gridView = (GridView)findViewById(R.id.gridView1);
registerForContextMenu(gridView);
gridView.setAdapter(mImageAdapter);
}
@Override
public void onCreateContextMenu(ContextMenu iMenu, View iView, ContextMenuInfo iMenuInfo) {
super.onCreateContextMenu(iMenu, iView, iMenuInfo);
iMenu.setHeaderTitle("TEAM NAME");
iMenu.clear();
for (int i = 0; i < nTotalTeam; i++) {
iMenu.add(Menu.NONE, i, Menu.NONE, teamName[i]);
}
}
private void getTeamName() {
//
// getTeamName and return the array of string
//
//
}
}
答案 1 :(得分:0)
GridView绝对是正确的。 GridView中的每个视图都是一个View-您想要的任何视图。是的,您可以以编程方式添加新视图。如果您不希望它们变得非常灵活,那么不同的单元格甚至不需要是相同类型的View。