我正在尝试在我的 android 应用程序中创建以下屏幕:
除了我可能需要列出多达 9 个或更多的团队。我一直在查看 GridView,但在GridView 文档或ListAdapter 文档中找不到任何东西来表明我可以做到这一点。这是可能的还是我找错地方了?
我正在尝试在我的 android 应用程序中创建以下屏幕:
除了我可能需要列出多达 9 个或更多的团队。我一直在查看 GridView,但在GridView 文档或ListAdapter 文档中找不到任何东西来表明我可以做到这一点。这是可能的还是我找错地方了?
这是可能的还是我找错地方了?
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
//
//
}
}
GridView 就在这里。GridView 中的每个视图都是一个视图——你想要的任何视图。是的,您可以以编程方式向其中添加新视图。如果您不希望它们具有相同的视图类型,那么不同的单元格甚至不需要是相同类型的视图——这一切都非常灵活。