Example usage for android.widget GridView setStretchMode

List of usage examples for android.widget GridView setStretchMode

Introduction

In this page you can find the example usage for android.widget GridView setStretchMode.

Prototype

public void setStretchMode(@StretchMode int stretchMode) 

Source Link

Document

Control how items are stretched to fill their space.

Usage

From source file:com.bilibili.socialize.share.utils.selector.BaseSharePlatformSelector.java

protected static GridView createShareGridView(final Context context,
        AdapterView.OnItemClickListener onItemClickListener) {
    GridView grid = new GridView(context);
    ListAdapter adapter = new ArrayAdapter<ShareTarget>(context, 0, shareTargets) {
        // no need scroll
        @Override/*www  .  j  av a  2 s .c  o m*/
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.bili_socialize_shareboard_item, parent, false);
            view.setBackgroundDrawable(null);
            ImageView image = (ImageView) view.findViewById(R.id.bili_socialize_shareboard_image);
            TextView platform = (TextView) view.findViewById(R.id.bili_socialize_shareboard_pltform_name);

            ShareTarget target = getItem(position);
            image.setImageResource(target.iconId);
            platform.setText(target.titleId);
            return view;
        }
    };
    grid.setNumColumns(-1);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.bili_socialize_shareboard_size));
    grid.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
    grid.setSelector(R.drawable.bili_socialize_selector_item_background);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(onItemClickListener);
    return grid;
}

From source file:com.chale22.ico01.iconfragment.IconFragmentGames.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.icon_main, container, false);
    int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
    GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
    gridview.setNumColumns(4);//from  w  ww .j a va 2  s.  c  o m
    gridview.setColumnWidth(iconSize);
    gridview.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
    gridview.setVerticalSpacing(iconSize / 2);
    gridview.setOnItemClickListener(this);
    gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
    gridview.setOnItemClickListener(this);
    CONTENT_URI = Uri.parse("content://" + iconsProvider.class.getCanonicalName());
    return view;

}

From source file:it.gcaliendo.elytheme.iconfragment.IconFragmentGames.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.icon_main, container, false);
    int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
    GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
    gridview.setNumColumns(4);// w  w  w. j  av a 2 s . c  o m
    gridview.setColumnWidth(iconSize);
    gridview.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
    gridview.setVerticalSpacing(iconSize / 2);
    gridview.setOnItemClickListener(this);
    gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
    gridview.setOnItemClickListener(this);
    CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
    return view;

}

From source file:com.lm.im_huanxin.emoji.EmojiPageFragment.java

protected void initWidget(View rootView) {
    mPagerFace = (ViewPager) rootView.findViewById(R.id.frag_pager_face);
    pagePointLayout = (LinearLayout) rootView.findViewById(R.id.frag_point);

    int total = datas.size();
    int pages = total / ITEM_PAGE_COUNT + (total % ITEM_PAGE_COUNT == 0 ? 0 : 1);

    allPageViews = new GridView[pages];
    pointViews = new RadioButton[pages];

    for (int x = 0; x < pages; x++) {
        int start = x * ITEM_PAGE_COUNT;
        int end = (start + ITEM_PAGE_COUNT) > total ? total : (start + ITEM_PAGE_COUNT);
        final List<Emojicon> itemDatas = datas.subList(start, end);
        GridView view = new GridView(aty);
        EmojiAdapter faceAdapter = new EmojiAdapter(view, itemDatas);

        view.setNumColumns(7);/*from   w w w  .j  a v a  2s.  com*/
        view.setHorizontalSpacing(1);
        view.setVerticalSpacing(1);
        view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        view.setCacheColorHint(0);
        view.setPadding(2, 0, 2, 0);
        view.setBackgroundResource(android.R.color.transparent);
        view.setSelector(android.R.color.transparent);
        view.setVerticalScrollBarEnabled(false);
        view.setGravity(Gravity.CENTER);
        view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        view.setAdapter(faceAdapter);

        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    Emojicon emoji = itemDatas.get(position);
                    if (DisplayRules.isDeleteEmojicon(emoji)) {
                        listener.selectedBackSpace(emoji);
                    } else {
                        listener.selectedEmoji(emoji);
                    }
                }
            }
        });
        allPageViews[x] = view;

        RadioButton tip = new RadioButton(aty);
        tip.setBackgroundResource(R.drawable.selector_bg_tip);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(8, 8);
        layoutParams.leftMargin = 10;
        pagePointLayout.addView(tip, layoutParams);
        if (x == 0) {
            tip.setChecked(true);
        }
        pointViews[x] = tip;
    }

    PagerAdapter facePagerAdapter = new FacePagerAdapter(allPageViews);
    mPagerFace.setAdapter(facePagerAdapter);
    mPagerFace.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {
            pointViews[index].setChecked(true);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

From source file:com.app.sample.chatting.data.emoji.EmojiPageFragment.java

@Override
protected void initWidget(View rootView) {
    super.initWidget(rootView);
    mPagerFace = (ViewPager) rootView.findViewById(R.id.frag_pager_face);
    pagePointLayout = (LinearLayout) rootView.findViewById(R.id.frag_point);

    int total = datas.size();
    int pages = total / ITEM_PAGE_COUNT + (total % ITEM_PAGE_COUNT == 0 ? 0 : 1);

    allPageViews = new GridView[pages];
    pointViews = new RadioButton[pages];

    for (int x = 0; x < pages; x++) {
        int start = x * ITEM_PAGE_COUNT;
        int end = (start + ITEM_PAGE_COUNT) > total ? total : (start + ITEM_PAGE_COUNT);
        final List<Emojicon> itemDatas = datas.subList(start, end);
        GridView view = new GridView(aty);
        EmojiAdapter faceAdapter = new EmojiAdapter(view, itemDatas);

        view.setNumColumns(7);/*from   w  ww  .j  a v a  2 s  .  c o m*/
        view.setHorizontalSpacing(1);
        view.setVerticalSpacing(1);
        view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        view.setCacheColorHint(0);
        view.setPadding(2, 0, 2, 0);
        view.setBackgroundResource(android.R.color.transparent);
        view.setSelector(android.R.color.transparent);
        view.setVerticalScrollBarEnabled(false);
        view.setGravity(Gravity.CENTER);
        view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        view.setAdapter(faceAdapter);

        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    Emojicon emoji = itemDatas.get(position);
                    if (DisplayRules.isDeleteEmojicon(emoji)) {
                        listener.selectedBackSpace(emoji);
                    } else {
                        listener.selectedEmoji(emoji);
                    }
                }
            }
        });
        allPageViews[x] = view;

        RadioButton tip = new RadioButton(aty);
        tip.setBackgroundResource(R.drawable.selector_bg_tip);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(8, 8);
        layoutParams.leftMargin = 10;
        pagePointLayout.addView(tip, layoutParams);
        if (x == 0) {
            tip.setChecked(true);
        }
        pointViews[x] = tip;
    }

    PagerAdapter facePagerAdapter = new FacePagerAdapter(allPageViews);
    mPagerFace.setAdapter(facePagerAdapter);
    mPagerFace.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {
            pointViews[index].setChecked(true);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.ui.SpeakersGridFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final GridView gridView = getGridView();
    float sf = getResources().getDisplayMetrics().density;
    final int padding = (int) (5.0 * sf);
    final int spacing = (int) (20.0 * sf);

    gridView.setNumColumns(GridView.AUTO_FIT);
    gridView.setColumnWidth((int) (120 * sf));
    gridView.setStretchMode(GridView.STRETCH_SPACING);
    gridView.setVerticalFadingEdgeEnabled(true);
    gridView.setPadding(padding, padding, padding, padding);
    gridView.setHorizontalSpacing(spacing);
    gridView.setVerticalSpacing(spacing);
}

