根据类库惊醒上拉加载,下拉刷新以及一些小小的知识点

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

  <com.handmark.pulltorefresh.library.PullToRefreshGridView
      android:id="@+id/grid_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:fastScrollEnabled="false"  
        android:footerDividersEnabled="false"  
        android:headerDividersEnabled="false"  
        android:smoothScrollbar="true" 
    
      ></com.handmark.pulltorefresh.library.PullToRefreshGridView>

</RelativeLayout>

MainActivity.class:

package com.example.leiku_one;

import java.util.Arrays;  
import java.util.LinkedList;  
  
import android.app.Activity;  
import android.graphics.Color;
import android.os.AsyncTask;  
import android.os.Bundle;  
import android.text.format.DateUtils;  
import android.view.Menu;  
import android.widget.ArrayAdapter;  
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ListView;  
  
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;  
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.handmark.pulltorefresh.library.PullToRefreshListView; 
import com.handmark.pulltorefresh.library.internal.Utils;
/**
 * 根据导入libary作为类库,然后调用里面的方法以及自定义view,实现gridview上拉加载下拉刷新
 * @author 梁明辉
 *
 */
public class MainActivity extends Activity {  
    //导用类库里面的这个类
    private PullToRefreshGridView mPullToRefreshListView;  
    private LinkedList<String> mListItems;  
    private ArrayAdapter<String> mAdapter;
    private boolean isRefreshing;
    
//    mPullRefreshListView.getLoadingLayoutProxy(true, false);接收两个参数,为true,false返回设置下拉的ILoadingLayout;为false,true返回设置上拉的。
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        // Set a listener to be invoked when the list should be refreshed.  
        mPullToRefreshListView = (PullToRefreshGridView) findViewById(R.id.grid_view);  
        ILoadingLayout startLabels = mPullToRefreshListView  
        .getLoadingLayoutProxy();  
        startLabels.setPullLabel("你可劲拉,拉...");// 刚下拉时,显示的提示  
        startLabels.setRefreshingLabel("好嘞,正在刷新...");// 刷新时  
        startLabels.setReleaseLabel("你敢放,我就敢刷新...");// 下来达到一定距离时,显示的提示  
        mPullToRefreshListView.setMode(Mode.BOTH);//设置你需要的模式
       mPullToRefreshListView.setBackgroundColor(Color.RED);
     
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<GridView>() {  
            @Override  
            public void onRefresh(PullToRefreshBase<GridView> refreshView) {  
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  
                  
                // Update the LastUpdatedLabel  
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  
                  
                // Do work to refresh the list here.  
                new GetDataTask().execute();  
            }  
        });  
          
        GridView actualListView = mPullToRefreshListView.getRefreshableView();  
          
        mListItems = new LinkedList<String>();  
        mListItems.addAll(Arrays.asList(mStrings));  
          
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); 
       
        actualListView.setAdapter(mAdapter);  
        actualListView.setNumColumns(2);
    }  
  
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {  
  
        @Override  
        protected String[] doInBackground(Void... params) {  
            // Simulates a background job.  
            try {  
                Thread.sleep(4000);  
            } catch (InterruptedException e) {  
            }  
            return mStrings;  
        }  
        @Override  
        protected void onPostExecute(String[] result) {  
            mListItems.addFirst("Added after refresh...");  
            mAdapter.notifyDataSetChanged();  
              
            // Call onRefreshComplete when the list has been refreshed.  
            mPullToRefreshListView.onRefreshComplete();  
            super.onPostExecute(result);  
        }  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
//        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
    private String[] mStrings = { "John", "Michelle", "Amy", "Kim", "Mary",  
            "David", "Sunny", "James", "Maria", "Michael", "Sarah", "Robert",  
            "Lily", "William", "Jessica", "Paul", "Crystal", "Peter",  
            "Jennifer", "George", "Rachel", "Thomas", "Lisa", "Daniel", "Elizabeth",  
            "Kevin" };  
}  

效果图:

 

posted @ 2016-04-21 20:28  151  阅读(224)  评论(0编辑  收藏  举报