您的位置:首页 > 其它

pulltorefreshscrollview 布局 主页面 MyListView主页面

2018-01-17 13:37 477 查看
<?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) {

                //下拉刷新

                ope
4000
rType=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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: