Example usage for android.widget GridView setOnScrollListener

List of usage examples for android.widget GridView setOnScrollListener

Introduction

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

Prototype

public void setOnScrollListener(OnScrollListener l) 

Source Link

Document

Set the listener that will receive notifications every time the list scrolls.

Usage

From source file:com.cm.android.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);/*from w  w  w  .  j  a  va  2s .c o m*/
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        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);
                    }
                }
            }
        }
    });

    //@author anshu
    final ImageView mAddImage = (ImageView) v.findViewById(R.id.add_image);
    mAddImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                takePhoto();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e("takePhoto", ex.toString());
            }
        }
    });

    //Admob
    mAdView = (AdView) v.findViewById(R.id.adView);

    return v;
}

From source file:com.myinstagram.afinski.myinstgrmviewer.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);//  ww  w  .  j  a  va 2  s .  c o  m
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        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.simplelife.seeds.android.utils.gridview.gridviewui.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.activity_seeds_gridview_gridfragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mPullToRefreshView = (SeedsPullToRefreshView) v.findViewById(R.id.main_pull_refresh_view_grid);
    mGridView.setAdapter(mAdapter);//from www  . j  ava  2  s. c  o  m
    mGridView.setOnItemClickListener(this);
    mPullToRefreshView.setOnHeaderRefreshListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        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() {
            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);
                    }
                }
            }
        }
    });

    // Set a title for this page
    ActionBar tActionBar = getActivity().getActionBar();
    tActionBar.setTitle(R.string.seeds_details_top);

    // Show the details info of the seed
    // Prepare the seeds text info
    String tSeedName = Images.getSeedsEntity().getSeedName();
    String tSeedSize = Images.getSeedsEntity().getSeedSize();
    String tSeedFormat = Images.getSeedsEntity().getSeedFormat();
    String tSeedMosaic = (Images.getSeedsEntity().getSeedMosaic())
            ? getString(R.string.seeds_listperday_withmosaic)
            : getString(R.string.seeds_listperday_withoutmosaic);
    TextView tTextViewName = (TextView) v.findViewById(R.id.seed_grid_info_name);
    TextView tTextViewFormat = (TextView) v.findViewById(R.id.seed_grid_info_format);
    TextView tTextViewSize = (TextView) v.findViewById(R.id.seed_grid_info_size);
    TextView tTextViewMosaic = (TextView) v.findViewById(R.id.seed_grid_info_mosaic);

    if (null != tSeedName)
        tTextViewName.setText(getString(R.string.seedTitle) + ": " + tSeedName);
    if (null != tSeedFormat)
        tTextViewFormat.setText(getString(R.string.seedFormat) + ": " + tSeedFormat);
    if (null != tSeedSize)
        tTextViewSize.setText(getString(R.string.seedSize) + ": " + tSeedSize);
    if (null != tSeedMosaic)
        tTextViewMosaic.setText(getString(R.string.seeds_listperday_mosaic) + ": " + tSeedMosaic);

    return v;
}

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//  www.  j a v  a 2 s. c om
        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.krayzk9s.imgurholo.ui.AlbumsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    boolean newData = false;
    if (savedInstanceState == null && urls == null) {
        urls = new ArrayList<String>();
        ids = new ArrayList<String>();
        newData = true;/*  w  ww  . j  a  va  2 s  .  c o  m*/
    } else if (savedInstanceState != null) {
        urls = savedInstanceState.getStringArrayList("urls");
        ids = savedInstanceState.getStringArrayList("ids");
    }
    View view = inflater.inflate(R.layout.image_layout, container, false);
    errorText = (TextView) view.findViewById(R.id.error);
    mPullToRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.ptr_layout);
    ActionBarPullToRefresh.from(getActivity())
            // Mark All Children as pullable
            .allChildrenArePullable()
            // Set the OnRefreshListener
            .listener(this)
            // Finally commit the setup to our PullToRefreshLayout
            .setup(mPullToRefreshLayout);
    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) errorText.getLayoutParams();
    mlp.setMargins(0, 0, 0, 0);
    view.setPadding(0, getActivity().getActionBar().getHeight(), 0, 0);
    noImageView = (TextView) view.findViewById(R.id.no_images);
    GridView gridview = (GridView) view.findViewById(R.id.grid_layout);
    ImgurHoloActivity activity = (ImgurHoloActivity) getActivity();
    SharedPreferences settings = activity.getApiCall().settings;
    gridview.setColumnWidth(Utils.dpToPx(
            Integer.parseInt(settings.getString(getString(R.string.icon_size), getString(R.string.onetwenty))),
            getActivity()));
    imageAdapter = new ImageAdapter(view.getContext());
    gridview.setAdapter(imageAdapter);
    gridview.setOnItemClickListener(new GridItemClickListener());
    if (newData) {
        getImages();
    }
    gridview.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int i2, int i3) {
            if (lastInView == -1)
                lastInView = firstVisibleItem;
            else if (lastInView > firstVisibleItem) {
                getActivity().getActionBar().show();
                lastInView = firstVisibleItem;
            } else if (lastInView < firstVisibleItem) {
                getActivity().getActionBar().hide();
                lastInView = firstVisibleItem;
            }
        }
    });
    return view;
}

