Example usage for android.widget GridLayout START

List of usage examples for android.widget GridLayout START

Introduction

In this page you can find the example usage for android.widget GridLayout START.

Prototype

Alignment START

To view the source code for android.widget GridLayout START.

Click Source Link

Document

Indicates that a view should be aligned with the start edges of the other views in its cell group.

Usage

From source file:com.phonemetra.turbo.launcher.AsyncTaskCallback.java

public void syncWidgetPageItems(final int page, final boolean immediate) {
    int numItemsPerPage = mWidgetCountX * mWidgetCountY;

    // Calculate the dimensions of each cell we are giving to each widget
    final ArrayList<Object> items = new ArrayList<Object>();
    int contentWidth = mContentWidth;
    final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
            - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
    int contentHeight = mContentHeight;
    final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
            - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);

    // Prepare the set of widgets to load previews for in the background
    int offset = page * numItemsPerPage;
    for (int i = offset; i < Math.min(offset + numItemsPerPage, mFilteredWidgets.size()); ++i) {
        items.add(mFilteredWidgets.get(i));
    }/*from  w  w  w .  j ava  2  s  . co m*/

    // Prepopulate the pages with the other widget info, and fill in the previews later
    final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
    layout.setColumnCount(layout.getCellCountX());
    for (int i = 0; i < items.size(); ++i) {
        Object rawInfo = items.get(i);
        PendingAddItemInfo createItemInfo = null;
        PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(R.layout.apps_customize_widget,
                layout, false);
        if (rawInfo instanceof AppWidgetProviderInfo) {
            // Fill in the widget information
            AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
            createItemInfo = new PendingAddWidgetInfo(info, null, null);

            // Determine the widget spans and min resize spans.
            int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
            createItemInfo.spanX = spanXY[0];
            createItemInfo.spanY = spanXY[1];
            int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
            createItemInfo.minSpanX = minSpanXY[0];
            createItemInfo.minSpanY = minSpanXY[1];

            widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
            widget.setTag(createItemInfo);
            widget.setShortPressListener(this);
        } else if (rawInfo instanceof ResolveInfo) {
            // Fill in the shortcuts information
            ResolveInfo info = (ResolveInfo) rawInfo;
            createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
            createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
            createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
                    info.activityInfo.name);
            widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
            widget.setTag(createItemInfo);
        }
        widget.setOnClickListener(this);
        widget.setOnLongClickListener(this);
        widget.setOnTouchListener(this);
        widget.setOnKeyListener(this);

        // Layout each widget
        int ix = i % mWidgetCountX;
        int iy = i / mWidgetCountX;
        GridLayout.LayoutParams lp = new GridLayout.LayoutParams(GridLayout.spec(iy, GridLayout.START),
                GridLayout.spec(ix, GridLayout.TOP));
        lp.width = cellWidth;
        lp.height = cellHeight;
        lp.setGravity(Gravity.TOP | Gravity.START);
        if (ix > 0)
            lp.leftMargin = mWidgetWidthGap;
        if (iy > 0)
            lp.topMargin = mWidgetHeightGap;
        layout.addView(widget, lp);
    }

    // wait until a call on onLayout to start loading, because
    // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
    // TODO: can we do a measure/layout immediately?
    layout.setOnLayoutListener(new Runnable() {
        public void run() {
            // Load the widget previews
            int maxPreviewWidth = cellWidth;
            int maxPreviewHeight = cellHeight;
            if (layout.getChildCount() > 0) {
                PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
                int[] maxSize = w.getPreviewSize();
                maxPreviewWidth = maxSize[0];
                maxPreviewHeight = maxSize[1];
            }

            getWidgetPreviewLoader().setPreviewSize(maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
            if (immediate) {
                AsyncTaskPageData data = new AsyncTaskPageData(page, items, maxPreviewWidth, maxPreviewHeight,
                        null, null, getWidgetPreviewLoader());
                loadWidgetPreviewsInBackground(null, data);
                onSyncWidgetPageItems(data, immediate);
            } else {
                if (mInTransition) {
                    mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
                } else {
                    prepareLoadWidgetPreviewsTask(page, items, maxPreviewWidth, maxPreviewHeight,
                            mWidgetCountX);
                }
            }
            layout.setOnLayoutListener(null);
        }
    });
}

