package com.haicaojie.android.ui.main;
|
|
import android.app.Activity;
|
import android.app.ProgressDialog;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.provider.Settings;
|
import android.support.annotation.Nullable;
|
import android.support.v4.app.Fragment;
|
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentPagerAdapter;
|
import android.support.v4.view.ViewPager;
|
import android.view.View;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.haicaojie.android.BasicTextHttpResponseHandler;
|
import com.haicaojie.android.BuXinConstant;
|
import com.haicaojie.android.R;
|
import com.haicaojie.android.ShoppingApi;
|
import com.haicaojie.android.entity.FirstCategory;
|
import com.haicaojie.android.entity.HomeBanner;
|
import com.haicaojie.android.entity.RecommendGoods;
|
import com.haicaojie.android.entity.SpecialOffer;
|
import com.haicaojie.android.entity.SpecialOffer2;
|
import com.haicaojie.android.entity.TaoBaoGoodsBrief;
|
import com.haicaojie.android.ui.*;
|
import com.haicaojie.android.ui.category.CategoryTypeActivity;
|
import com.haicaojie.android.ui.recommend.CaptureActivity;
|
import com.haicaojie.android.ui.recommend.GoodsBrowserActivity;
|
import com.haicaojie.android.ui.recommend.RecommendCategoryFragment;
|
import com.haicaojie.android.ui.recommend.RecommendFragment;
|
import com.haicaojie.android.ui.recommend.SearchActivity;
|
import com.haicaojie.android.util.zxing.bean.ZxingConfig;
|
import com.haicaojie.android.util.zxing.common.Constant;
|
import com.lcjian.library.RetainViewFragment;
|
import com.lcjian.library.content.ConnectivityChangeHelper;
|
import com.lcjian.library.util.NetUtils;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.cache.DiskLruCache;
|
import com.lcjian.library.util.common.StorageUtils;
|
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
|
import com.umeng.analytics.MobclickAgent;
|
import com.umeng.commonsdk.debug.E;
|
import com.viewpagerindicator.TabPageIndicator;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/12/7.
|
* 主页---精选
|
*/
|
|
public class RecommendTopFragment extends RetainViewFragment implements View.OnClickListener {
|
|
private TextView tv_search_content, tv_category_top, tv_scan;
|
private LinearLayout ll_no_net, ll_recommend_content, ll_no_data, ll_request_failture;
|
private ConnectivityChangeHelper mChangeHelper;
|
// private FrameLayout fl_indicator;
|
|
private int REQUEST_CODE_SCAN = 111;
|
|
/*
|
* viewpager定义
|
*/
|
GoogleMusicAdapter adapter;
|
ViewPager pager;
|
TabPageIndicator indicator;
|
ProgressDialog pd = null;
|
|
private View v_status_bar;
|
private DiskLruCache cache;
|
private boolean isCache = false;
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_recommend_top;
|
}
|
|
@Override
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
try {
|
cache = DiskLruCache.open(
|
new File(StorageUtils.getCacheDirectory(getContext())
|
.toString(), "http"), getVersionNum(getContext()),
|
1, 1024 * 1024);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
v_status_bar = contentView.findViewById(R.id.v_status_bar);
|
/*
|
* 计算状态栏高度并设置
|
*/
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
Window window = getActivity().getWindow();
|
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
|
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
int result = 0;
|
int resourceId = getResources().getIdentifier("status_bar_height",
|
"dimen", "android");
|
if (resourceId > 0) {
|
result = getResources().getDimensionPixelSize(resourceId);
|
}
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
|
result);
|
v_status_bar.setLayoutParams(params);
|
} else {
|
v_status_bar.setVisibility(View.GONE);
|
}
|
tv_search_content = (TextView) contentView.findViewById(R.id.tv_search_content);
|
tv_search_content.setOnClickListener(this);
|
tv_scan = (TextView) contentView.findViewById(R.id.tv_scan);
|
tv_scan.setOnClickListener(this);
|
tv_category_top = (TextView) contentView.findViewById(R.id.tv_category_top);
|
tv_category_top.setOnClickListener(this);
|
ll_recommend_content = (LinearLayout) contentView.findViewById(R.id.ll_recommend_content);
|
indicator = (TabPageIndicator) contentView.findViewById(R.id.indicator);
|
pager = (ViewPager) contentView.findViewById(R.id.viewpager);
|
|
ll_no_net = (LinearLayout) contentView.findViewById(R.id.ll_no_net);
|
ll_no_data = (LinearLayout) contentView.findViewById(R.id.ll_no_data);
|
ll_request_failture = (LinearLayout) contentView.findViewById(R.id.ll_request_failture);
|
contentView.findViewById(R.id.iv_net_setting).setOnClickListener(this);
|
contentView.findViewById(R.id.tv_refresh).setOnClickListener(this);
|
mChangeHelper = new ConnectivityChangeHelper(ll_recommend_content.getContext(),
|
new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
requestState(0);
|
if (mList.size() == 0 || isCache) {
|
setFirstCategory();
|
}
|
}
|
});
|
// setHotData();
|
if (cache != null) {
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getFirstCategory"));
|
if (snapshot != null) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<FirstCategory> list = gson.fromJson(
|
snapshot.getString(0),
|
new TypeToken<List<FirstCategory>>() {
|
}.getType());
|
FirstCategory category = new FirstCategory();
|
category.setName("推荐");
|
mList.add(category);
|
mList.addAll(list);
|
adapter = new GoogleMusicAdapter(
|
getChildFragmentManager());
|
pager.setAdapter(adapter);
|
pager.setOffscreenPageLimit(0);
|
pager.setCurrentItem(0);
|
indicator.setViewPager(pager, 0);
|
|
isCache = true;
|
requestState(0);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (snapshot != null) {
|
snapshot.close();
|
}
|
}
|
}
|
setFirstCategory();
|
ll_no_data.setOnClickListener(this);
|
}
|
|
private int getVersionNum(Context context) {
|
try {
|
PackageInfo pi = context.getPackageManager().getPackageInfo(
|
context.getPackageName(), 0);
|
return pi.versionCode;
|
} catch (PackageManager.NameNotFoundException e) {
|
e.printStackTrace();
|
return 1;
|
}
|
}
|
|
private String getKey(String method) {
|
return new Md5FileNameGenerator().generate(method);
|
}
|
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
//设置状态栏文字颜色及图标为深色
|
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
MobclickAgent.onPageStart("精选");
|
mChangeHelper.registerReceiver();
|
ll_no_data.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
if (mList.size() == 0) {
|
setFirstCategory();
|
}
|
}
|
}, 2000);
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
MobclickAgent.onPageEnd("精选");
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.tv_search_content:
|
Intent intent = new Intent(getContext(), SearchActivity.class);
|
try {
|
intent.putExtra("content", tv_search_content.getHint().toString());
|
} catch (Exception e) {
|
}
|
startActivity(intent);
|
break;
|
case R.id.tv_category_top:
|
startActivity(new Intent(getContext(), CategoryTypeActivity.class));
|
break;
|
case R.id.ll_no_data:
|
setFirstCategory();
|
break;
|
case R.id.tv_scan:
|
Intent intent1 = new Intent(getContext(), CaptureActivity.class);
|
/*ZxingConfig是配置类 可以设置是否显示底部布局,闪光灯,相册,是否播放提示音 震动等动能
|
* 也可以不传这个参数
|
* 不传的话 默认都为默认不震动 其他都为true
|
* */
|
ZxingConfig config = new ZxingConfig();
|
config.setPlayBeep(true);
|
config.setShake(true);
|
intent1.putExtra(Constant.INTENT_ZXING_CONFIG, config);
|
startActivityForResult(intent1, REQUEST_CODE_SCAN);
|
break;
|
case R.id.iv_net_setting:
|
tv_category_top.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
break;
|
case R.id.tv_refresh:
|
tv_category_top.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
// requestState(0);
|
// setFirstCategory();
|
break;
|
}
|
}
|
|
|
List<FirstCategory> mList = new ArrayList<>();
|
|
private void setFirstCategory() {
|
if (pd == null) {
|
pd = new ProgressDialog(getContext());
|
pd.setMessage("加载中...");
|
}
|
pd.show();
|
ShoppingApi.getFirstCategory(getContext(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
isCache = false;
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<FirstCategory> list = gson.fromJson(
|
jsonObject.optJSONObject("data")
|
.optJSONArray("goodsClassList").toString(),
|
new TypeToken<List<FirstCategory>>() {
|
}.getType());
|
|
if (mList.size() > 0)
|
mList.clear();
|
|
FirstCategory category = new FirstCategory();
|
category.setName("推荐");
|
mList.add(category);
|
mList.addAll(list);
|
|
DiskLruCache.Editor editor = cache
|
.edit(getKey("getFirstCategory"));
|
editor.set(0, jsonObject.optJSONObject("data")
|
.optJSONArray("goodsClassList").toString());
|
editor.commit();
|
if (adapter == null) {
|
adapter = new GoogleMusicAdapter(
|
getChildFragmentManager());
|
pager.setAdapter(adapter);
|
} else {
|
adapter.notifyDataSetChanged();
|
}
|
|
pager.setOffscreenPageLimit(0);
|
pager.setCurrentItem(0);
|
|
indicator.setViewPager(pager, 0);
|
indicator.setCurrentItem(0);
|
requestState(mList.size() == 0 ? 1 : 0);
|
// fl_indicator.addView(indicator);
|
} else {
|
requestState(2);
|
SingleToast.showToast(getContext(), jsonObject.optString("msg"));
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable
|
throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
if (cache != null) {
|
DiskLruCache.Snapshot snapshot = null;
|
try {
|
snapshot = cache.get(getKey("getFirstCategory"));
|
} catch (Exception e) {
|
|
}
|
if (snapshot == null && mList.size() == 0) {
|
if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
SingleToast.showToast(getContext(), "网络未连接,请检测网络设置");
|
} else {
|
requestState(2);
|
SingleToast.showToast(getContext(), "网络连接异常,请检测网络设置");
|
}
|
} else {
|
if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
SingleToast.showToast(getContext(), "网络未连接,请检测网络设置");
|
} else {
|
SingleToast.showToast(getContext(), "网络连接异常,请检测网络设置");
|
}
|
}
|
} else {
|
if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
SingleToast.showToast(getContext(), "网络未连接,请检测网络设置");
|
} else {
|
requestState(2);
|
SingleToast.showToast(getContext(), "网络连接异常,请检测网络设置");
|
}
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
|
class GoogleMusicAdapter extends FragmentPagerAdapter {
|
|
public GoogleMusicAdapter(FragmentManager fm) {
|
super(fm);
|
}
|
|
@Override
|
public Fragment getItem(int position) {
|
if (position == 0) {
|
return RecommendFragment.newInstance(mList.get(position));
|
} else {
|
return RecommendCategoryFragment.newInstance(mList.get(position));
|
}
|
}
|
|
@Override
|
public CharSequence getPageTitle(int position) {
|
return mList.get(position % mList.size()).getName();
|
}
|
|
@Override
|
public int getCount() {
|
return mList.size();
|
}
|
|
}
|
|
|
@Override
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
// 扫描二维码/条码回传
|
if (requestCode == REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK) {
|
if (data != null) {
|
String content = data.getStringExtra(Constant.CODED_CONTENT);
|
if (content.contains("taobao://")) {
|
// Intent intent = new Intent(getContext(), GoodsDetailBrowerActivity.class);
|
Intent intent = new Intent(getContext(), GoodsBrowserActivity.class);
|
String id = content.substring(content.indexOf("//") + 2);
|
intent.putExtra("id", id);
|
startActivity(intent);
|
} else {
|
SingleToast.showToast(getContext(), "非海草街商品!");
|
}
|
}
|
}
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
ll_recommend_content.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
|
ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
|
ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
|
}
|
}
|