1.Fragment的复用
// 创建一个存放标题的集合
titleList = new ArrayList
();
// 创建一个存放fragment的及集合
pageList = new ArrayList
();
for (int i = 0; i < title.length; i++) {
titleList.add(title[i]);
// //将url传进fragment
pageList.add(PageFragment.getInstance(Url.getUrl().get(i)));
}
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 网路工具
*
* @author 大金
*
*/
public class MyHttpUtils {
// 判断是否有网
public static boolean hasNet(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo!=null){
return networkInfo.isAvailable();
}
return false;
}
//获取网络类型
public static int netType(Context context){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo.getSubtype();
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.jinri20161108.MyWebView;
import com.bawei.jinri20161108.R;
import com.bawei.jinri20161108_adapter.MyXlistAdapter;
import com.bawei.jinri20161108_bean.Data;
import com.bawei.jinri20161108_bean.News;
import com.bawei.jinri20161108_myhelper.MyHelper;
import com.bawei.jinri20161108_util.JsonUtils;
import com.bawei.jinri20161108_util.MyHttpUtils;
import com.bawei.jinri20161108_views.XListView;
import com.bawei.jinri20161108_views.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class PageFragment extends Fragment implements IXListViewListener {
private String tag = "PageFragment--》";
private XListView xlist;
private MyXlistAdapter adapter;
private ArrayList
arrayList;
private final int ERROR = 0;
private final int ACCESS = 1;
private final int REFESH = 2;
private final int REFESH2 = 3;
private ArrayList
datas;
ArrayList datas_tmp = new ArrayList();
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int num1 = msg.what;
datas = (ArrayList) msg.obj; switch (num1) { case ERROR: Toast.makeText(getActivity(), "请检查网络???/??", Toast.LENGTH_SHORT).show(); break; case ACCESS: // 设置适配器 datas_tmp.addAll(datas); if (datas.size() > 0) { adapter = new MyXlistAdapter(getActivity(), datas_tmp); xlist.setAdapter(adapter); } break; case REFESH: // ArrayList datas1 = (ArrayList) msg.obj; // 将每次查到的数据存放到空集合的第一个位置(第0个位置,是置顶,不可替换) // 先判断一下获得数据是不是置顶的信息内容,是的话,从第二条展示,否的话,从第一条添加。 // 设置更新时间。 long millis = System.currentTimeMillis(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String time = dateFormat.format(millis); xlist.setRefreshTime(time); // 更新数据内容 datas_tmp.addAll(0, datas); // 更新数据库 adapter.notifyDataSetChanged(); // 停止刷新 onload(); break; case REFESH2: datas_tmp.addAll(datas); // 更新数据库 adapter.notifyDataSetChanged(); // 停止刷新 onload(); break; } } }; private String string; private String json1; private TextView zuobiao; // fragment的复用 public static PageFragment getInstance(String string) { PageFragment pageFragment = null; if (pageFragment == null) { pageFragment = new PageFragment(); Bundle bundle = new Bundle(); bundle.putString("url", string); pageFragment.setArguments(bundle); } return pageFragment; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 获取展示界面的url string = getArguments().getString("url"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.pagefragment, null); xlist = (XListView) view.findViewById(R.id.xListView); // zuobiao = (TextView) view.findViewById(R.id.page_zuobiao); // 刷新时添加数据的集合 arrayList = new ArrayList
(); // 打开开关 xlist.setPullLoadEnable(true); // 设置xlistView的监听 xlist.setXListViewListener(this); // 判断网络,获取数据 initDataFromNet(string); // xlistView的item点击事件 xlist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView
arg0, View arg1, int arg2, long arg3) { // 跳转到webview,显示具体信息 Intent intent = new Intent(getActivity(), MyWebView.class); String share_url = datas_tmp.get(arg2 - 1).getShare_url(); intent.putExtra("url", share_url); startActivity(intent); } }); return view; } // 获取数据 private void initDataFromNet(String string) { // 判断是否有网 // 获取数据的时候,存储的到本地 // 没网的时候,加载本地数据 boolean hasNet = MyHttpUtils.hasNet(getActivity()); if (hasNet) { // 有网请求网络 getDataFromNetTo(string, ACCESS); } else { // 创建数据库 MyHelper myHelper = new MyHelper(getActivity()); SQLiteDatabase database = myHelper.getReadableDatabase(); // 没有网读取缓存 Cursor cursor = database.rawQuery("select * from news", null); while (cursor.moveToNext()) { json1 = cursor.getString(cursor.getColumnIndex("json")); } // 关闭数据库 database.close(); // 解析json parseJon(json1); Toast.makeText(getActivity(), "链接网络失败", Toast.LENGTH_SHORT).show(); } } // 请求网络(传进去形参) private void getDataFromNetTo(final String url1, final int ACCESS) { // 用Xutils请求网络 HttpUtils httpUtils = new HttpUtils(); // 清除网络缓存。设置缓存时间为0 httpUtils.configCurrentHttpCacheExpiry(0); // 设置缓存大小,为0 httpUtils.configHttpCacheSize(0); //httpuils请求网络 httpUtils.send(HttpMethod.GET, url1, new RequestCallBack
() { @Override//请求失败 public void onFailure(HttpException arg0, String arg1) { handler.sendEmptyMessage(ERROR); } // 请求成功的时候 @Override public void onSuccess(ResponseInfo
arg0) { // 从网络上获取json串 String json = arg0.result; // 获取json串时,首先保存到数据库 //存数据之前先清空数据库 //getActivity().deleteDatabase("news"); // 创建数据库 MyHelper myHelper = new MyHelper(getActivity()); SQLiteDatabase database = myHelper.getReadableDatabase(); //存储数据 ContentValues values = new ContentValues(); values.put("json", json); database.insert("news", null, values); // 关闭数据库 database.close(); // 解析json parseJon(json); } }); }; public void parseJon(String str) { News news = JsonUtils.parseJson(str); ArrayList
data = news.getData(); Message msg = new Message(); msg.what = ACCESS; msg.obj = data; // 将数据发送到主线程,更新UI handler.sendMessage(msg); }; @Override // 下拉刷新 public void onRefresh() { // 当下啦的时候,重新获取数据。将获取的新数据,发送给主线程 // REFESH--》意思是下拉时的数据类型 getDataFromNetTo(string, REFESH); // 刷新需要请求网络 // new Thread() { // public void run() { // try { // Thread.sleep(2000); // arrayList.add(0, "上啦加载的数据"); // // 发送消息更新适配器 // handler.sendEmptyMessage(REFESH); // } catch (Exception e) { // // e.printStackTrace(); // } // }; // }.start(); } // 停止刷新 public void onload() { xlist.stopRefresh(); xlist.stopLoadMore(); } @Override // 上啦加载 public void onLoadMore() { getDataFromNetTo(string, REFESH2); // 刷新需要请求网络 // new Thread() { // public void run() { // try { // Thread.sleep(2000); // arrayList.add("下啦加载的数据"); // // 发送消息更新适配器 // handler.sendEmptyMessage(REFESH); // } catch (Exception e) { // // e.printStackTrace(); // } // // }; // }.start(); } }
import android.app.ActionBar.LayoutParams;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.bawei.jinri20161108.R;
import com.bawei.jinri20161108_bean.Data;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MyXlistAdapter extends BaseAdapter implements OnClickListener {
private final int NO_IMAGER = 0;
private final int IMAGER_ONE = 1;
private final int IMAGER_LIST = 2;
private final int IMAGER_LAGER = 3;
private final FragmentActivity activity;
private final ArrayList datas;
// 四个ViewHolder
ViewHolder0 viewHolder0 = null;
ViewHolder1 viewHolder1 = null;;
ViewHolder2 viewHolder2 = null;;
ViewHolder3 viewHolder3 = null;;
public MyXlistAdapter(FragmentActivity activity, ArrayList datas) {
this.activity = activity;
this.datas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
// 优化处理1.条目复用convertView 2.控件复用 viewholder
public View getView(int position, View convertView, ViewGroup parent) {
// 取出数据
Data data = datas.get(position);
// 先获取类别,通过类别,分别加载不同的条目
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case 0:
// 将类new出来
viewHolder0 = new ViewHolder0();
convertView = View.inflate(activity, R.layout.image_text, null);
viewHolder0.title = (TextView) convertView
.findViewById(R.id.image_text_title);
viewHolder0.source = (TextView) convertView
.findViewById(R.id.image_text_zhuan);
viewHolder0.label = (TextView) convertView
.findViewById(R.id.image_text_zhi);
viewHolder0.comment_count = (TextView) convertView
.findViewById(R.id.image_text_ping);
viewHolder0.publish_time = (TextView) convertView
.findViewById(R.id.image_text_time);
viewHolder0.im = (ImageView) convertView
.findViewById(R.id.image_text_im);
convertView.setTag(viewHolder0);
break;
case 1:// 一张图片
// 将类new出来
viewHolder1 = new ViewHolder1();
convertView = View.inflate(activity, R.layout.image_one, null);
viewHolder1.title = (TextView) convertView
.findViewById(R.id.image_one_title);
viewHolder1.source = (TextView) convertView
.findViewById(R.id.image_one_zhuan);
viewHolder1.label = (TextView) convertView
.findViewById(R.id.image_one_zhi);
viewHolder1.comment_count = (TextView) convertView
.findViewById(R.id.image_one_ping);
viewHolder1.publish_time = (TextView) convertView
.findViewById(R.id.image_one_time);
viewHolder1.im = (ImageView) convertView
.findViewById(R.id.image_one_im);
viewHolder1.image = (ImageView) convertView
.findViewById(R.id.image_one_im2);
convertView.setTag(viewHolder1);
break;
case 2:
// 将类new出来
viewHolder2 = new ViewHolder2();
convertView = View.inflate(activity, R.layout.image_list, null);
viewHolder2.title = (TextView) convertView
.findViewById(R.id.image_list_title);
viewHolder2.source = (TextView) convertView
.findViewById(R.id.image_list_zhuan);
viewHolder2.label = (TextView) convertView
.findViewById(R.id.image_list_zhi);
viewHolder2.comment_count = (TextView) convertView
.findViewById(R.id.image_list_ping);
viewHolder2.publish_time = (TextView) convertView
.findViewById(R.id.image_list_time);
viewHolder2.im = (ImageView) convertView
.findViewById(R.id.image_list_im);
viewHolder2.middle_image1 = (ImageView) convertView
.findViewById(R.id.image_list_im1);
viewHolder2.middle_image2 = (ImageView) convertView
.findViewById(R.id.image_list_im2);
viewHolder2.middle_image3 = (ImageView) convertView
.findViewById(R.id.image_list_im3);
convertView.setTag(viewHolder2);
break;
case 3:
// 将类new出来
viewHolder3 = new ViewHolder3();
convertView = View
.inflate(activity, R.layout.image_large, null);
viewHolder3.title = (TextView) convertView
.findViewById(R.id.image_large_title);
viewHolder3.source = (TextView) convertView
.findViewById(R.id.image_large_zhuan);
viewHolder3.label = (TextView) convertView
.findViewById(R.id.image_large_zhi);
viewHolder3.comment_count = (TextView) convertView
.findViewById(R.id.image_large_ping);
viewHolder3.publish_time = (TextView) convertView
.findViewById(R.id.image_large_time);
viewHolder3.im = (ImageView) convertView
.findViewById(R.id.image_large_im);
viewHolder3.large = (ImageView) convertView
.findViewById(R.id.image_large_im2);
convertView.setTag(viewHolder3);
break;
}// 复用时调用
} else {
switch (type) {
case 0:
viewHolder0 = (ViewHolder0) convertView.getTag();// 得到布局中的控件
break;
case 1:
viewHolder1 = (ViewHolder1) convertView.getTag();
break;
case 2:
viewHolder2 = (ViewHolder2) convertView.getTag();
break;
case 3:
viewHolder3 = (ViewHolder3) convertView.getTag();
break;
}
}
switch (type) {// 赋值
case 0:
// 赋值
convertView.setTag(viewHolder0);
viewHolder0.title.setText(data.getTitle());
viewHolder0.source.setText(" " + data.getSource());
viewHolder0.label.setText(" " + data.getLabel());
viewHolder0.comment_count
.setText(" 评论数:" + data.getComment_count());
// 转换时间
long publish_time = data.getPublish_time();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String format = dateFormat.format(new Date(publish_time));
viewHolder0.publish_time.setText(" " + format);
break;
case 1:
// 赋值
viewHolder1.title.setText(data.getTitle());
viewHolder1.source.setText(" " + data.getSource());
viewHolder1.label.setText(" " + data.getLabel());
viewHolder1.comment_count
.setText(" 评论数:" + data.getComment_count());
// 转换时间
long publish_time2 = data.getPublish_time();
SimpleDateFormat dateFormat2 = new SimpleDateFormat("HH:mm:ss");
String time_one = dateFormat2.format(publish_time2);
viewHolder1.publish_time.setText(" " + time_one);
// 设置图片
ImageLoaderConfiguration createDefault = ImageLoaderConfiguration
.createDefault(activity);
ImageLoader.getInstance().init(createDefault);
ImageLoader.getInstance().displayImage(
data.getMiddle_image().getUrl(), viewHolder1.image);
break;
case 2:
// // 赋值
viewHolder2.title.setText(data.getTitle());
viewHolder2.source.setText(" " + data.getSource());
viewHolder2.label.setText(data.getLabel());
viewHolder2.comment_count
.setText(" 评论数:" + data.getComment_count());
// 转换时间
long publish_time3 = data.getPublish_time();
SimpleDateFormat dateFormat3 = new SimpleDateFormat("HH:mm:ss");
String time_one3 = dateFormat3.format(publish_time3);
viewHolder2.publish_time.setText(" " + time_one3);
// 设置图片
ImageLoaderConfiguration createDefault3 = ImageLoaderConfiguration
.createDefault(activity);
ImageLoader.getInstance().init(createDefault3);
ImageLoader.getInstance().displayImage(
data.getImage_list().get(0).getUrl(),
viewHolder2.middle_image1);
ImageLoader.getInstance().displayImage(
data.getImage_list().get(1).getUrl(),
viewHolder2.middle_image2);
ImageLoader.getInstance().displayImage(
data.getImage_list().get(2).getUrl(),
viewHolder2.middle_image3);
break;
case 3:
// 赋值
viewHolder3.title.setText(data.getTitle());
viewHolder3.source.setText(" " + data.getSource());
viewHolder3.label.setText(" " + data.getLabel());
viewHolder3.comment_count
.setText(" 评论数:" + data.getComment_count());
// 转换时间
long publish_time4 = data.getPublish_time();
SimpleDateFormat dateFormat4 = new SimpleDateFormat("HH:mm:ss");
String time_one4 = dateFormat4.format(publish_time4);
viewHolder3.publish_time.setText(" " + time_one4);
// 设置图片
ImageLoaderConfiguration createDefault4 = ImageLoaderConfiguration
.createDefault(activity);
ImageLoader.getInstance().init(createDefault4);
ImageLoader.getInstance().displayImage(
data.getLarge_image_list().get(0).getUrl(),
viewHolder3.large);
break;
}
return convertView;
}
// 传进id
public void jump() {
// 加载布局’
View view = View.inflate(activity, R.layout.popupwindow, null);
// 创建出来popupwindow
PopupWindow window = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
window.setOutsideTouchable(true);
window.setFocusable(true);
window.setBackgroundDrawable(new BitmapDrawable());
window.showAtLocation(view, Gravity.CENTER, 0, 0);
}
// 需手动添加这两个方法
@Override
public int getItemViewType(int position) {
// 取出数据
Data data = datas.get(position);
if (data.getLarge_image_list() != null
&& data.getLarge_image_list().size() > 0) {
return IMAGER_LAGER;
} else if (data.getImage_list() != null
&& data.getImage_list().size() > 0) {
return IMAGER_LIST;
} else if (!TextUtils.isEmpty(data.getMiddle_image().getUrl())) {
return IMAGER_ONE;
} else {
return NO_IMAGER;
}
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 4;
}
// 定义ViewHolder类
// 无图
class ViewHolder0 {
public TextView title;
public TextView label;
public TextView source;
public TextView comment_count;
public TextView publish_time;
public ImageView im;
} // 一张图
class ViewHolder1 {
public TextView title;
public TextView label;
public TextView source;
public TextView comment_count;
public TextView publish_time;
public ImageView image;
public ImageView im;
} // 三张图
class ViewHolder2 {
public TextView title;
public TextView label;
public TextView source;
public TextView comment_count;
public TextView publish_time;
public ImageView middle_image1;
public ImageView middle_image2;
public ImageView middle_image3;
public ImageView im;
} // 一张大图
class ViewHolder3 {
public TextView title;
public TextView label;
public TextView source;
public TextView comment_count;
public TextView publish_time;
public ImageView large;
public ImageView im;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case 0:
viewHolder0.im.setOnClickListener(this);
jump();
break;
case 1:
viewHolder1.im.setOnClickListener(this);
jump();
break;
}
}
}