本文通过使用Android自带的一些原生控件,实现了ListView,GridView,ScrollView以及 侧滑弹出菜单按钮的ListView 上拉加载下拉刷新的功能。
首先是效果图:
1、上拉加载下拉刷新ListView控件:SwipeRefreshListView
2、上拉加载下拉刷新GridView控件:SwipeRefreshGridView
3、上拉加载下拉刷新ScrollView控件:
SwipeRefreshScrollView(注意此控件只能有一个孩子,和ScrollView一样)
4、侧滑弹出菜单按钮的ListView控件:MuneListView
5、上拉加载下拉刷新且侧滑可弹出菜单按钮的ListView控件:SwipeRefreshMuneListView
导入自己的项目:
-
Android Studio
compile 'com.afinalstone:SwiperefreshView:1.0.4'
-
Eclipse
下载最新jar包:swiprefreshview-1.0.1.jar:
https://github.com/AFinalStone/SwiperefreshView-master/blob/master/jar/swiprefreshview-1.0.1.jar?raw=true注:需要同时导入最新的android-support-v4.jar:
https://github.com/AFinalStone/SwiperefreshView-master/blob/master/jar/android-support-v4.jar?raw=true
简单介绍一下ListView和GridView刷新控件的使用方法:
1、SwipeRefreshListView的使用方法:
- 首先是activity_listview.xml布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<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="com.shi.androidstudy.swiperefreshview_master.ListViewActivity">
<com.afinalstone.androidstudy.swiperefreshview.SwipeRefreshListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
- 然后是ListViewActivity代码内容:
public class ListViewActivity extends AppCompatActivity implements OnSwipeRefreshViewListener {
private SwipeRefreshListView listView;
private int[] imagesUrl = {R.mipmap.item01,R.mipmap.item02,R.mipmap.item03
,R.mipmap.item02,R.mipmap.item01,R.mipmap.item03,R.mipmap.item02,R.mipmap.item03};
private MyAdapter adapter = new MyAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
listView = (SwipeRefreshListView) findViewById(R.id.listview);
listView.getListView().setAdapter(adapter);
listView.setOnRefreshListener(this);//这句是关键
listView.openRefreshState();
}
@Override
public void onTopRefrushListener() {
Toast.makeText(ListViewActivity.this,"顶部刷新被执行",Toast.LENGTH_SHORT).show();
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.closeRefreshState();
}
},5000);
}
@Override
public void onBottomRefrushListener() {
Toast.makeText(ListViewActivity.this,"底部刷新被执行",Toast.LENGTH_SHORT).show();
listView.postDelayed(new Runnable() {
@Override
public void run() {
listView.closeRefreshState();
}
},5000);
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return imagesUrl.length;
}
@Override
public Object getItem(int position) {
return imagesUrl[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(ListViewActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,150));
imageView.setImageResource(imagesUrl[position]);
return imageView;
}
}
}
2、SwipeRefreshGridView的使用方法:
- 首先是activity_gridview.xml布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<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="com.shi.androidstudy.swiperefreshview_master.GridViewActivity">
<com.afinalstone.androidstudy.swiperefreshview.SwipeRefreshGridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:stretchMode="columnWidth"/>
</RelativeLayout>
- 然后是GridViewActivity代码内容:
public class GridViewActivity extends AppCompatActivity implements OnSwipeRefreshViewListener{
private SwipeRefreshGridView gridview;
private int[] imagesUrl = {R.mipmap.item01,R.mipmap.item02,R.mipmap.item03
,R.mipmap.item02,R.mipmap.item01,R.mipmap.item03,R.mipmap.item02,R.mipmap.item03,
R.mipmap.item02,R.mipmap.item01,R.mipmap.item03};
private MyAdapter adapter = new MyAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
gridview = (SwipeRefreshGridView) findViewById(R.id.gridview);
gridview.getGridView().setAdapter(adapter);
gridview.setOnRefreshListener(this);
gridview.openRefreshState();
}
@Override
public void onTopRefrushListener() {
Toast.makeText(GridViewActivity.this,"顶部刷新被执行",Toast.LENGTH_SHORT).show();
gridview.postDelayed(new Runnable() {
@Override
public void run() {
gridview.closeRefreshState();
}
},5000);
}
@Override
public void onBottomRefrushListener() {
Toast.makeText(GridViewActivity.this,"底部刷新被执行",Toast.LENGTH_SHORT).show();
gridview.postDelayed(new Runnable() {
@Override
public void run() {
gridview.closeRefreshState();
}
},5000);
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return imagesUrl.length;
}
@Override
public Object getItem(int position) {
return imagesUrl[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(GridViewActivity.this);
imageView.setLayoutParams(new AbsListView.LayoutParams(180,180));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(imagesUrl[position]);
return imageView;
}
}
}
代码很简单,控件使用也很简单,我就不多说了,有兴趣的童鞋可以下载下来研究一下。
最后附上GitHub上面的项目地址:项目地址