pulltorefreshscrollview的简单实现(上拉刷新,下拉加载)

首先,在build.gradle文件中写入以下代码:

compile 'com.github.userswlwork:pull-to-refresh:1.0.0'

布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="zhanghaijiao.bawei.com.pulltorefreshscrollview_demo.MainActivity">
    <!--scrollview:纵向滚动条  viewGroup
        只能有一个子孩子
        -->
    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scc">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!---viewpager-->
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher_round"/>
            <!---listview列表 高度无法正确计算,只能显示一条数据
              解决办法:自定义的listview -->
            <zhanghaijiao.bawei.com.pulltorefreshscrollview_demo.MyListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/lv"></zhanghaijiao.bawei.com.pulltorefreshscrollview_demo.MyListView>
        </LinearLayout>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</RelativeLayout>

条目布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
</LinearLayout>

MainActivity中的代码:

public class MainActivity extends AppCompatActivity {
    private PullToRefreshScrollView scrollView;
    private int pageIndex=1;
private String url="https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10&page="+pageIndex;
    private MyAdapter adapter;
    private List<Result.NewslistEntity> newslist=new ArrayList<>();
    private int operType=1;//1:刷新 2:加载更多
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = findViewById(R.id.scc);
        listView = findViewById(R.id.lv);
        initScc();
        requestNetData();
    }
    private void requestNetData() {
        if(NetStateUtil.isConn(this)){
            MyTask myTask=new MyTask(new MyTask.Icallbacks() {
                @Override
                public void updateUiByjson(String jsonstr) {
                    Toast.makeText(MainActivity.this,"111",Toast.LENGTH_SHORT).show();
                    Gson gson=new Gson();
                    Result result = gson.fromJson(jsonstr, Result.class);
                    if(operType==1){
                        newslist.clear();
                    }
                    //添加新的集合数据
                    newslist.addAll(result.getNewslist());
                    //设置适配器
                    setLvAdapter();
                    //关闭头尾视图
                    scrollView.onRefreshComplete();
                }
            });
            myTask.execute(url);
        }else{
            NetStateUtil.showNoNetWorkDlg(this);
        }
    }
    public void setLvAdapter(){
        if(adapter==null){
            adapter=new MyAdapter(this,newslist);
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }
    private void initScc() {
        scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                //下拉刷新
                operType=1;
                pageIndex=1;
                url="https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10&page="+pageIndex;
                requestNetData();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                //上拉加载
                operType=2;
                pageIndex++;
                url="https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10&page="+pageIndex;
                requestNetData();
            }
        });
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值