pulltorefreshscrollview 布局 主页面 MyListView主页面

<?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>





package zhanghaijiao.bawei.com.pulltorefreshscrollview_demo;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;


import java.util.ArrayList;
import java.util.List;


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();
            }
        });
    }
}





package zhanghaijiao.bawei.com.pulltorefreshscrollview_demo;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;


/**
 * Created by jane on 2018/1/10.
 */


public class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }


    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //重新计算高度
        int newHeight=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, newHeight);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值