Android Open Source - PullToRefresh Grid View Activity






From Project

Back to project page PullToRefresh.

License

The source code is released under:

Apache License

If you think the Android project PullToRefresh listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 Chris Banes// w  w  w. j a  v  a2  s . c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.senab.actionbarpulltorefresh.samples.stock;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;

/**
 * This sample shows how to use ActionBar-PullToRefresh with a {@link android.widget.GridView
 * GridView}, and manually creating (and attaching) a {@link PullToRefreshLayout} to the view.
 */
public class GridViewActivity extends BaseSampleActivity
        implements OnRefreshListener {

    private static String[] ITEMS = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
            "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Abbaye de Belloc",
            "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost",
            "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler"};

    private PullToRefreshLayout mPullToRefreshLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        GridView gridView = (GridView) findViewById(R.id.ptr_gridview);
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                ITEMS);
        gridView.setAdapter(adapter);

        // Now find the PullToRefreshLayout and set it up
        mPullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout);
        ActionBarPullToRefresh.from(this)
                .options(Options.create()
                        // Here we make the refresh scroll distance to 75% of the GridView height
                        .scrollDistance(.75f)
                        // Here we define a custom header layout which will be inflated and used
                        .headerLayout(R.layout.customised_header)
                        // Here we define a custom header transformer which will alter the header
                        // based on the current pull-to-refresh state
                        .headerTransformer(new CustomisedHeaderTransformer())
                        .build())
                .allChildrenArePullable()
                .listener(this)
                // Here we'll set a custom ViewDelegate
                .useViewDelegate(GridView.class, new AbsListViewDelegate())
                .setup(mPullToRefreshLayout);
    }

    @Override
    public void onRefreshStarted(View view) {
        /**
         * Simulate Refresh with 4 seconds sleep
         */
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(Constants.SIMULATED_REFRESH_LENGTH);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                // Notify PullToRefreshLayout that the refresh has finished
                mPullToRefreshLayout.setRefreshComplete();
            }
        }.execute();
    }

    /**
     * Here's a customised header transformer which displays the scroll progress as text.
     */
    static class CustomisedHeaderTransformer extends HeaderTransformer {

        private View mHeaderView;
        private TextView mMainTextView;
        private TextView mProgressTextView;

        @Override
        public void onViewCreated(Activity activity, View headerView) {
            mHeaderView = headerView;
            mMainTextView = (TextView) headerView.findViewById(R.id.ptr_text);
            mProgressTextView = (TextView) headerView.findViewById(R.id.ptr_text_secondary);
        }

        @Override
        public void onReset() {
            mMainTextView.setVisibility(View.VISIBLE);
            mMainTextView.setText(R.string.pull_to_refresh_pull_label);

            mProgressTextView.setVisibility(View.GONE);
            mProgressTextView.setText("");
        }

        @Override
        public void onPulled(float percentagePulled) {
            mProgressTextView.setVisibility(View.VISIBLE);
            mProgressTextView.setText(Math.round(100f * percentagePulled) + "%");
        }

        @Override
        public void onRefreshStarted() {
            mMainTextView.setText(R.string.pull_to_refresh_refreshing_label);
            mProgressTextView.setVisibility(View.GONE);
        }

        @Override
        public void onReleaseToRefresh() {
            mMainTextView.setText(R.string.pull_to_refresh_release_label);
        }

        @Override
        public void onRefreshMinimized() {
            // In this header transformer, we will ignore this call
        }

        @Override
        public boolean showHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE;
            if (changeVis) {
                mHeaderView.setVisibility(View.VISIBLE);
            }
            return changeVis;
        }

        @Override
        public boolean hideHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() == View.VISIBLE;
            if (changeVis) {
                mHeaderView.setVisibility(View.GONE);
            }
            return changeVis;
        }
    }
}




Java Source Code List

android.support.v7.appcompat.BuildConfig.java
android.support.v7.appcompat.BuildConfig.java
android.support.v7.gridlayout.BuildConfig.java
android.support.v7.gridlayout.BuildConfig.java
fr.castorflex.android.smoothprogressbar.BuildConfig.java
fr.castorflex.android.smoothprogressbar.BuildConfig.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.AbcDefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.AbcPullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.AbsDefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.AbsPullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh.java
uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh.java
uk.co.senab.actionbarpulltorefresh.library.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.library.BuildConfig.java
uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.EnvironmentDelegate.java
uk.co.senab.actionbarpulltorefresh.library.EnvironmentDelegate.java
uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer.java
uk.co.senab.actionbarpulltorefresh.library.InstanceCreationUtils.java
uk.co.senab.actionbarpulltorefresh.library.InstanceCreationUtils.java
uk.co.senab.actionbarpulltorefresh.library.Options.java
uk.co.senab.actionbarpulltorefresh.library.Options.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout.java
uk.co.senab.actionbarpulltorefresh.library.listeners.HeaderViewListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.HeaderViewListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener.java
uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatBase.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatBase.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV11.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV11.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV16.java
uk.co.senab.actionbarpulltorefresh.library.sdk.CompatV16.java
uk.co.senab.actionbarpulltorefresh.library.sdk.Compat.java
uk.co.senab.actionbarpulltorefresh.library.sdk.Compat.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ScrollYDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ScrollYDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.WebViewDelegate.java
uk.co.senab.actionbarpulltorefresh.library.viewdelegates.WebViewDelegate.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabPager.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.FragmentTabsViewPagerActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarcompat.WebViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabPager.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.FragmentTabsViewPagerActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.actionbarsherlock.WebViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.BaseSampleActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.Constants.java
uk.co.senab.actionbarpulltorefresh.samples.stock.FragmentTabsActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.GridViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.ListViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.MainActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.ScrollViewActivity.java
uk.co.senab.actionbarpulltorefresh.samples.stock.WebViewActivity.java