好库网
package com.vanco.abplayer.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vanco.abplayer.R;
import com.vanco.abplayer.VideoInfoActivity;
import com.vanco.abplayer.adapter.VideoListAdapter;
import com.vanco.abplayer.model.Page;
import com.vanco.abplayer.model.VideoItem;
import com.vanco.abplayer.util.Constants;
import com.vanco.abplayer.util.HttpUtil;
import com.vanco.abplayer.util.URLUtil;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

/**
 * Fragment椤甸潰
 * 
 * @author wwj_748
 * @date 2014/8/9
 */
public class DonghuaFragment extends Fragment implements IXListViewRefreshListener,
		IXListViewLoadMore {
	private XListView videoListView;// 瑙嗛鍒楄〃
	private View noBlogView; // 鏃犳暟鎹椂鏄剧ず
	private VideoListAdapter adapter;// 鍒楄〃閫傞厤鍣?	private List<VideoItem> templist;

	private boolean isLoad = false; // 鏄惁鍔犺浇
	private int videoType = 1; // 瑙嗛绫诲埆
	private Page page; // 椤甸潰寮曠敤

	private String refreshDate = ""; // 鍒锋柊鏃ユ湡

	public DonghuaFragment(int videoType) {
		this.videoType = videoType;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		templist = new ArrayList<VideoItem>();
		VideoItem tempItem = new VideoItem();
		tempItem.setAid("7");
		tempItem.setTitle("[绀轰緥鏁版嵁]绔ュ勾鍔ㄧ敾涓婚鏇?);
		tempItem.setPic("链接已屏蔽;);
		tempItem.setAuthor("鏍瑰彿鈶?);
		tempItem.setPlay("23333");
		templist.add(tempItem);
		initComponent();
		if (isLoad == false) {
			isLoad = true;
			// 鍔犺浇鏁版嵁搴撲腑鐨勬暟鎹?			List<VideoItem> list = templist;
			adapter.setList(list);
			adapter.notifyDataSetChanged();

			videoListView.startRefresh(); // 寮€濮嬪埛鏂?
		} else {
			videoListView.NotRefreshAtBegin(); // 涓嶅紑濮嬪埛鏂?		}
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.e("NewsFrag", "onCreateView");
		return inflater.inflate(R.layout.fragment_donghua, null);
	}

	// 鍒濆鍖?	private void init() {
		
		adapter = new VideoListAdapter(getActivity());
		page = new Page();
		page.setPageStart();
	}

	// 鍒濆鍖栫粍浠?	private void initComponent() {
		videoListView = (XListView) getView().findViewById(R.id.videoListView);
		videoListView.setAdapter(adapter);// 璁剧疆閫傞厤鍣?		videoListView.setPullRefreshEnable(this);// 璁剧疆鍙笅鎷夊埛鏂?		videoListView.setPullLoadEnable(this);// 璁剧疆鍙笂鎷夊姞杞?		// 璁剧疆鍒楄〃椤圭偣鍑讳簨浠?		videoListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 鑾峰緱鍗氬鍒楄〃椤?				VideoItem item = (VideoItem) adapter.getItem(position - 1);
				Intent i = new Intent();
				Bundle bundle = new Bundle();
				bundle.putSerializable("videoItemdata", item);
				i.setClass(getActivity(), VideoInfoActivity.class);
				i.putExtras(bundle);
				startActivity(i);
				// 鍔ㄧ敾杩囨浮
				getActivity().overridePendingTransition(R.anim.push_left_in,
						R.anim.push_no);
			}
		});

		noBlogView = getView().findViewById(R.id.noBlogLayout);
	}

	private class MainTask extends AsyncTask<String, Void, Integer> {

		@Override
		protected Integer doInBackground(String... params) {
			List<VideoItem> list=new ArrayList<VideoItem>();
			try {
			JSONObject donghuajson = new JSONObject(HttpUtil.getHtmlString(params[0]));
			//鍔ㄧ敾鏁版嵁瑙f瀽
			JSONArray dougaarray=donghuajson.getJSONArray("list");
			for (int i=0;i<dougaarray.length();i++) {			
				VideoItem item = new VideoItem();		
				item.setAid(dougaarray.getJSONObject(i).getString("aid").toString());
				item.setTypeid(dougaarray.getJSONObject(i).getString("typeid").toString());
				item.setTitle(dougaarray.getJSONObject(i).getString("title").toString());
				item.setSbutitle(dougaarray.getJSONObject(i).optString("sbutitle").toString());
				item.setPlay(dougaarray.getJSONObject(i).getString("play").toString());
				item.setReview(dougaarray.getJSONObject(i).getString("review").toString());
				item.setVideo_review(dougaarray.getJSONObject(i).getString("video_review").toString());
				item.setFavorites(dougaarray.getJSONObject(i).getString("favorites").toString());
				item.setMid(dougaarray.getJSONObject(i).getString("mid").toString());
				item.setAuthor(dougaarray.getJSONObject(i).getString("author").toString());
				item.setDescription(dougaarray.getJSONObject(i).getString("description").toString());
				item.setCreate(dougaarray.getJSONObject(i).getString("create").toString());
				item.setPic(dougaarray.getJSONObject(i).getString("pic").toString());
				item.setCredit(dougaarray.getJSONObject(i).getString("credit").toString());
				item.setCoins(dougaarray.getJSONObject(i).getString("coins").toString());
				item.setDuration(dougaarray.getJSONObject(i).getString("duration").toString());	
				list.add(item);
				//Log.d("TAG--->", "--->"+item.getTitle());
			}
			
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return Constants.DEF_RESULT_CODE.ERROR;
			}
//			// 鑾峰彇缃戦〉json鏁版嵁
//			String temp = HttpUtil.httpGet(params[0]);
//			if (temp == null) {
//				return Constants.DEF_RESULT_CODE.ERROR;
//			}
//			// 瑙f瀽json鑾峰彇鍒楄〃
//			List<VideoItem> list = JsoupUtil.getBlogItemList(blogType, temp);
			
			if (list.size() == 0) {
				return Constants.DEF_RESULT_CODE.NO_DATA;
			}
			// 鍒锋柊鍔ㄤ綔
			if (params[1].equals("refresh")) {
				adapter.setList(list);
				return Constants.DEF_RESULT_CODE.REFRESH;
			} else {// 鍔犺浇鏇村
				adapter.addList(list);
				return Constants.DEF_RESULT_CODE.LOAD;
			}

		}

		@Override
		protected void onPostExecute(Integer result) {
			// 閫氱煡鍒楄〃鏁版嵁鏇存柊
			adapter.notifyDataSetChanged();
			switch (result) {
			case Constants.DEF_RESULT_CODE.ERROR: // 閿欒
				Toast.makeText(getActivity(), "缃戠粶淇″彿涓嶄匠", Toast.LENGTH_LONG).show();
				videoListView.stopRefresh(getDate());
				videoListView.stopLoadMore();
				break;
			case Constants.DEF_RESULT_CODE.NO_DATA: // 鏃犳暟鎹?				// Toast.makeText(getActivity(), "鏃犳洿澶氬姞杞藉唴瀹?, Toast.LENGTH_LONG)
				// .show();
				videoListView.stopLoadMore();
				// noBlogView.setVisibility(View.VISIBLE); // 鏄剧ず鏃犲崥瀹?				break;
			case Constants.DEF_RESULT_CODE.REFRESH: // 鍒锋柊
				videoListView.stopRefresh(getDate());
				if (adapter.getCount() == 0) {
					noBlogView.setVisibility(View.VISIBLE); // 鏄剧ず鏃犲崥瀹?				}
				break;
			case Constants.DEF_RESULT_CODE.LOAD:
				videoListView.stopLoadMore();
				page.addPage();
				if (adapter.getCount() == 0) {
					noBlogView.setVisibility(View.VISIBLE); // 鏄剧ず鏃犲崥瀹?				}
				break;
			default:
				break;
			}
			super.onPostExecute(result);
		}

	}

	// 鍔犺浇鏇村鏃惰皟鐢?	@Override
	public void onLoadMore() {
		System.out.println("loadmore");
		new MainTask()
				.execute(
						URLUtil.getVideoListURL(videoType),
						"load");
	}

	@Override
	public void onRefresh() {
		System.out.println("refresh");
		page.setPageStart();
		new MainTask().execute(URLUtil.getRefreshBlogListURL(videoType),
				"refresh");
	}

	public String getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM鏈坉d鏃?HH:mm",
				Locale.CHINA);
		return sdf.format(new java.util.Date());
	}

}