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.example.android.popmovies.MainActivityFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mMovieAdapter = new ImageAdapter(getActivity());
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    GridView gridView = (GridView) rootView.findViewById(R.id.gridview_movie);
    gridView.setAdapter(mMovieAdapter);
    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) {
            Movie movie = mMovies.getMovie(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    movie.toString()); // Send formatted description
            intent.putExtra(Intent.EXTRA_STREAM, movie.getPosterUri()); // Send poster URL w/prefix
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:com.bluetech.gallery5.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 w  w  w  . j  a  v a2 s.c o  m
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help with performance

                //   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() {
            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);
                    mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        }
    });

    return v;
}

From source file:com.sughimura.samplebitmaps.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 w w w  . j av  a  2  s .  c  o m*/
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help with performance
                if (!Utils.hasHoneycomb()) {
                    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() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        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);
                    }
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

From source file:sample.hawk.com.mybasicappcomponents.data_structure.cache.DisplayingBitmaps.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/* www.  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) {
                // Before Honeycomb pause image loading on scroll to help with performance
                if (!Utils.hasHoneycomb()) {
                    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() {
        @TargetApi(VERSION_CODES.JELLY_BEAN)
        @Override
        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) {
                        SMLog.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

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);//w ww  .j  a  va 2s . com
    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.example.kent_zheng.sdk_displayingbitmaps.displayingbitmaps.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//w  w w.j  av a2 s. c  o m
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help with performance
                if (!Utils.hasHoneycomb()) {
                    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() {
        @TargetApi(VERSION_CODES.JELLY_BEAN)
        @Override
        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);
                    }
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.friedran.appengine.dashboard.gui.DashboardLoadFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.load_fragment, container, false);

    mActivity = (DashboardActivity) getSherlockActivity();

    setSpinnerWithItems(layout, R.array.load_time_options, R.id.load_chart_time_spinner);
    mDisplayedTimeID = 0;//  w  w  w .j  a  v  a 2s  .com

    mDisplayMetrics = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);

    Account account = getArguments().getParcelable(KEY_ACCOUNT);
    String applicationID = getArguments().getString(KEY_APPLICATION_ID);
    mChartGridAdapter = new ChartAdapter(mActivity, AppEngineDashboardAPI.getInstance().getClient(account),
            applicationID);

    GridView chartsGridView = (GridView) layout.findViewById(R.id.load_charts_grid);
    chartsGridView.setAdapter(mChartGridAdapter);

    mChartsMemoryCache = initChartsMemoryCache();

    mTracker = AnalyticsUtils.getTracker(mActivity);

    mPullToRefreshAttacher = mActivity.getPullToRefreshAttacher();
    mPullToRefreshAttacher.setRefreshableView(chartsGridView, this);

    return layout;
}

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);/*from  w w w. j  ava2 s  . com*/
    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:org.bwgz.quotation.fragment.AuthorFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.d(TAG, String.format("onCreateView - savedInstanceState: %s", savedInstanceState));

    View view = inflater.inflate(R.layout.author_fragment, container, false);

    viewHolder = new ViewHolder();
    viewHolder.author_name = (TextView) view.findViewById(R.id.author_name);
    viewHolder.author_image = (NetworkImageView) view.findViewById(R.id.author_image);
    viewHolder.author_description_citation_full = (TextView) view
            .findViewById(R.id.author_description_citation_full);
    viewHolder.author_description_short = (TextView) view.findViewById(R.id.author_description_short);
    viewHolder.author_description_full = (TextView) view.findViewById(R.id.author_description_full);
    viewHolder.author_notable_for = (TextView) view.findViewById(R.id.author_notable_for);
    viewHolder.author_description_layout = (LinearLayout) view.findViewById(R.id.author_description_layout);
    viewHolder.author_description_layout_short = (RelativeLayout) view
            .findViewById(R.id.author_description_layout_short);
    viewHolder.author_description_layout_full = (RelativeLayout) view
            .findViewById(R.id.author_description_layout_full);
    viewHolder.quotation_grid = (GridView) view.findViewById(R.id.quotations);

    viewHolder.author_description_citation_full.setMovementMethod(LinkMovementMethod.getInstance());

    viewHolder.author_description_layout.setOnClickListener(new ViewToggleOnClickListener(
            viewHolder.author_description_layout_short, viewHolder.author_description_layout_full));

    GridView gridView = viewHolder.quotation_grid;
    gridView.setOnItemClickListener(new GridViewOnItemClickListener(gridView));
    gridView.setAdapter(new LoadingAdapter());

    Bundle bundle = new Bundle();
    bundle.putParcelable(LOADER_BUNDLE_URI, Person.withAppendedId(getPick().getId()));
    bundle.putStringArray(LOADER_BUNDLE_PROJECTION,
            new String[] { Person.NAME, Person.DESCRIPTION, Person.NOTABLE_FOR, Person.IMAGE_ID,
                    Person.CITATION_PROVIDER, Person.CITATION_STATEMENT, Person.CITATION_URI,
                    BookmarkPerson.BOOKMARK_ID });
    authorLoaderId = initLoader(this, bundle);
    quotationLoaderId = initLoader(this, PersonQuotation.withAppendedId(getPick().getId()),
            QuotationQuery.PROJECTION, null, null, null);

    return view;//from  ww w . j  a v a  2 s . co m
}

From source file:edgargtzg.popularmovies.DiscoverMoviesFragment.java

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

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

    // Get a reference to the GridView, and attach the adapter to it.
    GridView gridView = (GridView) rootView.findViewById(R.id.movies_discovery_gridview);
    gridView.setAdapter(mMoviePosterAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/*from w w w  .ja  va 2  s. co  m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            MovieItem movieItem = mMoviePosterAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), MovieDetailsActivity.class)
                    .putExtra(MovieItem.class.getCanonicalName(), movieItem);
            startActivity(intent);
        }
    });

    // Registers the preference listener to populate movies.
    PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);

    return rootView;
}