可以参考http://developer.android.com/guide/tutorials/views/hello-gridview.html
然后只需要修改:
public class MyAdapter extends BaseAdapter {
private Context context;
private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};
public MyAdapter(Context context) {
this.context = context;
}
public int getCount() { return 9;}
public Object getItem(int position) { return null;}
public long getItemId(int position) { return 0;}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(85, 85));
}
else {
tv = (TextView) convertView;
}
tv.setText(texts[position]);
return tv;
}
view集成的方法
public View getView(int position, View contentView, ViewGroup arg2)
{ ViewHolder holder;
if (contentView == null) {
holder = new ViewHolder();
contentView = inflater.inflate(R.layout.my_magic_list,null);
holder.label = (TextView) contentView.findViewById(R.id.label);
contentView.setTag(holder); }
else { holder = (ViewHolder) contentView.getTag(); }
holder.label.setText(getLabel());
return contentView; }
•Use convertView
•If you have images, don't scale your images on the fly. Use Bitmap.createScaledBitmap to create a scaled bitmap and put that into your views
•Use a ViewHolder so you don't have to call a bunch of findViewByIds() every time
•Decrease the complexity of the views in your listview. The fewer subviews, the better. RelativeLayout is much better at this than, say, LinearLayout. And make sure to use if you're implementing custom views.
4.LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.edit_event, null);
ArrayList<String> routes = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);
String dest = events.get(pos).getDestination();
int routesPos = routes.indexOf(dest);
Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos);
destSpinner.setAdapter(aa);
destSpinner.setSelection(routesPos);
最后一句是在spiner没有显示之前显示一个特定信息
5.
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
android.R.layout.simple_gallery_item,
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME},
// The "text1" view defined in the XML template
new int[] { android.R.id.text1 });
5.只想显示一个
display a static list of String objects
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});
add more String objects to the list later
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());
本文介绍如何在 Android 应用中使用 GridView,并通过创建自定义适配器实现动态加载字符串列表。文章展示了如何减少视图复杂度、优化图片加载及使用 ViewHolder 模式提高 ListView 性能。
256

被折叠的 条评论
为什么被折叠?



