之前做上拉加载下拉刷新的时候使用PullToRefreshListView,非常方便,之后在ListView的上方加了一个轮播图,希望可以实现下拉的头部在轮播图的上方,就发现了PullToRefreshScrollView,用法和PullToRefreshListView基本上差不多。
xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/ptr_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
ptr:ptrAnimationStyle="flip"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp_img"
android:layout_width="match_parent"
android:layout_height="200dp"></android.support.v4.view.ViewPager>
<wangxuewei.bwie.com.wangxuewei1510c20171023.MyListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></wangxuewei.bwie.com.wangxuewei1510c20171023.MyListView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
这里是把ViewPager和ListView放在了ScrollView中间,这个MyListView是自己定义的一个ListView,因为使用普通的ListView会有一点问题,下面会说,ViewPager也可以换成Banner,不过PullToRefreshScrollView中间一样要有一个布局包起来
这里是写在了Fragment中的代码
package fragments;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import java.util.ArrayList;
import java.util.List;
import adapters.ImgAdapter;
import adapters.LvAdapter;
import bean.Results;
import utils.MyTask;
import wangxuewei.bwie.com.wangxuewei1510c20171023.MyListView;
import wangxuewei.bwie.com.wangxuewei1510c20171023.R;
/**
* Created by jim on 2017/10/23.
*/
public class Tab_TuiJian extends Fragment {
private List<String> img_list;
private ViewPager vp_img;
private int index = 0;
private int flag = 1;
private List<Results.DataBean> lists = new ArrayList<>();
private Handler myHandler = new Handler();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
index++;
vp_img.setCurrentItem(index);
handler.sendEmptyMessageDelayed(0, 3500);
}
}
};
private PullToRefreshScrollView ptr_lv;
private LvAdapter adapter;
private MyListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.vp_tuijian, null);
vp_img = (ViewPager) view.findViewById(R.id.vp_img);
ptr_lv = (PullToRefreshScrollView) view.findViewById(R.id.ptr_lv);
lv = (MyListView) view.findViewById(R.id.lv);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//设置无线轮播
setImgVp();
//初始化数据
getNetData();
//设置listView列表
setPtrListView();
}
//加载网络数据
private void getNetData() {
MyTask myTask = new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
Results results = gson.fromJson(jsonstr, Results.class);
List<Results.DataBean> data = results.getData();
lists.addAll(data);
//设置适配器
setAdapter();
}
});
myTask.execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0");
}
//刷新方法
private void addtoTop() {
lists.clear();
getNetData();
}
//加载方法
private void addtoBottom() {
MyTask myTask = new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
Results results = gson.fromJson(jsonstr, Results.class);
List<Results.DataBean> data = results.getData();
lists.addAll(data);
setAdapter();
}
});
flag += 20;
myTask.execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + flag);
}
//设置适配器
private void setAdapter() {
if (adapter == null) {
Log.d("zzz", lists.size() + "条数据");
adapter = new LvAdapter(lists, getActivity());
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
//配置上拉加载,下拉刷新
private void setPtrListView() {
//同时支持上拉加载下拉刷新
ptr_lv.setMode(PullToRefreshBase.Mode.BOTH);
//配置刷新的设置
ILoadingLayout startLabels = ptr_lv.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = ptr_lv.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
//刷新监听
ptr_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
addtoTop();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
ptr_lv.onRefreshComplete();
}
}, 2000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//加载更多的数据,添加到集合列表的最后面
addtoBottom();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
ptr_lv.onRefreshComplete();
}
}, 2000);
}
});
}
//加载轮播图的的资源文件
private void setImgVp() {
//加载图片数据
img_list = new ArrayList<>();
img_list.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg");
img_list.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg");
img_list.add("http://img3.3lian.com/2013/s1/20/d/57.jpg");
img_list.add("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg");
img_list.add("http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg");
vp_img.setAdapter(new ImgAdapter(img_list, getActivity()));
vp_img.setCurrentItem(index);
handler.sendEmptyMessageDelayed(0, 3500);
}
}
之前说ListView是使用的自定义的MyLIstView,使用默认的ListView的话可能会出现只加载一条数据的情况,我们的ListVIew是包起来的,所以可能得不到我们ListView的高度,所以无法正常加载我们的ListView,就需要我们自己定义一个MyListView,通过一个方法去得到我们的高
自定义的ListView
package wangxuewei.bwie.com.wangxuewei1510c20171023;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by jim on 2017/10/23.
*/
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 expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
创建一个类继承我们的ListView,重写方法,onMeasure就是我们解决这个问题的主要方法,这里有解释,解决ListView与ScollView冲突问题
其他的适配器需要自己去书写了。