使用自定義按鈕android填充gridview

[英]populating gridview with custom button android


I would like to have a grid view be populated with a custom button I created, the problem is I do not know how this adapter works.

我想在網格視圖中填充我創建的自定義按鈕,問題是我不知道這個適配器是如何工作的。

This is what I am trying to achieve

這就是我想要實現的目標

enter image description here

This is my custom button.java

這是我的自定義button.java

    public class Items extends Button {
        private final static int WIDTH_PADDING = 10;
        private final static int HEIGHT_PADDING = 10;
        private final String label;
        private final String name;
        private final double price;

        public Items(Context context, String name, double price) {
            super(context);
            this.name = name;
            this.price = price;
            this.label = name;

            setHeight(100);
            setWidth(100);
            setFocusable(true);
            setBackgroundColor(Color.parseColor("#eef771"));
            setClickable(true);
        }

        protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
            super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
            if(gainFocus == true) {
                this.setBackgroundColor(Color.parseColor("#ced662"));
            } else {
                this.setBackgroundColor(Color.parseColor("#eef771"));
            }

        }

        protected void onDraw(Canvas canvas) {
            Paint textPaint = new Paint();
            textPaint.setColor(Color.parseColor("#f4426e"));
            canvas.drawText(label + " (P" + price + ")", (WIDTH_PADDING / 2), (HEIG

HT_PADDING / 2,  textPaint);
    }
}

and my adapter still looks like this :

我的適配器仍然是這樣的:

public class ItemsAdapter extends BaseAdapter {
    private Context context;

    public ItemsAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }
}

My question : I would like to be able to add a custom button whenever I want to the adapter, do I use an arrayList here? how do I return that arrayList so my gridView will be populated?

我的問題:我希望每當我想要適配器時都能添加自定義按鈕,我在這里使用arrayList嗎?如何返回該arrayList以便我的gridView將被填充?

I am new to android so I do not know how these things work. Thanks in advance.

我是android的新手,所以我不知道這些東西是如何工作的。提前致謝。

1 个解决方案

#1


0  

I recommend you to first read this Android developer topic. You should use the RecyclerView component for creating this screen because it's more optimised and more improved then regular ListView. After you read this Android developer topic, you can go through this blog post. There is an example of how to do this kind of screen as you presented in your picture. But be sure to get full knowledge of how does this RecyclerView.Adapter works and how is related to RecyclerView.ViewHolder object.

我建議您先閱讀此Android開發人員主題。您應該使用RecyclerView組件來創建此屏幕,因為它比常規ListView更優化和更好。閱讀此Android開發人員主題后,您可以瀏覽此博客文章。有一個例子說明如何在你的照片中顯示這種屏幕。但請務必充分了解此RecyclerView.Adapter的工作原理以及與RecyclerView.ViewHolder對象的關系。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/07/23/72a17e0ec930c0c21fd92d474ec84b66.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com