//++++++++==布局+++++++++++
<?xml version="1.0" encoding="utf-8"?><com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/listview1"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
//item_one
<+LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<+LinearLayout
android:layout_width=“match_parent”
android:orientation=“vertical”
android:layout_weight=“1”
android:layout_height=“150dp”>
<+TextView
android:id="@+id/t2_one"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“112”/>
<+/LinearLayout>
<+ImageView
android:id="@+id/image_one"
android:layout_width=“120dp”
android:layout_height=“120dp”
android:src="@drawable/ic_launcher_background"/>
<+/LinearLayout>
//item_two
<?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”
android:orientation=“vertical”>
<+TextView
android:id="@+id/name_two"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“211”/>
<+LinearLayout
android:layout_width=“match_parent”
android:layout_height=“150dp”
android:orientation=“horizontal”>
<+ImageView
android:id="@+id/image_two1"
android:layout_width=“120dp”
android:layout_height=“150dp”
android:src="@drawable/ic_launcher_background"
android:layout_weight=“1”/>
<+ImageView
android:id="@+id/image_two2"
android:layout_width=“120dp”
android:layout_height=“150dp”
android:src="@drawable/ic_launcher_background"
android:layout_weight=“1”/>
<+ImageView
android:id="@+id/image_two3"
android:layout_width=“120dp”
android:layout_height=“150dp”
android:src="@drawable/ic_launcher_background"
android:layout_weight=“1”/>
<+/LinearLayout>
<+/LinearLayout>
//+++++++++ThreeFragment+++++++
import android.annotation.SuppressLint;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.bwie.asus.mn1.Bean.JsonBean;
import com.bwie.asus.mn1.adapter.LAdapter;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import static
com.bwie.asus.mn1.Utils.NetWorkUtil.isNetWork;
public class ThreeFragment extends Fragment {
private int i=1;
private PullToRefreshListView listView1;
private String path = "http://www.xieast.com/api/news/news.php?page="+i;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag3, container, false);
listView1 = view.findViewById(R.id.listview1);
listView1.setMode (PullToRefreshListView.Mode.BOTH);
if (isNetWork(getActivity())){
initData(i);
listView1.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
initData(1);
handler.sendEmptyMessage(2);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
i++;
initData2(i);
}
});
}else{
Toast.makeText(getActivity(),"无网络",Toast.LENGTH_SHORT).show();
}
return view;
}
private void initData(final int i) {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(4000);
if (connection.getResponseCode()==connection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
String s = CharStreams.toString(streamReader);
Message message=new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
}
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
}
private void initData2(final int i) {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(4000);
if (connection.getResponseCode()==connection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
String s = CharStreams.toString(streamReader);
Message message=new Message();
message.what=3;
message.obj=s;
handler.sendMessage(message);
}
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
}
@SuppressLint("HandlerLeak")
Handler handler=new Handler(){
private ArrayList<JsonBean.Data> jsonList;
private LAdapter lAdapter;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
String s= (String) msg.obj;
Gson gson=new Gson();
JsonBean jsonBean= gson.fromJson(s, JsonBean.class);
jsonList=jsonBean.getData();
lAdapter=new LAdapter(getActivity(),jsonList);
listView1.setAdapter(lAdapter);
Toast.makeText(getActivity(),""+jsonBean,Toast.LENGTH_SHORT);
break;
case 2:
lAdapter.notifyDataSetChanged();
listView1.onRefreshComplete();
break;
case 3:
String s2= (String) msg.obj;
Gson gson2=new Gson();
JsonBean jsonBean2= gson2.fromJson(s2, JsonBean.class);
ArrayList<JsonBean.Data> jsonList2 =jsonBean2.getData();
jsonList.addAll(jsonList2);
//刷新适配器
lAdapter.notifyDataSetChanged();
//停止刷新
listView1.onRefreshComplete();
Toast.makeText(getActivity(),""+jsonBean2,Toast.LENGTH_SHORT);
break;
}
}
};
}
//++++++++++++NetWorkUtil+++++++
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetWorkUtil {
public static boolean isNetWork(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI){
return true;
}
return false;
}
}
//+++++++++LAdapter++++++
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.asus.mn1.Bean.JsonBean;
import com.bwie.asus.mn1.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class LAdapter extends BaseAdapter {
private Context context;
private ArrayList<JsonBean.Data> jsonList;
public LAdapter(Context context, ArrayList<JsonBean.Data> jsonList) {
this.context = context;
this.jsonList = jsonList;
}
@Override
public int getCount() {
return jsonList.size();
}
@Override
public Object getItem(int position) {
return jsonList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
int num = 0;
String pic = jsonList.get(position).getThumbnail_pic_s02();
if (pic != null) {
num = 1;
} else {
num = 0;
}
return num;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1=null;
ViewHolder2 holder2=null;
int type = getItemViewType(position);
if (convertView==null){
switch (type){
case 0:
holder1=new ViewHolder1();
convertView = View.inflate(context, R.layout.dtm_one, null);
holder1.name_one=convertView.findViewById(R.id.name_one);
holder1.text_one=convertView.findViewById(R.id.t2_one);
holder1.iamge_one=convertView.findViewById(R.id.image_one);
convertView.setTag(holder1);
break;
case 1:
holder2=new ViewHolder2();
convertView = View.inflate(context, R.layout.dtm_two, null);
holder2.name_two=convertView.findViewById(R.id.name_two);
holder2.image_two1=convertView.findViewById(R.id.image_two1);
holder2.image_two2=convertView.findViewById(R.id.image_two2);
holder2.image_two3=convertView.findViewById(R.id.image_two3);
convertView.setTag(holder2);
break;
}
}else {
switch (type) {
case 0:
holder1 = (ViewHolder1) convertView.getTag();
break;
case 1:
holder2 = (ViewHolder2) convertView.getTag();
break;
}
}
//设置内容
switch (type){
case 0:
holder1.text_one.setText(jsonList.get(position).getTitle());
holder1.name_one.setText(jsonList.get(position).getAuthor_name());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(jsonList.get(position).getThumbnail_pic_s(),holder1.iamge_one,options);
break;
case 1:
holder2.name_two.setText(jsonList.get(position).getTitle());
Log.i("aaa",jsonList.get(position).getTitle());
DisplayImageOptions options2 = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(jsonList.get(position).getThumbnail_pic_s(),holder2.image_two1,options2);
ImageLoader.getInstance().displayImage(jsonList.get(position).getThumbnail_pic_s02(),holder2.image_two2,options2);
ImageLoader.getInstance().displayImage(jsonList.get(position).getThumbnail_pic_s03(),holder2.image_two3,options2);
break;
}
return convertView;
}
class ViewHolder1{
TextView name_one,text_one;
ImageView iamge_one;
}
class ViewHolder2{
TextView name_two;
ImageView image_two1,image_two2,image_two3;
}
}