From source file:com.near.chimerarevo.fragments.ProductsListFragment.java

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

    boolean isLandscapeLarge = false;

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if ((getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
            isLandscapeLarge = true;/* w w w  .j  a  v  a  2s .c  o  m*/
    } else
        isLandscapeLarge = false;

    if (!isLandscapeLarge)
        v.setPadding(0, getResources().getDimensionPixelSize(R.dimen.actionbar_height), 0, 0);

    GridView mGrid = (GridView) v.findViewById(R.id.products_grid);
    mCat = (Spinner) v.findViewById(R.id.category_spinner);
    mBrand = (Spinner) v.findViewById(R.id.brand_spinner);
    mLoading = (ProgressWheel) v.findViewById(R.id.product_progress);

    brandsList = new ArrayList<>();
    catList = new ArrayList<>();
    catIdList = new ArrayList<>();

    arrayList = new ArrayList<>();

    mAdapter = new ProductsAdapter(arrayList);
    if (mGrid != null) {
        mGrid.setAdapter(mAdapter);
        mGrid.setOnScrollListener(this);
        mGrid.setOnItemClickListener(this);
    }

    return v;
}

From source file:com.achep.header2actionbar.HeaderFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Activity activity = getActivity();
    assert activity != null;
    mFrameLayout = new FrameLayout(activity);

    mHeader = onCreateHeaderView(inflater, mFrameLayout);
    mHeaderHeader = mHeader.findViewById(android.R.id.title);
    mHeaderBackground = mHeader.findViewById(android.R.id.background);
    assert mHeader.getLayoutParams() != null;
    mHeaderHeight = mHeader.getLayoutParams().height;

    mFakeHeader = new Space(activity);
    mFakeHeader.setLayoutParams(new ListView.LayoutParams(0, mHeaderHeight));

    View content = onCreateContentView(inflater, mFrameLayout);
    if (content instanceof ListView) {
        isListViewEmpty = true;/*from  ww w  .  ja v  a 2 s  .c o m*/

        final ListView listView = (ListView) content;
        listView.addHeaderView(mFakeHeader);
        onSetAdapter();
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                isListViewEmpty = listView.getAdapter() == null;
                if (isListViewEmpty) {
                    scrollHeaderTo(0);
                } else {
                    final View child = absListView.getChildAt(0);
                    assert child != null;
                    scrollHeaderTo(child == mFakeHeader ? child.getTop() : -mHeaderHeight);
                    //                        Log.v("header height",String.format("mHeaderHeight:%s",mHeaderHeight));
                }
            }
        });
    } else if (content instanceof GridView) {
        isListViewEmpty = true;

        final GridView grid = (GridView) content;
        //isListViewEmpty = grid.getAdapter() == null;
        //            grid.addHeaderView(mFakeHeader);

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(mFakeHeader);
        view.addView(grid);

        grid.setOnScrollListener(new AbsListView.OnScrollListener() {

            private ListViewScrollTracker mScrollTracker;;

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            public int getGridScrollY(GridView grid) {

                Method privateStringMethod = null;
                Integer scrollOffset = null;
                Integer scrollExtent = null;
                Integer scrollRange = null;
                try {
                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollOffset", null);
                    privateStringMethod.setAccessible(true);
                    scrollOffset = (Integer) privateStringMethod.invoke(grid, null);

                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollExtent", null);
                    privateStringMethod.setAccessible(true);
                    scrollExtent = (Integer) privateStringMethod.invoke(grid, null);

                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollRange", null);
                    privateStringMethod.setAccessible(true);
                    scrollRange = (Integer) privateStringMethod.invoke(grid, null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return scrollRange;//scrollExtent*scrollOffset;  

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (mScrollTracker == null)
                    mScrollTracker = new ListViewScrollTracker(absListView);
                mScrollPosition = mScrollTracker.calculateIncrementalOffset(firstVisibleItem, visibleItemCount);
                Log.v("header", "scrollPosition:" + mScrollPosition);
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                final View child = absListView.getChildAt(0);
                if (child != null) {
                    int gridScrollY = mScrollPosition;//getGridScrollY((GridView) absListView);
                    scrollHeaderTo(-gridScrollY);

                    //change the size of the fake header on scrolling                        
                    int currentHeight = mFakeHeader.getLayoutParams().height;
                    Integer fakeHeaderHeight = (int) (mHeaderHeight - (gridScrollY * 2));
                    mFakeHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(
                            mFakeHeader.getWidth(), fakeHeaderHeight));
                    //                  setViewTranslationY(mFrameLayout, -1*(gridScrollY));
                } else
                    scrollHeaderTo(0);
            }
        });
        content = view;
    } else {

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(mFakeHeader);
        view.addView(content);

        // Put merged content to ScrollView
        final NotifyingScrollView scrollView = new NotifyingScrollView(activity);
        scrollView.addView(view);
        scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                scrollHeaderTo(-t);
            }
        });
        content = scrollView;
    }

    mFrameLayout.addView(content);
    mFrameLayout.addView(mHeader);

    // Content overlay view always shows at the top of content.
    if ((mContentOverlay = onCreateContentOverlayView(inflater, mFrameLayout)) != null) {
        mFrameLayout.addView(mContentOverlay, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    // Post initial scroll
    mFrameLayout.post(new Runnable() {
        @Override
        public void run() {
            scrollHeaderTo(0, true);
        }
    });

    return mFrameLayout;
}

From source file:com.example.android.bitmapfun.ui.SearchStreamGridFragment.java

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

    final View v = inflater.inflate(R.layout.search_image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridViewS);
    mGridView.setAdapter(mAdapter);//from  ww w  .ja  v  a  2 s.  c  o m

    final EditText editText = (EditText) v.findViewById(R.id.editText1);
    editText.setText(searchCriteria);

    TextView streamText = (TextView) v.findViewById(R.id.textView1);
    streamText.setText(String.valueOf(streamList.size()) + " results for: " + searchCriteria
            + ", click on an image to view stream...");

    Button button = (Button) v.findViewById(R.id.button_searchS);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click
            String newSearchCriteria = editText.getText().toString();
            Intent intent = new Intent(getActivity(), SearchStreamGridActivity.class);
            intent.putExtra(SearchStreamGridActivity.STREAM_SEARCH, newSearchCriteria);
            startActivity(intent);
        }
    });

    button = (Button) v.findViewById(R.id.button_streams);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click
            Intent intent = new Intent(getActivity(), ImageGridActivity.class);
            startActivity(intent);
        }
    });

    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        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() {
            if (mAdapter.getNumColumns() == 0) {

                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth =
                            //(mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                            (mGridView.getWidth() / 4) - mImageThumbSpacing;
                    //mAdapter.setNumColumns(numColumns);
                    mAdapter.setNumColumns(4);
                    mAdapter.setItemHeight(columnWidth);
                    //ViewGroup.LayoutParams params = new GridView.LayoutParams(mGridView.getWidth(), 4*(columnWidth + mImageThumbSpacing));
                    //mGridView.setLayoutParams(params);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                } /* MM: change to portrait mode 4 rows*/

            }
        }
    });

    return v;
}

