如果我们将项目添加到gridView
自动添加为横向,则如何在Android中从上到下按列从gridView
垂直添加项目。
答案 0 :(得分:1)
GridView,在大多数情况下使用ListAdapter。您不会将项目添加到GridView,而是将它们添加到适配器。
ListAdapters不是为您提供基于网格的布局。所以,这是不可能的。我不认为GridView支持在随机索引中添加项目。至少,文档没有提到这一点。
答案 1 :(得分: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;
}