无线自动轮播,获取网络上的图片+scrollView+listView 联动
结合pulltorefresh那篇:
效果:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#f890" android:orientation="vertical" android:layout_height="match_parent"> <com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/refresh_scroll_view" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="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/scroll_vp" android:layout_width="match_parent" android:layout_height="200dp"> </android.support.v4.view.ViewPager> <com.example.day16_pulltorefresh.view.MyListView android:id="@+id/scroll_lv" android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.day16_pulltorefresh.view.MyListView> </LinearLayout> </com.handmark.pulltorefresh.library.PullToRefreshScrollView> </LinearLayout>
代码:
public class FragmentScroll extends Fragment{ List<DataDataBean.ResultsBean> list=new ArrayList<>(); private PullToRefreshScrollView refreshScrollView; private ViewPager viewPager; private ListView listView; private int num_page=1; private ListAdapter adapter; private ILoadingLayout startLabels; private List<String> imglist;
Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what==0){ viewPager.setCurrentItem(viewPager.getCurrentItem()+1); handler.sendEmptyMessageDelayed(0,2000); } } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_scroll, container, false); refreshScrollView = (PullToRefreshScrollView) view.findViewById(R.id.refresh_scroll_view); viewPager = (ViewPager) view.findViewById(R.id.scroll_vp); listView = (ListView) view.findViewById(R.id.scroll_lv); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //失去焦点 listView.setFocusable(false); //轮播 String lbpath="http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android"; NetConnection.getconnection(getActivity(), lbpath, new CallJsonBack() { @Override public void getcallBack(String json) { imglist = new ArrayList<String>(); Gson gson=new Gson(); GridBean gridBean = gson.fromJson(json, GridBean.class); List<GridBean.DataBean.EssayBean> essay = gridBean.getData().getEssay(); for (GridBean.DataBean.EssayBean essayBean: essay) { imglist.add(essayBean.getAuthor().get(0).getWeb_url()); } VpAdapter vpAdapter=new VpAdapter(imglist,getActivity()); viewPager.setAdapter(vpAdapter); viewPager.setCurrentItem(imglist.size()*100000); handler.sendEmptyMessageDelayed(0,2000); } }); //listView展示 getconn(); startLabels = refreshScrollView.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在拉"); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = refreshScrollView.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); refreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { String path="http://gank.io/api/data/Android/10/1"; NetConnection.getconnection(getActivity(), path, new CallJsonBack() { @Override public void getcallBack(String json) { Gson gson=new Gson(); DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class); Log.i("-----sc2",dataDataBean.toString()); list.addAll(0,dataDataBean.getResults()); setAdapter(); //停止刷新 refreshScrollView.onRefreshComplete(); startLabels.setLastUpdatedLabel("上次更新时间:"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis()))); } }); } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { num_page++; getconn(); } }); } private void getconn() { String path="http://gank.io/api/data/Android/10/"+num_page; NetConnection.getconnection(getActivity(), path, new CallJsonBack() { @Override public void getcallBack(String json) { Gson gson=new Gson(); DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class); Log.i("----s1",dataDataBean.toString()); List<DataDataBean.ResultsBean> results = dataDataBean.getResults(); Log.i("-----s1",results.toString()); list.addAll(results); setAdapter(); //停止刷新 refreshScrollView.onRefreshComplete(); } }); } private void setAdapter() { if (adapter==null){ adapter = new ListAdapter(list,getActivity()); listView.setAdapter(adapter); }else{ adapter.notifyDataSetChanged(); } } }
轮播的适配器
public class VpAdapter extends PagerAdapter{ List<String> imglist; Context con; public VpAdapter(List<String> imglist, Context con) { this.imglist = imglist; this.con = con; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView=new ImageView(con); imageView.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(imglist.get(position%imglist.size()),imageView, ImageUtil.getoption()); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
ScrollView和ListView有冲突所以
自定义个类继承Listview
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的全路径,复制到布局listView上(参考布局)