Example usage for android.widget GridView setAdapter

List of usage examples for android.widget GridView setAdapter

Introduction

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

Prototype

@Override
public void setAdapter(ListAdapter adapter) 

Source Link

Document

Sets the data behind this GridView.

Usage

From source file:com.zetterstrom.android.soundboarder.fragments.SoundboardFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    int i = getArguments().getInt(ARG_BOARD_NUMBER);
    String board = getResources().getStringArray(R.array.boards_array)[i];

    getActivity().setTitle(board);/*w  w w.j a  v  a  2  s.  co m*/
    createSounds(i);

    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist. The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed. Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    GridView gridView = (GridView) inflater.inflate(R.layout.fragment_board, container, false);
    gridView.setAdapter(
            new SoundAdapter(getActivity().getApplicationContext(), R.layout.grid_item, getSounds()));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Sound s = (Sound) getSounds().get(position);

            AssetFileDescriptor afd;
            try {
                afd = getActivity().getAssets().openFd(s.getAssetDescription());
                ((MainActivity) getActivity()).playSound(afd);
            } catch (IOException e) {
                Toast.makeText(getActivity(), "Error Playing Sound Byte", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

        }
    });
    gridView.setOnItemLongClickListener(longClickListener);
    return gridView;
}

From source file:edu.rowan.app.fragments.HomescreenFragment.java

/**
 * Construct the view for this fragment.
 * The core layout of this fragment is a CarouselView and grid of available buttons
 *///from www. j ava  2  s  .c o  m
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // hide actionbar loading indicator TODO: move this elsewhere
    activity.showLoading(false);

    View view = inflater.inflate(R.layout.activity_main, container, false);
    RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.mainLayout);
    layout.setPadding(0, 20, 0, 0);

    CarouselView rowanFeatures = new CarouselView(inflater.getContext());
    // set view Id so that relativeLayout can position. Without an Id relativeLayout doesn't work
    rowanFeatures.setId(2342343);
    layout.addView(rowanFeatures);
    setTouchListener(rowanFeatures);

    GridView grid = new GridView(inflater.getContext());
    grid.setOnItemClickListener(this);
    grid.setNumColumns(3);
    grid.setVerticalSpacing(20);
    grid.setAdapter(new RowanAdapter());
    RelativeLayout.LayoutParams gridParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    gridParams.addRule(RelativeLayout.BELOW, rowanFeatures.getId());
    gridParams.topMargin = 20;
    layout.addView(grid, gridParams);

    return view;
}

From source file:com.example.gael.popularmovies.PosterFragment.java

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

    Log.e(LOG_TAG, "creating view");
    mPosterAdapter = new ImageAdapter(getActivity(), // The current context (this activity)
            R.layout.grid_item_poster, // The name of the layout ID.
            R.id.grid_item_poster_imageview, new ArrayList<Poster>());

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the GridView, and attach this adapter to it.
    GridView gridView = (GridView) rootView.findViewById(R.id.gridview_poster);
    gridView.setAdapter(mPosterAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override/*from  ww w  .j  a v  a  2  s  .c o m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Poster poster = mPosterAdapter.getItem(position);
            Context context = getActivity();
            CharSequence text = poster.getKey();
            Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra("poster", poster);
            startActivity(intent);
        }
    });

    gridView.setOnScrollListener(this);

    return rootView;
}

From source file:com.battlelancer.seriesguide.ui.CheckinActivity.java

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    setContentView(R.layout.checkin);/*from www  .j a va  2  s  .  c o  m*/
    setupNavDrawer();

    setupActionBar();

    // setup search box
    mSearchBox = (EditText) findViewById(R.id.editTextCheckinSearch);
    mSearchBox.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mSearchFilter = !TextUtils.isEmpty(s) ? s.toString() : null;
            getSupportLoaderManager().restartLoader(LOADER_ID, null, CheckinActivity.this);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    // setup clear button
    findViewById(R.id.imageButtonClearSearch).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mSearchBox.setText(null);
            mSearchBox.requestFocus();
        }
    });
    mSearchBox.requestFocus();

    // setup adapter
    mAdapter = new CheckinAdapter(this, null, 0);

    // setup grid view
    GridView list = (GridView) findViewById(android.R.id.list);
    list.setAdapter(mAdapter);
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Cursor show = (Cursor) mAdapter.getItem(position);
            int episodeTvdbId = show.getInt(CheckinQuery.NEXTEPISODE);
            if (episodeTvdbId <= 0) {
                return;
            }

            // display a check-in dialog
            CheckInDialogFragment f = CheckInDialogFragment.newInstance(CheckinActivity.this, episodeTvdbId);
            f.show(getSupportFragmentManager(), "checkin-dialog");
        }
    });
    list.setEmptyView(findViewById(R.id.empty));

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);
}

