在GridView的最后一行复制背景

时间:2011-10-24 16:48:32

标签: android android-layout android-gridview

我有一个带有四列的GridView,在每个项目上我添加了我的库背景(一个架子样式),但问题是,在最后一行如果少于四个itens,背景只会到最后一项,我需要它来填补这一行,我该怎么做?

这是我的实际代码:

public class LibraryGridView extends GridView {
    private LibraryActivity activity;

    public LibraryGridView(LibraryActivity activity) {
        super(activity);
        setBackgroundColor(Color.rgb(223, 223, 223));

        this.activity = activity;

        setNumColumns(activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 6 : 4);
    }

    public void setData(List<LibraryItem> books) {
        setAdapter(new BookAdapter(getContext(), books));
    }

    private class BookAdapter extends ArrayAdapter<LibraryItem> {
        private List<LibraryItem> books;

        public BookAdapter(Context context, List<LibraryItem> books) {
            super(context, 0, books);
            this.books = books;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            BookGridView view = null;

            if (convertView == null)
                view = new BookGridView(activity);
            else
                view = (BookGridView) convertView;

            view.setBackgroundResource(R.drawable.background_grid);
            view.loadStateFrom(getItem(position));

            return view;
        }

        public LibraryItem getItem(int id) {
            return books.get(id);
        }

        public int getCount() {
            return books.size();
        }

        public long getItemId(int position) {
            return position;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您必须add empty item at the last of private List<LibraryItem> books in multiple of 4然后将其传递给数组适配器,因此您列出包含(n%4 = 0)项。

这里使用4,因为你的gridview有4列。

感谢。