From source file:com.app.sample.chatting.activity.chat.FacePageFragment.java

@Override
protected void initWidget(View rootView) {
    super.initWidget(rootView);
    mPagerFace = (ViewPager) rootView.findViewById(R.id.frag_pager_face);
    pagePointLayout = (LinearLayout) rootView.findViewById(R.id.frag_point);

    int total = datas.size();
    int pages = total / ITEM_PAGE_COUNT + (total % ITEM_PAGE_COUNT == 0 ? 0 : 1);

    allPageViews = new GridView[pages];
    pointViews = new RadioButton[pages];

    for (int x = 0; x < pages; x++) {
        int start = x * ITEM_PAGE_COUNT;
        int end = (start + ITEM_PAGE_COUNT) > total ? total : (start + ITEM_PAGE_COUNT);
        final List<Faceicon> itemDatas = datas.subList(start, end);
        GridView view = new GridView(aty);
        FaceAdapter faceAdapter = new FaceAdapter(view, itemDatas);
        view.setAdapter(faceAdapter);//from w  ww.  j a  va2s  . c  o  m

        view.setNumColumns(4);
        view.setBackgroundColor(Color.TRANSPARENT);
        view.setHorizontalSpacing(1);
        view.setVerticalSpacing(1);
        view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        view.setCacheColorHint(0);
        view.setVerticalScrollBarEnabled(false);
        view.setPadding(5, 0, 5, 0);
        view.setSelector(new ColorDrawable(Color.TRANSPARENT));
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        view.setGravity(Gravity.CENTER);

        view.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    listener.selectedFace(itemDatas.get(position));
                }
            }
        });
        allPageViews[x] = view;

        RadioButton tip = new RadioButton(aty);
        tip.setBackgroundResource(R.drawable.selector_bg_tip);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(8, 8);
        layoutParams.leftMargin = 10;
        pagePointLayout.addView(tip, layoutParams);
        if (x == 0) {
            tip.setChecked(true);
        }
        pointViews[x] = tip;
    }

    PagerAdapter facePagerAdapter = new FacePagerAdapter(allPageViews);
    mPagerFace.setAdapter(facePagerAdapter);
    mPagerFace.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {
            pointViews[index].setChecked(true);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

From source file:ch.arnab.simplelauncher.GridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 *
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 *///w  ww . j a  v a  2  s  .co  m
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(60, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setHorizontalSpacing(convertDpToPixels(20, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(20, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    return root;
}

From source file:com.imobile.mt8382hotkeydefine.GridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 *
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 *///from   w w  w.jav a  2 s  .c om
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);

    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(60, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setHorizontalSpacing(convertDpToPixels(20, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(20, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------
    /*
            root.setLayoutParams(new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    */
    return root;
}

From source file:com.ckdroid.ilauncher.ShowGridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 * <p/>//from   ww  w. ja  v  a2 s.  co  m
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(80, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setNumColumns(4);
    lv.setHorizontalSpacing(convertDpToPixels(10, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(10, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    return root;
}