package duanxinmeng.bwie.com.sanzhoukao; import android.os.AsyncTask; 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.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ScrollView; import android.widget.Toast; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshScrollView; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import duanxinmeng.bwie.com.sanzhoukao.util.ImageLoderUtil; import duanxinmeng.bwie.com.sanzhoukao.util.NetWorkUtil; /** * Created by Administrator on 2017/10/24/024. */ public class Fragment_Tj extends Fragment { private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); num++; viewPager.setCurrentItem(num); handler.sendEmptyMessageDelayed(0,2000); } }; private PullToRefreshScrollView pullToRefreshListView; private ViewPager viewPager; private MyListview listView; private int startNum = 0; private List<DataDataBean.DataBean> list = new ArrayList<>(); private MyAdapter adapter; private List<Integer> tu; private int num = 0; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tj,container,false); pullToRefreshListView = (PullToRefreshScrollView) view.findViewById(R.id.refresh_list_view); viewPager = (ViewPager) view.findViewById(R.id.viewpager); listView = (MyListview) view.findViewById(R.id.listview); //轮播图 // tu = new ArrayList<>(); // tu.add(R.drawable.jay_a); // tu.add(R.drawable.jay_b); // tu.add(R.drawable.jay_c); // tu.add(R.drawable.jay_d); // tu.add(R.drawable.jay_e); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getDataFrom(); if (NetWorkUtil.isConn(getActivity())){ Toast.makeText(getActivity(),"网络已连接~",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getActivity(),"网络未连接!",Toast.LENGTH_SHORT).show(); NetWorkUtil.showNoNetWorkDlg(getActivity()); } //刷新模式 pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); ILoadingLayout startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在拉"); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); //监听 pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { getDataFrom(); if (NetWorkUtil.isConn(getActivity())){ Toast.makeText(getActivity(),"网络已连接~",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getActivity(),"网络未连接!",Toast.LENGTH_SHORT).show(); NetWorkUtil.showNoNetWorkDlg(getActivity()); } } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { startNum = startNum +20; getModeLoad(); } }); } public void getDataFrom() { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { try { URL url = new URL("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET");//设置请求方式 connection.setReadTimeout(5000); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class); //有轮播图的数据,,,还有listView的数据 list.addAll(0,dataDataBean.getData()); setAdapter(); //刷新停止 pullToRefreshListView.onRefreshComplete(); //轮播图数据 viewPager.setAdapter(new PagerAdapter() { @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(getActivity()); // Integer integer = tu.get(position % tu.size()); // imageView.setImageResource(integer); ImageLoader.getInstance().displayImage((String) list.get(position%list.size()).getIMAGEURL(),imageView, ImageLoderUtil.getOption()); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }); //发送 handler.sendEmptyMessageDelayed(0,2000); } }; asyncTask.execute(); } //设置适配器 private void setAdapter() { if (adapter == null){ adapter = new MyAdapter(getActivity(),list); listView.setAdapter(adapter); }else { adapter.notifyDataSetChanged(); } } private String streamToString(InputStream inputStream, String string) { InputStreamReader inputStreamReader = null; try { inputStreamReader = new InputStreamReader(inputStream,string); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder builder = new StringBuilder(); String s = null; while ((s = bufferedReader.readLine()) != null) { //添加 builder.append(s); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } public void getModeLoad() { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { try { URL url = new URL("http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+startNum); try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); return json; } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { Gson gson = new Gson(); DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class); list.addAll(dataDataBean.getData()); //设置适配器 setAdapter(); pullToRefreshListView.onRefreshComplete(); } }; asyncTask.execute(); } }
PullToRefreshScrollView刷新轮播图和ListView以及上拉下拉、网络获取图片并提示
最新推荐文章于 2021-03-23 11:33:56 发布