package com.bwie.fragment; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.preference.Preference; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.media.MediaBrowserCompat; 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.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.bwie.bean.bean; import com.bwie.bean.bean2; import com.bwie.view.XListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.AbstractList; import java.util.ArrayList; import java.util.Date; import java.util.List; import Mysqlite.Mysqlitedao; import xiangmu.bwiei.com.xiangmu01.R; /** * Created by 0514 on 2017/8/17. */ public class fragment1 extends Fragment implements ViewPager.OnPageChangeListener, XListView.IXListViewListener{ private List<ImageView> listtu = new ArrayList<ImageView>(); private List<Integer> list = new ArrayList<Integer>(); private List<bean2> books1 = new ArrayList<bean2>(); private LinearLayout lv_line; private SharedPreferences sp; private int zidong; private ViewPager view_pager2; private Handler han; private View view; private XListView xlist; private ImageView te_dong; private ImageView te_deng2; private Adapter adapter; private int start = 0; private int skip=2000; private String Posturl = "http://japi.juhe.cn/comic/book?skip=2000&key=305fe3d8b1cff7311942a3a4ee72801c"; private Mysqlitedao dao; private List<bean2> books; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getContext(), R.layout.fragment1, null); view_pager2 = view.findViewById(R.id.view_Pager2); lv_line = view.findViewById(R.id.lv_line); xlist = view.findViewById(R.id.xlist); xlist.setXListViewListener(this); sp = getActivity().getSharedPreferences("s",Context.MODE_PRIVATE); xlist.setPullLoadEnable(true); xlist.setPullRefreshEnable(true); dao = new Mysqlitedao(getActivity()); inittu(); initData(); new MyAsyntask().execute(Posturl); han = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==zidong){ int currentItem = view_pager2.getCurrentItem(); currentItem++; view_pager2.setCurrentItem(currentItem); han.sendEmptyMessageDelayed(zidong,2000); } } }; han.sendEmptyMessageDelayed(zidong,2000); view_pager2.setAdapter(new Myadapter()); view_pager2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i <listtu.size(); i++) { if(i==position%list.size()) { listtu.get(i).setImageResource(R.drawable.hong); }else{ listtu.get(i).setImageResource(R.drawable.bai); } } } @Override public void onPageScrollStateChanged(int state) { } }); return view; } private void initData() { for (int i = 0; i <list.size() ; i++) { ImageView iv = new ImageView(getContext()); if(i==0) { iv.setImageResource(R.drawable.hong); }else{ iv.setImageResource(R.drawable.bai); } LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20,20); lp.setMargins(10,10,10,10); lv_line.addView(iv,lp); listtu.add(iv); } } private void inittu() { list.add(R.drawable.a); list.add(R.drawable.b); list.add(R.drawable.d); } @Override public void onRefresh() { ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if(info!=null) { han.postDelayed(new Runnable() { @Override public void run() { start = 0; books1.clear(); books.clear(); xlist.setPullLoadEnable(true); skip+=2000; Posturl = "http://japi.juhe.cn/comic/book?skip="+skip+"&key=3e09796970b04508f16de756d36d4bb8"; new MyAsyntask().execute(Posturl); onload(); } },2000); }else{ Toast.makeText(getActivity(), "请检查网络", Toast.LENGTH_SHORT).show(); onload(); } } @Override public void onLoadMore() { ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if(info!=null) { han.postDelayed(new Runnable() { @Override public void run() { start+=5; books = getItems(books1); adapter.notifyDataSetChanged(); onload(); } },2000); }else{ Toast.makeText(getActivity(), "请检查网络", Toast.LENGTH_SHORT).show(); onload(); } } private List<bean2> getItems(List<bean2> books1){ if(start+10>=books1.size()) { books.clear(); for (int i = 0; i <books1.size() ; i++) { books.add(books1.get(i)); } xlist.setPullLoadEnable(false); Toast.makeText(getActivity(), "已加载全部数据", Toast.LENGTH_SHORT).show(); start=0; books=books1; }else{ books.clear(); for (int i = 0; i <start+10 ; i++) { books.add(books1.get(i)); } } return books; } private void onload(){ xlist.stopRefresh(); xlist.stopLoadMore(); SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis()); String str = formatter.format(curDate); xlist.setRefreshTime(str); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } class Myadapter extends PagerAdapter { @Override public Object instantiateItem(ViewGroup container, int position) { View view = View.inflate(getContext(), R.layout.tu1, null); ImageView te_tu2 = view.findViewById(R.id.te_tu2); te_tu2.setImageResource(list.get(position%list.size())); container.addView(view); return view; } @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; } } class Adapter extends BaseAdapter{ private final int a = 0; private final int b = 1; private final int sum = 2; @Override public int getCount() { return books.size(); } @Override public Object getItem(int i) { return books.get(i); } @Override public long getItemId(int i) { return i; } @Override public int getItemViewType(int position) { String area = books1.get(position).area; if(area.equals("国漫")) { return a; }else{ return b; } } @Override public int getViewTypeCount() { return sum; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHloder1 viewHloder1= null; ViewHloder2 viewHloder2 = null; int type = getItemViewType(i); if(view==null) { switch (type) { case a: viewHloder1 = new ViewHloder1(); view = View.inflate(getContext(), R.layout.item1, null); view.setTag(viewHloder1); viewHloder1.te_title = view.findViewById(R.id.te_title); viewHloder1.te_lei = view.findViewById(R.id.te_lei); viewHloder1.te_dong = view.findViewById(R.id.te_dong); break; case b: viewHloder2 = new ViewHloder2(); view = View.inflate(getContext(), R.layout.item2, null); view.setTag(viewHloder2); viewHloder2.te_title2 = view.findViewById(R.id.te_title2); viewHloder2.te_lei2 = view.findViewById(R.id.te_lei2); viewHloder2.te_dong2 = view.findViewById(R.id.te_dong2); break; } }else{ switch (type){ case a: viewHloder1 = (ViewHloder1) view.getTag(); break; case b: viewHloder2 = (ViewHloder2) view.getTag(); break; } } switch (type){ case a: viewHloder1.te_title.setText(books.get(i).name); viewHloder1.te_lei.setText(books.get(i).area); DisplayImageOptions opt = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build(); ImageLoaderConfiguration con = new ImageLoaderConfiguration.Builder(getContext()).defaultDisplayImageOptions(opt).build(); ImageLoader.getInstance().init(con); ImageLoader.getInstance().displayImage(books.get(i).coverImg,viewHloder1.te_dong); break; case b: viewHloder2.te_title2.setText(books.get(i).name); viewHloder2.te_lei2.setText(books.get(i).area); DisplayImageOptions opt2 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build(); ImageLoaderConfiguration con1 = new ImageLoaderConfiguration.Builder(getContext()).defaultDisplayImageOptions(opt2).build(); ImageLoader.getInstance().init(con1); ImageLoader.getInstance().displayImage(books.get(i).coverImg,viewHloder2.te_dong2); break; } return view; } } public class ViewHloder1{ public TextView te_title,te_lei; public ImageView te_dong; } public class ViewHloder2{ public TextView te_title2,te_lei2; public ImageView te_dong2; } class MyAsyntask extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... strings) { try { final StringBuffer sb = new StringBuffer(); URL url = new URL(strings[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.flush(); outputStream.close(); connection.connect(); if(200==connection.getResponseCode()) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; while ((line=reader.readLine())!=null){ sb.append(line); } return sb.toString(); } } catch (java.io.IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if(info!=null) { if(sp.getBoolean("f",false)) { JieXi(s); dao.update("shuju",s); }else{ sp.edit().putBoolean("f",true).commit(); dao.add("shuju",s); JieXi(s); } }else{ s=dao.select(); JieXi(s); } } } //解析 private void JieXi(String s){ try { JSONObject json=new JSONObject(s); JSONObject result = json.getJSONObject("result"); JSONArray bookList = result.getJSONArray("bookList"); for (int i = 0; i <bookList.length() ; i++) { JSONObject b= (JSONObject) bookList.get(i); bean2 book = new bean2(); book.name=b.getString("name"); book.area=b.getString("area"); book.coverImg=b.getString("coverImg"); books1.add(book); //System.out.println(book.name); } if(books1!=null){ books=new ArrayList<>(); books = getItems(books1); adapter= new Adapter(); xlist.setAdapter(adapter); } } catch (Exception e) { e.printStackTrace(); } } }
shua
最新推荐文章于 2021-04-18 19:20:29 发布