主函数
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.bean.JsonBean;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* 作者:王钦
* 日期:2017/10/26
* 时间:15:44
* 用途:主界面
*/
public class MainActivity extends AppCompatActivity {
//创建一个Handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
//得到数据
JsonBean bean = (JsonBean) msg.obj;
list = bean.getData();
//Log.i("aaa", list.toString());
//创建适配器
myAdapter = new MyAdapter();
//给ListView设置适配器
list_view.setAdapter(myAdapter);
}
}
};
//全局变量
private PullToRefreshLayout pull;
private ListView list_view;
private String path;
private int shu = 0;
private List<JsonBean.DataBean> list;
private MyAdapter myAdapter;
private DrawerLayout mDrawerLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//判断网络
if (isNetworkAvailable(MainActivity.this)) {
Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show();
}
//初始化控件
pull = (PullToRefreshLayout) findViewById(R.id.pull);
list_view = (ListView) findViewById(R.id.list_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
//DrawerLayout的监听事件
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
/**
* 当抽屉滑动状态改变的时候被调用
* 状态值是STATE_IDLE(闲置--0), STATE_DRAGGING(拖拽的--1), STATE_SETTLING(固定--2)中之一。
* 抽屉打开的时候,点击抽屉,drawer的状态就会变成STATE_DRAGGING,然后变成STATE_IDLE
*/
@Override
public void onDrawerStateChanged(int arg0) {
//Log.i("drawer", "drawer的状态:" + arg0);
}
/**
* 当抽屉被滑动的时候调用此方法
* arg1 表示 滑动的幅度(0-1)
*/
@Override
public void onDrawerSlide(View arg0, float arg1) {
//Log.i("drawer", arg1 + "");
}
/**
* 当一个抽屉被完全打开的时候被调用
*/
@Override
public void onDrawerOpened(View arg0) {
Log.i("drawer", "抽屉被完全打开了!");
}
/**
* 当一个抽屉完全关闭的时候调用此方法
*/
@Override
public void onDrawerClosed(View arg0) {
Log.i("drawer", "抽屉被完全关闭了!");
}
});
//请求网络
getData();
//pulltorefresh的监听事件
pull.setRefreshListener(new BaseRefreshListener() {
//下拉刷新
@Override
public void refresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//得到接口
path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
//请求网络
getData();
//刷新适配器
myAdapter.notifyDataSetChanged();
//停止下拉刷新
pull.finishRefresh();
}
//设置下拉刷新的时间
}, 3000);
}
//上拉加载
@Override
public void loadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
shu++;
//得到接口
path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
//请求网络
getData();
//刷新适配器
myAdapter.notifyDataSetChanged();
//停止上拉加载
pull.finishLoadMore();
}
//设置上拉加载的时间
}, 3000);
}
});
}
//适配器
class MyAdapter extends BaseAdapter {
private final int lin = 0;
private final int one = 1;
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
int type = getItemViewType(i);
ViewHolderOne holderOne;
ViewHolderTwo holderTwo;
//多条目展示
switch (type) {
case lin:
if (view == null) {
//初始化视图
view = View.inflate(MainActivity.this, R.layout.item, null);
//重写ViewHolderOne
holderOne = new ViewHolderOne();
//初始化控件
holderOne.img = view.findViewById(R.id.img);
holderOne.text = view.findViewById(R.id.text);
//绑定
view.setTag(holderOne);
}
break;
case one:
if (view == null) {
//初始化视图
view = View.inflate(MainActivity.this, R.layout.item1, null);
//重写ViewHolderTwo
holderTwo = new ViewHolderTwo();
//初始化控件
holderTwo.text_01 = view.findViewById(R.id.text_1);
//绑定
view.setTag(holderTwo);
}
break;
default:
break;
}
switch (type) {
case lin:
holderOne = (ViewHolderOne) view.getTag();
//赋值
holderOne.text.setText(list.get(i).getTITLE());
ImageLoader.getInstance().displayImage((String) list.get(i).getIMAGEURL(), holderOne.img);
break;
case one:
holderTwo = (ViewHolderTwo) view.getTag();
//赋值
holderTwo.text_01.setText(list.get(i).getTITLE());
break;
default:
break;
}
//返回视图
return view;
}
//得到视图的类型
@Override
public int getItemViewType(int position) {
int i = position % 2;
if (i == 0) {
return 0;
} else {
return 1;
}
}
//得到视图类型的数量
@Override
public int getViewTypeCount() {
return 2;
}
class ViewHolderOne {
ImageView img;
TextView text;
}
class ViewHolderTwo {
TextView text_01;
}
}
//请求网络
public void getData() {
new Thread() {
@Override
public void run() {
//判断接口地址
if (path == null) {
path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
}
try {
//获取接口地址
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置提交方式
connection.setRequestMethod("GET");
//设置传递数据的超时时间
connection.setReadTimeout(5000);
//设置建立连接的超时时间
connection.setConnectTimeout(5000);
//判断请求码
if (connection.getResponseCode() == 200) {
InputStream stream = connection.getInputStream();
//转码
String json = zhuan(stream, "utf-8");
//Log.i("aaa", json);
//解析
Gson gson = new Gson();
JsonBean fromJson = gson.fromJson(json, JsonBean.class);
Message message = Message.obtain();
message.what = 0;
message.obj = fromJson;
//发送消息
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}.start();
}
//转码
private String zhuan(InputStream stream, String s) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(stream, s);
BufferedReader reader = new BufferedReader(inputStreamReader);
String ss = null;
StringBuilder builder = new StringBuilder();
while ((ss = reader.readLine()) != null) {
builder.append(ss);
}
inputStreamReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//检查当前网络是否可用
public boolean isNetworkAvailable(Activity activity) {
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
System.out.println(i + "===状态===" + networkInfo[i].getState());
System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
// 判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
主页面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#fff">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="立即登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/linear"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="我的消息" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="教学视频" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="我的成绩" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="学车日记" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="文章收藏" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="文章足迹" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="教员中心" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="设置" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
android:id="@+id/pull"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:layout_width="70dp" android:layout_height="70dp" android:padding="10dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="aaaaaaaaaa" /> </LinearLayout>
item1<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="aaaaa" /> </LinearLayout>