From source file:com.leo.runningman.ui.ImageGridFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Window window = getActivity().getWindow();
    window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.layout_title);

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);

    TextView txt_title = (TextView) this.getActivity().findViewById(R.id.txt_title);
    txt_title.setText(getActivity().getResources().getString(R.string.app_name));
    Button btn_delete = (Button) this.getActivity().findViewById(R.id.btn_delete);
    TextView txt_delete_confirm = (TextView) this.getActivity().findViewById(R.id.txt_delete_confirm);
    btn_delete.setVisibility(View.GONE);
    txt_delete_confirm.setVisibility(View.GONE);

    layout_bottom = (RelativeLayout) v.findViewById(R.id.layout_bottom);
    autoHideview(4000);//from   w w w.ja v a 2s. co  m

    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
        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);
                    }
                }
            }
        }
    });
    mGridView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_IDLE:
                if (loadMore) {
                    new HttpGetTask(1000L).execute();
                }
                break;
            case OnScrollListener.SCROLL_STATE_FLING:
                break;
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                break;
            }
            hideBottomLayout();
            cancelAutoHideview();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            Log.i(TAG, "firstVisibleItem:--->" + firstVisibleItem + "visibleItemCount:----->" + visibleItemCount
                    + "totalItemCount:" + totalItemCount);
            loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
            start = totalItemCount;
            autoShowview(2000);
        }
    });

    adview_first = (AdView) v.findViewById(R.id.adview_first); //TODO
    return v;
}

