package com.mugua.mgvideo.ui.category.fragment;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.handmark.pulltorefresh.library.PullToRefreshBase;
|
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
|
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
|
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
|
import com.lcjian.library.RetainViewFragment;
|
import com.lcjian.library.util.cache.DiskLruCache;
|
import com.lcjian.library.util.common.StorageUtils;
|
import com.mugua.mgvideo.R;
|
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
|
import com.umeng.analytics.MobclickAgent;
|
import com.mugua.mgvideo.MGVideoAPI;
|
import com.mugua.mgvideo.ui.category.ChannelAdapter;
|
import com.mugua.mgvideo.ui.category.ChannelAdapterHot;
|
import com.yeshi.base.entity.video.VideoType;
|
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.os.Bundle;
|
import android.view.View;
|
import android.widget.GridView;
|
import android.widget.ScrollView;
|
import android.widget.TextView;
|
|
/**
|
* 频道碎片
|
*/
|
public class ChannelFragment extends RetainViewFragment {
|
private PullToRefreshScrollView channelScrollView;
|
private GridView gv_channel, gv_channel_hot;
|
private TextView tv_hotsort;
|
private DiskLruCache cache;
|
// 判断请求是否成功,默认成功,避免二次执行请求
|
private boolean isSucceed = true;
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
try {
|
cache = DiskLruCache.open(
|
new File(StorageUtils.getCacheDirectory(getActivity())
|
.toString(), "http"), getVersionNum(getActivity()),
|
1, 1024 * 1024);
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/** 获取频道页数据 */
|
private void getCategories() {
|
if (getActivity() == null)
|
return;
|
SharedPreferences preferences = getActivity().getSharedPreferences(
|
"user", Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
MGVideoAPI.getClass(getActivity(), uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
getdatas(jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString());
|
// cache.remove(getKey("getCategories"));
|
DiskLruCache.Editor editor = cache
|
.edit(getKey("getCategories"));
|
|
editor.set(0, jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString());
|
editor.commit();
|
isSucceed = true;
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
byte[] responseBytes, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseBytes,
|
throwable);
|
isSucceed = false;
|
}
|
|
@Override
|
public void onFinish() {
|
channelScrollView.onRefreshComplete();
|
}
|
});
|
}
|
|
/**
|
* 通过json String获取数据
|
*
|
* @param jsonObject
|
* jsonObject.getJSONObject("Data").getJSONArray("data").toString
|
* ()
|
* @throws Exception
|
*/
|
protected void getdatas(String jsonObject) throws Exception {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE).create();
|
List<VideoType> categories = gson.fromJson(jsonObject,
|
new TypeToken<List<VideoType>>() {
|
}.getType());
|
List<VideoType> mainTypes = new ArrayList<VideoType>();
|
List<VideoType> hotTypes = new ArrayList<VideoType>();
|
for (VideoType vt : categories) {
|
if ("1".equalsIgnoreCase(vt.getMain())) {// 主分类
|
mainTypes.add(vt);
|
} else {// 热门分类
|
hotTypes.add(vt);
|
}
|
}
|
if (hotTypes.size() > 0) {
|
gv_channel_hot.setVisibility(View.VISIBLE);
|
tv_hotsort.setVisibility(View.INVISIBLE);
|
} else {
|
gv_channel_hot.setVisibility(View.GONE);
|
}
|
gv_channel.setAdapter(new ChannelAdapter(mainTypes, getActivity()));
|
gv_channel_hot
|
.setAdapter(new ChannelAdapterHot(hotTypes, getActivity()));
|
// 显示热门分类
|
if (tv_hotsort.getVisibility() == View.INVISIBLE) {
|
tv_hotsort.setVisibility(View.VISIBLE);
|
}
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
private String getKey(String method) {
|
return new Md5FileNameGenerator().generate(method);
|
}
|
|
/** 获取版本号 */
|
public static int getVersionNum(Context context) {
|
try {
|
PackageInfo pi = context.getPackageManager().getPackageInfo(
|
context.getPackageName(), 0);
|
return pi.versionCode;
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
return 1;
|
}
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.category_channel;
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
channelScrollView = (PullToRefreshScrollView) contentView
|
.findViewById(R.id.channel_ptrsv);
|
channelScrollView.setMode(Mode.PULL_FROM_START);
|
channelScrollView
|
.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
|
@Override
|
public void onRefresh(
|
PullToRefreshBase<ScrollView> refreshView) {
|
// TODO 频道下拉刷新逻辑
|
getCategories();
|
}
|
});
|
tv_hotsort = (TextView) contentView
|
.findViewById(R.id.category_channel_tv_hot);
|
gv_channel = (GridView) contentView
|
.findViewById(R.id.category_channel_gv_sort);
|
gv_channel_hot = (GridView) contentView
|
.findViewById(R.id.category_channel_gv_hotsort);
|
if (cache != null) {
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getCategories"));
|
if (snapshot != null) {
|
getdatas(snapshot.getString(0));
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (snapshot != null) {
|
snapshot.close();
|
}
|
}
|
}
|
getCategories();
|
}
|
|
/** 刷新分类页 */
|
public void refreshCategories() {
|
if (!isSucceed) {// 之前请求失败时才执行
|
getCategories();
|
}
|
}
|
}
|