Equally distributes a fixed amount of android.view.View Views from an android.widget.Adapter in a android.widget.LinearLayout . - Android User Interface

Android examples for User Interface:Layout

Description

Equally distributes a fixed amount of android.view.View Views from an android.widget.Adapter in a android.widget.LinearLayout .

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;

import android.widget.Adapter;
import android.widget.LinearLayout;

public class Main {
    private static final Object NOT_USED = new Object();

    /**//from ww  w. ja v  a 2  s  . c o m
     * Equally distributes a fixed amount of {@link android.view.View Views} from an {@link android.widget.Adapter}
     * in a {@link android.widget.LinearLayout}. If the {@link android.widget.Adapter} cannot supply enough {@link android.view.View Views}
     * to fill the {@link android.widget.LinearLayout} then transparent empty dummy {@link android.view.View Views} will be
     * used instead. These dummy {@link android.view.View Views} will be automatically replaced if a {@link android.view.View}
     * is available by the next time {@link #distributeViews(android.widget.LinearLayout, android.widget.Adapter, int)} is called.
     *
     * @param layout    The {@link android.widget.LinearLayout} which will be populated
     * @param adapter   The {@link android.widget.Adapter} which supplies the {@link android.view.View Views} to the {@link android.widget.LinearLayout}
     * @param viewCount The amount of {@link android.view.View Views} which will be displayed in the {@link android.widget.LinearLayout}.
     */
    public static void distributeViews(LinearLayout layout,
            Adapter adapter, int viewCount) {
        distributeViews(layout, adapter, viewCount, 0);
    }

    /**
     * Equally distributes a fixed amount of {@link android.view.View Views} from an {@link android.widget.Adapter}
     * in a {@link android.widget.LinearLayout}. If the {@link android.widget.Adapter} cannot supply enough {@link android.view.View Views}
     * to fill the {@link android.widget.LinearLayout} then transparent empty dummy {@link android.view.View Views} will be
     * used instead. These dummy {@link android.view.View Views} will be automatically replaced if a {@link android.view.View}
     * is available by the next time {@link #distributeViews(android.widget.LinearLayout, android.widget.Adapter, int)} is called.
     *
     * @param layout    The {@link android.widget.LinearLayout} which will be populated
     * @param adapter   The {@link android.widget.Adapter} which supplies the {@link android.view.View Views} to the {@link android.widget.LinearLayout}
     * @param viewCount The amount of {@link android.view.View Views} which will be displayed in the {@link android.widget.LinearLayout}.
     * @param spacing   Specifies a margin between each {@link android.view.View} in the {@link android.widget.LinearLayout}
     */
    public static void distributeViews(LinearLayout layout,
            Adapter adapter, int viewCount, int spacing) {
        if (layout != null && adapter != null) {
            int adapterCount = adapter.getCount();
            int layoutCount = layout.getChildCount();
            for (int i = 0; i < viewCount; i++) {
                int itemSpacing = i > 0 ? spacing : 0;

                View convertView = null;
                if (i < layoutCount) {
                    View view = layout.getChildAt(i);
                    if (view != null && view.getTag() != NOT_USED) {
                        convertView = view;
                        convertView.setVisibility(View.VISIBLE);
                    }
                }

                View view;
                if (i < adapterCount) {
                    view = adapter.getView(i, convertView, layout);
                } else if (convertView != null) {
                    view = convertView;
                    view.setVisibility(View.INVISIBLE);
                } else {
                    view = new View(layout.getContext());
                    view.setTag(NOT_USED);
                    view.setVisibility(View.INVISIBLE);
                }

                if (view != null) {
                    if (layout.getOrientation() == LinearLayout.HORIZONTAL) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                0, ViewGroup.LayoutParams.MATCH_PARENT,
                                1.0f);
                        params.setMargins(itemSpacing, 0, 0, 0);
                        view.setLayoutParams(params);
                    } else if (layout.getOrientation() == LinearLayout.VERTICAL) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT, 0,
                                1.0f);
                        params.setMargins(0, itemSpacing, 0, 0);
                        view.setLayoutParams(params);
                    }
                    if (i >= layoutCount) {
                        layout.addView(view);
                    }
                }
            }
        }
    }
}

Related Tutorials