From source file:com.fbbackup.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.album_photo_grid, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);

    TextView tx_user_name = (TextView) v.findViewById(R.id.tx_album_name);

    tx_user_name.setText(albumName);//from   w  ww.j  a  va2 s  .c  om

    cb_all = (CheckBox) v.findViewById(R.id.cb_all_photo);

    cb_all.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton v, boolean choose) {
            // TODO Auto-generated method stub

            if (choose) {

                // Toast.makeText(getActivity(), "choose:" + choose,
                // Toast.LENGTH_SHORT).show();

            } else {
                // Toast.makeText(getActivity(), "choose:" + choose,
                // Toast.LENGTH_SHORT).show();
            }
            setSelectedData(choose);

            mAdapter.notifyDataSetChanged();

        }

    });

    btn_download_photo = (ImageButton) v.findViewById(R.id.btn_download_photo);

    btn_download_photo.setOnClickListener(new ImageButton.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String dir = Utils.getDirName(albumName);

            int select = 0;

            // QarrayU
            for (int i = 0; i < isSelected.size(); i++) {

                if (isSelected.get(i)) {
                    DownloadList.downloadPhotoAlbumQueue.add(dir);
                    DownloadList.downloadPhotoQueue.add(photoArray[i]);
                    DownloadList.downloadUserNameQueue.add(name);

                    select++;
                }

            }

            DownloadList.setAddNumber(select);

            DownloadList.setUserName(name);

            MyFriendFragmentActivity.downloadHandler.sendMessage(MyFriendFragmentActivity.downloadHandler
                    .obtainMessage(MyFriendFragmentActivity.DOWNLOAD_PRB, 0, 0));

        }

    });

    isSelected = new HashMap<Integer, Boolean>();

    initSelectedData();

    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        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() {
            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);
                    }
                }
            }
        }
    });
    //imageRun();
    return v;
}