From source file:org.dmfs.android.colorpicker.PaletteFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    /*//from   w w w . ja v  a2s.  c o  m
       * TODO: build the layout programmatically to get rid of the resources, so we can distribute this in a single jar
     */
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.org_dmfs_colorpickerdialog_palette_grid,
            container, false);
    final GridView gridview = (GridView) rootView.findViewById(android.R.id.content);

    mAdapter = new PaletteGridAdapter(getActivity(), mPalette);
    gridview.setAdapter(mAdapter);
    gridview.setOnItemClickListener(this);
    gridview.setNumColumns(mAdapter.getNumColumns());

    /*
       * Adjust the layout of the gridview to a square.
     *
     * Inspired by Bill Lahti, see http://blahti.wordpress.com/2012/07/23/three-variations-of-image-squares/
     */
    gridview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressLint("NewApi")
        public void onGlobalLayout() {
            int parentHeight = rootView.getHeight() - rootView.getPaddingTop() - rootView.getPaddingBottom();
            int parentWidth = rootView.getWidth() - rootView.getPaddingLeft() - rootView.getPaddingRight();

            int gridWidth = Math.min(parentWidth, parentHeight);

            int columnSpacing;
            if (android.os.Build.VERSION.SDK_INT >= 16) {
                columnSpacing = gridview.getHorizontalSpacing() * (mAdapter.getNumColumns() - 1);
            } else {
                /*
                * TODO: getHorizontalSpacing() has been introduced in SDK level 16. We need to find a way to get get the actual spacing. Until then we use
                * a hard coded value of 8 dip.
                *
                * One way would be to use a dimension in the layout. That would allow us to resolve the dimension here. However, that would be one step
                * away from a library without resource dependencies. Maybe there is an Android dimension resource with a reasonable value?
                */
                DisplayMetrics metrics = inflater.getContext().getResources().getDisplayMetrics();
                if (android.os.Build.VERSION.SDK_INT > 10) {
                    columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics)
                            * (mAdapter.getNumColumns() - 1);
                } else {
                    // Android 2 seems to add spacing around the entire gridview
                    columnSpacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics)
                            * mAdapter.getNumColumns();
                }
            }

            // width of a single column
            int columnWidth = (gridWidth - columnSpacing) / mAdapter.getNumColumns();

            // estimated width of the grid
            int actualGridWidth = mAdapter.getNumColumns() * columnWidth + columnSpacing;

            // add padding to center the grid if we don't use the entire space due to rounding errors
            if (actualGridWidth < gridWidth - 1) {
                int padding = (gridWidth - actualGridWidth) / 2;
                if (padding > 0) {
                    gridview.setPadding(padding, padding, padding, padding);

                }
            } else {
                // no padding needed
                gridview.setPadding(0, 0, 0, 0);
            }

            // set the column width
            gridview.setColumnWidth(columnWidth);

            android.view.ViewGroup.LayoutParams params = gridview.getLayoutParams();
            if (params == null || params.height != gridWidth) // avoid unnecessary updates
            {
                LayoutParams lparams = new LinearLayout.LayoutParams(gridWidth, gridWidth);
                gridview.setLayoutParams(lparams);
            }
        }
    });
    return rootView;
}

From source file:co.touchlab.thumbcache.ui.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override//from   w  w w . j  av a  2  s.  com
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                }
            }
        }
    });

    return v;
}

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);

        view.setNumColumns(4);/*from  www  . java2s  . c  o m*/
        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:com.mbpro.tweebook.facebook.images.FacebookImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.facebook_album_image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*from  w  w w  .  j  a  v  a  2s.c  o m*/
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.android.activity.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable   
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*w w w  . j av a 2 s. c  o m*/
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (LogSettings.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.xuwakao.mixture.ui.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override/*from   ww w .j  av a2s  .  co  m*/
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                mImageFetcher.setPauseWork(true);
            } else {
                mImageFetcher.setPauseWork(false);
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
        }
    });

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final int numColumns = (int) Math
                    .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
            if (numColumns > 0) {
                final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                mAdapter.setItemHeight(columnWidth);
                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                }
            }
        }
    });

    return v;
}