FragmentXlistview
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>
FragmentXlistview的更多相关文章
随机推荐
- Android NDK pthreads详细使用
这个pthread.h文件可以在NDK环境里创建子线程,并对线程能够做出互斥所.等待.销毁等控制. 写这个博客的原因是我要写如何使用FFmpeg播放视频,因为同时需要播放音频和视频所以需要开启线程,并 ...
- const修饰指针+volatile +restrict
const这块的难点 const修饰指针有以下的几种形式 ,不同的形式 它的意义不一样. 形式1: int a=23: const int *p=&a: a是int型,&a是int * ...
- mybatis Interceptor拦截器代码详解
mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- JavaScript 进阶
字符串方法 ① charAt() 方法可返回指定位置的字符 ② indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置 ③ split() 方法将字符串分割为字符串数组,并返回此数组 ...
- python 多进程多线程的对比
link:http://www.cnblogs.com/whatisfantasy/p/6440585.html mark一下,挺详细
- sql 约束Check中使用Case函数
CHECK 约束用于限制列中的值的范围 在Check中使用Case函数在很多情况下都是非常不错的解决方法.可能有很多人根本就不用Check,那么我建议你在看过下面的例子之后也尝试一下在SQL中使用Ch ...
- TreeSet VS HashSet VS LinkedHashSet; TreeMap VS HashMap VS LinkedHashMap
From online resources Set HashSet is much faster than TreeSet (constant-time versus log-time for mos ...
- 软工作业PSP与单元测试训练:java语言判断电子邮箱格式
任务说明(二选一): 一.实现模块判断传入的身份证号码的正确性: 二.实现模块判断传入的电子邮箱账号的正确性: 实现要求: 一.实现功能模块: 1. 判断邮箱地址是否为空: 2. 判断邮箱地址是否 ...
- erlang中http请求
1..app文件中 {application,backend,[ {description,"ebank backend"}, {vsn,1.0}, {modules,[]} ...
- mongo-2ds索引对超过半球范围的适用性测试
以下测试均基于mongo v4.0 win10 一.GeoJSON GeoJSON是一种基于json的经纬度描述数据格式.在这里主要服务于2dsphere索引查询. 基本格式 <type:&q ...