Jamshid
Jamshid

Reputation: 340

How to add item for vertical to gridview

If we add item to gridView automatically added for horizontal, how to add item vertically to gridView from top to bottom by column in Android.

Upvotes: 1

Views: 581

Answers (2)

Jamshid
Jamshid

Reputation: 340

this is getView method of grid view adapter

   @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;
    }

this is list view adapter

/**
 * 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;
    }

Upvotes: 0

Kumar Bibek
Kumar Bibek

Reputation: 9117

GridView, in most cases uses a ListAdapter. You don't add items to a GridView, rather, you add them to the Adapter.

ListAdapters are not designed to give you a grid based layout. And so, it's not possible. I don't think GridView supports adding items at random indexes. At-least, the docs don't mention this.

Upvotes: 1

Related Questions