问题:如何实现listview和gridview的下拉刷新与上拉加载?
回答:使用XListView或者PullToFreshView中的类,再进行调用即可
例子:
①listview----XListView
strings.xml
<string name="xlistview_header_hint_normal">下拉刷新</string>
<string name="xlistview_header_hint_ready">松开刷新数据</string>
<string name="xlistview_header_hint_loading">正在加载...</string>
<string name="xlistview_header_last_time">上次更新时间:</string>
<string name="xlistview_footer_hint_normal">查看更多</string>
<string name="xlistview_footer_hint_ready">松开载入更多</string>
xlistview_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" >
<RelativeLayout
android:id="@+id/xlistview_header_content"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#ffffff" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" android:id="@+id/xlistview_header_text">
<TextView
android:id="@+id/xlistview_header_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_hint_normal"
android:textColor="#000000" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_last_time"
android:textSize="12sp"
android:textColor="#000000" />
<TextView
android:id="@+id/xlistview_header_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/xlistview_header_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-35dp"
android:src="@drawable/xlistview_arrow" />
<ProgressBar
android:id="@+id/xlistview_header_progressbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-40dp"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
xlistview_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/xlistview_footer_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#ffffff" >
<ProgressBar
android:id="@+id/xlistview_footer_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="@+id/xlistview_footer_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#000000"
android:text="@string/xlistview_footer_hint_normal" />
</RelativeLayout>
</LinearLayout>
fragment_home.xml
<com.example.aaa.galview.XListView
android:id="@+id/xListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</com.example.weipa.galview.XListView>
HomeFragment.java
public class HomeFragment extends Fragment {
// 声明管理对象
public static AllThemesInfoManager allThemesInfoManager;
// 创建出分页对象
public static PageInfo pageInfo = new PageInfo();
// 声明listView控件对象
private XListView mListView;
// 声明适配器对象
private AllThemesAdapter mAdapter;
private Handler mHandler;
private String date;
private int showItem = 0;
private List<Themes> entities = new ArrayList<Themes>();
public List<Themes> getEntities() {
return entities;
}
private void onLoad() {
mListView.stopRefresh();
mListView.stopLoadMore();
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
date = format.format(new Date());
mListView.setRefreshTime(date);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case WhatUtil.QUERYCATEGORIES:
pageInfo = allThemesInfoManager.getPageInfo();
entities = allThemesInfoManager.getEntities();
mListView.setPullLoadEnable(true);
mAdapter = new AllThemesAdapter(getActivity(), entities, getActivity());
mListView.setAdapter(mAdapter);
mListView.setSelection(showItem);
// 设置item点击事件
mListView.setOnItemClickListener(new myOnItemClickListener());
mListView.setXListViewListener(new MyIXListViewListener());
mHandler = new Handler();
break;
}
}
};
class MyIXListViewListener implements IXListViewListener {
@Override
public void onRefresh() {
// TODO Auto-generated method stub
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
entities.clear();
allThemesInfoManager.queryObjects(1);
showItem = 0;
mAdapter.notifyDataSetChanged();
onLoad();
}
}, 1000);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (pageInfo.getNowPage() < pageInfo.getCountPage()) {
allThemesInfoManager.queryObjects(pageInfo.getNowPage() + 1);
showItem = entities.size();
} else if (pageInfo.getNowPage() == pageInfo.getCountPage()) {
Toast.makeText(getActivity(), "已经是最后一页了*-*", 1).show();
}
mAdapter.notifyDataSetChanged();
onLoad();
}
}, 1000);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_home, container, false);
mListView = (XListView) v.findViewById(R.id.xListView);
// 创建管理对象
allThemesInfoManager = new AllThemesInfoManager(getActivity(),handler);
allThemesInfoManager.queryObjects(1);
return v;
}
}
②gridview-----PullToFreshView
strings.xml
<string name="pull_to_refresh_pull_label">下拉刷新</string>
<string name="pull_to_refresh_release_label">松开后刷新</string>
<string name="pull_to_refresh_refreshing_label">正在刷新\u2026</string>
<string name="pull_to_refresh_footer_release_label">松开后加载</string>
<string name="pull_to_refresh_footer_pull_label">上拉加载更多</string>
<string name="pull_to_refresh_footer_refreshing_label">正在加载中\u2026</string>
<string name="text">http://miloisbadboy.com/</string>
refresh_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pull_to_refresh_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="15dp"
android:paddingTop="10dp" >
<ProgressBar
android:id="@+id/pull_to_refresh_progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:visibility="gone" />
<ImageView
android:id="@+id/pull_to_refresh_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:src="@drawable/ic_pulltorefresh_arrow"
android:visibility="visible" />
<TextView
android:id="@+id/pull_to_refresh_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000000"
android:text="@string/pull_to_refresh_pull_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/pull_to_refresh_updated_at"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_below="@+id/pull_to_refresh_text"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="gone" />
</RelativeLayout>
refresh_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pull_to_refresh_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ProgressBar
android:id="@+id/pull_to_load_progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:visibility="gone" />
<ImageView
android:id="@+id/pull_to_load_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:src="@drawable/ic_pulltorefresh_arrow_up"
android:visibility="visible" />
<TextView
android:id="@+id/pull_to_load_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000000"
android:text="@string/pull_to_refresh_footer_pull_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</RelativeLayout>
fragment_viewer.xml
<com.example.aaa.galview.PullToRefreshView
android:id="@+id/pull_refresh_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 这里放置listview,gridview或者scrollview的布局 ,PullToRefreshView
要设置android:orientation="vertical"属性 否则,显示不正确 -->
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:verticalSpacing="10dp" />
</com.example.aaa.galview.PullToRefreshView>
ViewerFragment.java
public class ViewerFragment extends Fragment {
private PullToRefreshView mPullToRefreshView;
private GridView mGridView;
private List<Themes> entities;
// 声明适配器对象
private TestAdapter adapter;
// 声明管理对象
private TestInfoManager testInfoManager;
// 创建出分页对象
private PageInfo pageInfo = new PageInfo();
private int showItem = 0;
private View view;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case WhatUtil.QUERYCATEGORIES:
// 创建适配器对象
pageInfo = testInfoManager.getPageInfo();
entities = testInfoManager.getEntities();
adapter = new TestAdapter(getActivity(),
entities, getActivity());
// 设置GridView控件的适配器
mGridView.setAdapter(adapter);
mGridView.setSelection(showItem);
mPullToRefreshView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
@Override
public void onHeaderRefresh(PullToRefreshView view) {
// TODO Auto-generated method stub
mPullToRefreshView.postDelayed(new Runnable() {
@Override
public void run() {
entities.clear();
testInfoManager.queryObjects(1);
showItem = 0;
adapter.notifyDataSetChanged();
// 设置更新时间
mPullToRefreshView.onHeaderRefreshComplete();
}
}, 1000);
}
});
mPullToRefreshView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
@Override
public void onFooterRefresh(PullToRefreshView view) {
// TODO Auto-generated method stub
mPullToRefreshView.postDelayed(new Runnable() {
@Override
public void run() {
System.out.println("上拉加载");
if (pageInfo.getNowPage() != pageInfo.getCountPage()) {
testInfoManager.queryObjects(pageInfo.getNowPage() + 1);
showItem = entities.size()-1;
}else if(pageInfo.getNowPage() == pageInfo.getCountPage()){
Toast.makeText(getActivity(), "已经是最后一页了*-*", 1).show();
}
adapter.notifyDataSetChanged();
mPullToRefreshView.onFooterRefreshComplete();
}
}, 1000);
}
});
break;
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_viewer, container, false);
mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_refresh_view);
mGridView = (GridView) view.findViewById(R.id.gridview);
// 创建管理对象
testInfoManager = new TestInfoManager(getActivity(), handler);
// 获取当前页的数据
testInfoManager.queryObjects(1);
return view;
}
}