//下面我们用GridView做展示 PullToRefresh可以用于多种组件的上拉加载下拉刷新
让我们看一下gridView的布局
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.baway.test.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshGridView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:numColumns="3"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</RelativeLayout>
主来中的实现代码:
import java.util.List;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateUtils;
import android.widget.GridView;
import android.widget.Toast;
import com.baway.bean.Book;
import com.baway.bean.MyAdapter;
import com.baway.bean.MyHttpClient;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
public class MainActivity extends ActionBarActivity {
private PullToRefreshGridView mGridView;
private int i=10;
private GridView actualListView;
private MyAdapter adapter;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_list);//1
mGridView.setOnRefreshListener(new OnRefreshListener() { //2
@Override
public void onRefresh(PullToRefreshBase refreshView) {
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);//3.刷新界面处理代理,显示新的时间
new GetDataTask().execute();//4.执行自定义AsyncTask,进行数据的请求刷新
}
});
mGridView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(MainActivity.this, "正在加载。。。", 0).show();
new GetDataTask().execute();//4.执行自定义AsyncTask,进行数据的请求刷新
}
});
actualListView = mGridView.getRefreshableView();//6!!!----从下拉刷新组件中获得ListView组件,以便适配数据
}
private class GetDataTask extends AsyncTask<Void, Void, List<Book>>{
@Override
protected List<Book> doInBackground(Void... params) {
String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=10&rn="+i;
i+=10;
List<Book> list=null;
MyHttpClient httpClient=new MyHttpClient();
list=httpClient.myClient(url);
return list;
}
@Override
protected void onPostExecute(List<Book> result) {
adapter=new MyAdapter(MainActivity.this,result);//这个地方需要添加参数
actualListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mGridView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
当然了我们还需要写javabean adapter httpClient连接网络请求数据,最后不要忘了添加连接网络的权限 此处就不贴代码了