//网络请求数据工具类
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();
}
}
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);
}
}
//适配器
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;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}