上拉加载下拉刷新控件

本文通过使用Android自带的一些原生控件,实现了ListView,GridView,ScrollView以及 侧滑弹出菜单按钮的ListView 上拉加载下拉刷新的功能。

首先是效果图:


1、上拉加载下拉刷新ListView控件:SwipeRefreshListView

listview


2、上拉加载下拉刷新GridView控件:SwipeRefreshGridView
gridview


3、上拉加载下拉刷新ScrollView控件:

SwipeRefreshScrollView(注意此控件只能有一个孩子,和ScrollView一样)
scrollview


4、侧滑弹出菜单按钮的ListView控件:MuneListView
menu_listview


5、上拉加载下拉刷新且侧滑可弹出菜单按钮的ListView控件:SwipeRefreshMuneListView
swipe_menu_listview

导入自己的项目:

简单介绍一下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上面的项目地址:项目地址


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值