package com.example.lenovo.tablayout;

/**
* Created by lenovo on 2018/7/18.
*/ 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.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast; import com.bawei.jane.mxlistview.view.XListView;
import com.google.gson.Gson; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* 1. 类的用途
* 2. @author forever
* 3. @date 2018/7/18 11:37
*/ public class Tab0Fragment extends Fragment {
@Nullable private List<String> picUrl;
private XListView xlv;
private int pageIndex=1;//第一页的数据
private String urlpart="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="; private String url =urlpart + pageIndex; private MyAdapter adapter;
private List<Result.DataBean> data = new ArrayList<>();
private int operType = 1;
private ViewPager viewpage; private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int index = viewpage.getCurrentItem();
index++;
viewpage.setCurrentItem(index);
sendEmptyMessageDelayed(0,2000);
}
}; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.tab0, null);
// picUrl = new ArrayList<>(); xlv = view.findViewById(R.id.xlv);
viewpage = view.findViewById(R.id.vp); if (NetUtils.isConn(getActivity())) {
initData();
MyTask myTask = new MyTask();
myTask.execute(url); Toast.makeText(getActivity(), "有网络", Toast.LENGTH_SHORT).show();
//设置适配器
viewpage.setAdapter(new MyPagerAdapter(picUrl, getActivity()));
//设置默认显示页面
viewpage.setCurrentItem(picUrl.size() * 10);
//发消息
handler.sendEmptyMessage(0); } else {
NetUtils.openDg(getActivity());
}
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() { pageIndex = 1;
url = urlpart + pageIndex;
//重新赋值
operType = 1;
MyTask myTask = new MyTask();
myTask.execute(url); } @Override
public void onLoadMore() {
// 页数增加
pageIndex++;
url = urlpart + pageIndex;
// 重新赋值
operType = 2;
Toast.makeText(getActivity(), "加载更多", Toast.LENGTH_SHORT).show();
MyTask task = new MyTask();
task.execute(url); } });
return view;
} private void initData() {
picUrl.add("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=353970577,2929946804&fm=200&gp=0.jpg");
picUrl.add("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3382711420,562977377&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2267393344,4258926851&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1211379323,1253125292&fm=27&gp=0.jpg");
picUrl.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1132229679,4204511222&fm=27&gp=0.jpg");
} class MyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(5000);
c.setConnectTimeout(5000);
if (c.getResponseCode()==200){
InputStream inputStream = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str=reader.readLine())!=null){
builder.append(str);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
} @Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class); if (operType==1){
data.clear();
}
data.addAll(result.getData());
setAdapter();
//关闭头尾布局
if (operType ==1){
xlv.stopRefresh();
Date date = new Date(System.currentTimeMillis());//获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间
String refreShTime = sf.format(date);
xlv.setRefreshTime(refreShTime);//设置刷新时间
}else {
xlv.stopLoadMore();//关闭尾布局 } }
}
public void setAdapter(){
if (adapter==null){
adapter = new MyAdapter(data,getActivity());
xlv.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:background="#ff0000 "
android:id="@+id/toutiao"
android:textColor="#ffffff"
android:gravity="center"
android:text="今日头条C"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="60dp" />
<android.support.design.widget.TabLayout
android:layout_below="@id/toutiao"
android:id="@+id/tab"
android:layout_width="match_parent"
app:tabTextColor="#00ff96"
app:tabSelectedTextColor="#ff0033"
app:tabBackground="@color/colorPrimaryDark"
android:layout_height="3dp"
android:layout_weight="1"></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager
android:id="@+id/vp" android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:background="#f00"
android:layout_gravity="start"
android:layout_width="200dp"
android:layout_height="match_parent"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
05-11 15:15