From source file:com.zyk.launcher.AsyncTaskCallback.java

/**
 * Widget// w w w.  j  a v  a2s.  c  o  m
 * @param page
 * @param immediate
 */
public void syncWidgetPageItems(final int page, final boolean immediate) {
    int numItemsPerPage = mWidgetCountX * mWidgetCountY;

    final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);

    // Calculate the dimensions of each cell we are giving to each widget
    final ArrayList<Object> items = new ArrayList<Object>();
    int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
    final int cellWidth = contentWidth / mWidgetCountX;
    int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();

    final int cellHeight = contentHeight / mWidgetCountY;

    // Prepare the set of widgets to load previews for in the background
    int offset = page * numItemsPerPage;
    for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
        items.add(mWidgets.get(i));
    }

    // Prepopulate the pages with the other widget info, and fill in the previews later
    layout.setColumnCount(layout.getCellCountX());
    for (int i = 0; i < items.size(); ++i) {
        Object rawInfo = items.get(i);
        PendingAddItemInfo createItemInfo = null;
        PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(R.layout.apps_customize_widget,
                layout, false);
        if (rawInfo instanceof AppWidgetProviderInfo) {
            // Fill in the widget information
            AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
            createItemInfo = new PendingAddWidgetInfo(info, null, null);

            // Determine the widget spans and min resize spans.
            int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
            createItemInfo.spanX = spanXY[0];
            createItemInfo.spanY = spanXY[1];
            int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
            createItemInfo.minSpanX = minSpanXY[0];
            createItemInfo.minSpanY = minSpanXY[1];

            widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
            widget.setTag(createItemInfo);
            widget.setShortPressListener(this);
        } else if (rawInfo instanceof ResolveInfo) {
            // Fill in the shortcuts information
            ResolveInfo info = (ResolveInfo) rawInfo;
            createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
            createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
            createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
                    info.activityInfo.name);
            widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
            widget.setTag(createItemInfo);
        }
        widget.setOnClickListener(this);
        widget.setOnLongClickListener(this);
        widget.setOnTouchListener(this);
        widget.setOnKeyListener(this);

        // Layout each widget
        int ix = i % mWidgetCountX;
        int iy = i / mWidgetCountX;

        if (ix > 0) {
            View border = widget.findViewById(R.id.left_border);
            border.setVisibility(View.VISIBLE);
        }
        if (ix < mWidgetCountX - 1) {
            View border = widget.findViewById(R.id.right_border);
            border.setVisibility(View.VISIBLE);
        }

        GridLayout.LayoutParams lp = new GridLayout.LayoutParams(GridLayout.spec(iy, GridLayout.START),
                GridLayout.spec(ix, GridLayout.TOP));
        lp.width = cellWidth;
        lp.height = cellHeight;
        lp.setGravity(Gravity.TOP | Gravity.START);
        layout.addView(widget, lp);
    }

    // wait until a call on onLayout to start loading, because
    // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
    // TODO: can we do a measure/layout immediately?
    layout.setOnLayoutListener(new Runnable() {
        public void run() {
            // Load the widget previews
            int maxPreviewWidth = cellWidth;
            int maxPreviewHeight = cellHeight;
            if (layout.getChildCount() > 0) {
                PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
                int[] maxSize = w.getPreviewSize();
                maxPreviewWidth = maxSize[0];
                maxPreviewHeight = maxSize[1];
            }

            getWidgetPreviewLoader().setPreviewSize(maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
            if (immediate) {
                AsyncTaskPageData data = new AsyncTaskPageData(page, items, maxPreviewWidth, maxPreviewHeight,
                        null, null, getWidgetPreviewLoader());
                loadWidgetPreviewsInBackground(null, data);
                onSyncWidgetPageItems(data, immediate);
            } else {
                if (mInTransition) {
                    mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
                } else {
                    prepareLoadWidgetPreviewsTask(page, items, maxPreviewWidth, maxPreviewHeight,
                            mWidgetCountX);
                }
            }
            layout.setOnLayoutListener(null);
        }
    });
}