如果我们将项目gridView
添加为自动添加为水平,如何gridView
在Android中从上到下按列垂直添加项目。
问问题
516 次
2 回答
1
GridView,在大多数情况下使用 ListAdapter。您不会将项目添加到 GridView,而是将它们添加到适配器。
ListAdapters 并非旨在为您提供基于网格的布局。所以,这是不可能的。我不认为 GridView 支持在随机索引处添加项目。至少,文档没有提到这一点。
于 2013-04-01T11:53:54.843 回答
0
这是网格视图适配器的 getView 方法
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ListView listView;
if (view == null) {
listView = (ListView) LayoutInflater.from(mContext).inflate(R.layout.product_list_row, null);
listView.setLayoutParams(new GridView.LayoutParams(COLUMN_WIDTH, COLUMN_WIDTH * max_size));
listView.setDividerHeight(0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
if (getItem(i).size() > index) {
Product product = getItem(i).get(index);
if (product != null && !product.getStartPrice().equalsIgnoreCase("")) {
ItemDetailDialog dialog = new ItemDetailDialog(mContext, getItem(i), index);
dialog.show();
}
}
}
});
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
} else {
listView = (ListView) view;
}
ListViewAdapter adapter = new ListViewAdapter(getItem(i));
listView.setAdapter(adapter);
listView.setCacheColorHint(0);
listView.setId(i);
listView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
// v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
return listView;
}
这是列表视图适配器
/**
* list view adapter
*/
private class ListViewAdapter extends BaseAdapter {
// list products
private List<Product> products;
//
ProductRowView productRowView;
//
Product product;
// constructor
private ListViewAdapter(List<Product> products){
this.products = products;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Product getItem(int i) {
if (products.size() <= i )
return null;
return products.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int index, View convertView, ViewGroup parent) {
// any code
return convertView;
}
于 2013-04-26T13:42:53.573 回答