1.布局 导入library,建立依赖,只能有一个子控件,所以建立linearlayout
<com.handmark.pulltorefresh.library.PullToRefreshScrollView android:id="@+id/sw" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="150dp"> android.support.v4.view.ViewPager> <com.example.administrator.pulltorefresh.MyList android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> com.example.administrator.pulltorefresh.MyList> LinearLayout> com.handmark.pulltorefresh.library.PullToRefreshScrollView>2.fragment中 无限轮播结合listview
private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==1){ vp.setCurrentItem(vp.getCurrentItem()+1); } handler.sendEmptyMessageDelayed(1,2000); } }; private List<String> imageurl_list; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.scroll_layout, container, false); sw = (PullToRefreshScrollView) v.findViewById(R.id.sw); lv = (MyList) v.findViewById(R.id.lv); vp = (ViewPager) v.findViewById(R.id.vp); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //失去焦点 lv.setFocusable(false); //首次加载数据 getDataFromNet(); //设置下拉刷新和上拉加载 sw.setMode(PullToRefreshBase.Mode.BOTH); //刷新 proxy = sw.getLoadingLayoutProxy(true, false); proxy.setPullLabel("下拉刷新"); proxy.setRefreshingLabel("正在刷新"); proxy.setReleaseLabel("放开刷新"); //加载 ILoadingLayout proxy1 = sw.getLoadingLayoutProxy(false, true); proxy1.setPullLabel("上拉加载"); proxy1.setRefreshingLabel("正在加载"); proxy1.setReleaseLabel("放开加载"); //刷新的点击事件 sw.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { NetDataUtil.getData("http://gank.io/api/data/Android/10/1", getActivity(), new JsonCallBack() { @Override public void getJsonString(String json) { Gson gson = new Gson(); DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class); List<DataDataBean.ResultsBean> results = dataDataBean.getResults(); list.addAll(results); setadp(); //停止刷新 sw.onRefreshComplete(); } }); } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { page++; getDataFromNet(); } }); //设置无限轮播 LunBo(); } private void LunBo() { NetDataUtil.getData("http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android", getActivity(), new JsonCallBack() { @Override public void getJsonString(String json) { imageurl_list= new ArrayList<>(); Gson gson = new Gson(); LunBoBean lunBoBean= gson.fromJson(json, LunBoBean.class); List<LunBoBean.DataBean.EssayBean> essay = lunBoBean.getData().getEssay(); for (LunBoBean.DataBean.EssayBean e:essay) { List<LunBoBean.DataBean.EssayBean.AuthorBean> author= e.getAuthor(); String web_url= author.get(0).getWeb_url(); imageurl_list.add(web_url); } vp.setAdapter(new MyPageAdp(getActivity(),imageurl_list)); vp.setCurrentItem(imageurl_list.size()*1000); handler.sendEmptyMessageDelayed(1,2000); } }); } private void getDataFromNet() { NetDataUtil.getData("http://gank.io/api/data/Android/10/" + page, getActivity(), new JsonCallBack() { @Override public void getJsonString(String json) { Gson gson = new Gson(); DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class); List<DataDataBean.ResultsBean> results = dataDataBean.getResults(); list.addAll(results); setadp(); //停止刷新 sw.onRefreshComplete(); } }); } private void setadp() { if (adp == null) { adp = new Myadp(getActivity(), list); lv.setAdapter(adp); } else { adp.notifyDataSetChanged(); } }
3.适配器
public class MyPageAdp extends PagerAdapter{ Context context; List<String> imageurl_list; public MyPageAdp(Context context, List<String> imageurl_list) { this.context=context; this.imageurl_list=imageurl_list; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView image=new ImageView(context); ImageLoader.getInstance().displayImage(imageurl_list.get(position%imageurl_list.size()),image, ImageLoaderUtil.getDefaultOption()); container.addView(image); return image; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } }4.自己写一个listview,在布局中代替listview
public class MyList extends ListView{ public MyList(Context context) { super(context); } public MyList(Context context, AttributeSet attrs) { super(context, attrs); } public MyList(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); } }