從網路獲取圖片實現無限輪播 外賣公眾號開發找捌躍科技
//網路請求資料工具類
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;
public class HttpUrlUtils {
private static String string;
public static String requestData(String s){ HttpURLConnection urlConnection;
try { URL url = new URL(s); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(5000); urlConnection.setConnectTimeout(5000); urlConnection.setRequestMethod("GET"); int responseCode = urlConnection.getResponseCode(); if (responseCode==200){ string = readstring(urlConnection.getInputStream()); } } catch (Exception e) { e.printStackTrace(); }
return string; } //將流轉換為資料 private static String readstring(InputStream inputStream) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String temp=""; while ((temp=br.readLine())!=null){ stringBuilder.append(temp); } return stringBuilder.toString(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 }
MainActivity
import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.TextView;
import com.example.qmy.a1020.R;
import com.example.qmy.a1020.adapter.ImageAdapter; import com.example.qmy.a1020.model.Banner; import com.example.qmy.a1020.utils.HttpUrlUtils; import com.google.gson.Gson;
import java.util.List;
public class AFragment extends android.support.v4.app.Fragment { ViewPager vpager; ImageAdapter imageAdapter; LinearLayout line; TextView tv; View view; Banner banner;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
imageAdapter.setDate((List<Banner.DataBean>) msg.obj);
//解決開始無法向右滑動的問題
vpager.setCurrentItem(5000);
break;
case 1:
//無限輪播
int currentItem = vpager.getCurrentItem();
currentItem++;
vpager.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(1,1000);
}
}
}; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.afrag, null);
vpager = view.findViewById(R.id.vpager);
line = view.findViewById(R.id.line);
tv = view.findViewById(R.id.tv);
//請求資料
new Thread(new Runnable() {
@Override
public void run() {
String string = HttpUrlUtils.requestData("http://www.xieast.com/api/banner.php");
//解析
banner = new Gson().fromJson(string, Banner.class);
handler.sendMessage(handler.obtainMessage(0,banner.getData()));
}
}).start();
//介面卡
imageAdapter = new ImageAdapter(getActivity());
vpager.setAdapter(imageAdapter);
handler.sendEmptyMessageDelayed(1,1000);
//設定小圓點
settinground();
//viewapger的滑動監聽事件
vpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
//設定文字
int currentItem = vpager.getCurrentItem();
chageText(currentItem % banner.getData().size());
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
//觸控事件
vpager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
handler.removeMessages(1);
break;
case MotionEvent.ACTION_MOVE:
handler.removeMessages(1);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(1,1000);
break;
}
return false;
}
});
return view;
}
//設定文字 private void chageText(int position) { String title = banner.getData().get(position).getTitle(); tv.setText(title); //小圓點 for (int x =0;x<banner.getData().size();x++){ //獲取子控制元件 View child = line.getChildAt(x); child.setBackgroundResource(x==position?R.mipmap.dot_focus:R.mipmap.dot_normal);
}
}
//設定圓點 private void settinground() { View view = new View(getActivity()); //設定背景圖片 view.setBackgroundResource(R.mipmap.dot_normal); //設定寬高 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
layoutParams.leftMargin=10;
//設定給view
view.setLayoutParams(layoutParams);
line.addView(view);
} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 } //介面卡 package com.example.qmy.a1020.adapter;
import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView;
import com.example.qmy.a1020.model.Banner; import com.example.qmy.a1020.utils.ImageLoaderConfing; import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList; import java.util.List;
public class ImageAdapter extends PagerAdapter {
private final FragmentActivity activity; private final List<Banner.DataBean> imagelist;
public ImageAdapter(FragmentActivity activity) { this.activity = activity; imagelist = new ArrayList<>(); }
public void setDate(List<Banner.DataBean> obj) { this.imagelist.clear(); this.imagelist.addAll(obj); notifyDataSetChanged(); } @Override public int getCount() { if (imagelist.size()==0){ return 0; }else { return Integer.MAX_VALUE; } }
@Override public boolean isViewFromObject(@NonNull View view, @NonNull Object o) { return view==o; }
@NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { ImageView imageView = new ImageView(activity); ImageLoader.getInstance().displayImage(imagelist.get(position % imagelist.size()).getImg(), imageView,ImageLoaderConfing.getimageoption(activity)); container.addView(imageView); return imageView; }