package com.tejia.lijin.app.ui.trends;
|
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.provider.Settings;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
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.wpc.library.RetainViewFragment;
|
import com.wpc.library.content.ConnectivityChangeHelper;
|
import com.wpc.library.util.NetUtils;
|
import com.wpc.library.util.RefreshLayout;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.HomeBanner;
|
import com.tejia.lijin.app.entity.SpecialOffer2;
|
import com.tejia.lijin.app.entity.TrendsCategory;
|
import com.tejia.lijin.app.entity.trends.CollegeArticle;
|
import com.tejia.lijin.app.entity.trends.CollegeContent;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 动态--活动
|
*/
|
|
public class CollegeFragmet extends RetainViewFragment {
|
private RefreshLayout srl_college;
|
private RecyclerView rv_college;
|
private List<HomeBanner> mBannerList = new ArrayList<>();
|
private List<SpecialOffer2> mSpecialList = new ArrayList<>();
|
private int page = 1;
|
private ImageView iv_slid_top;
|
private View v_divider;
|
private int count = 0;
|
private CollegeAdapter adapter;
|
private boolean isLoad = false;
|
private View bottom;
|
private TextView tv_loading;
|
private ImageView iv_loading;
|
private ConnectivityChangeHelper mChangeHelper;
|
private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
|
private boolean haveCache = false;
|
private String TYPE = "college";
|
private TextView tv_notice_content, tv_no_data_hint;
|
private LinearLayout ll_notice;
|
private ImageView iv_close, iv_no_data;
|
private TrendsCategory mCategory;
|
private int cId;
|
private String cate;
|
|
private List<CollegeContent> mList = new ArrayList<>();
|
|
public static CollegeFragmet newInstance(int cId) {
|
CollegeFragmet fragment = new CollegeFragmet();
|
Bundle bundle = new Bundle();
|
bundle.putInt("cid", cId);
|
fragment.setArguments(bundle);
|
return fragment;
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_college;
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
ll_no_net = contentView.findViewById(R.id.ll_no_net);
|
ll_no_data = contentView.findViewById(R.id.ll_no_data);
|
ll_request_failture = contentView.findViewById(R.id.ll_request_failture);
|
v_divider = contentView.findViewById(R.id.v_divider);
|
|
srl_college = contentView.findViewById(R.id.srl_college);
|
rv_college = contentView.findViewById(R.id.lv_college);
|
|
tv_notice_content = contentView.findViewById(R.id.tv_content);
|
ll_notice = contentView.findViewById(R.id.ll_notice);
|
ll_notice.setVisibility(View.GONE);
|
iv_close = contentView.findViewById(R.id.iv_left);
|
tv_no_data_hint = contentView.findViewById(R.id.tv_no_data_hint);
|
iv_no_data = contentView.findViewById(R.id.iv_no_data);
|
/**小黄条*/
|
getAppPageNotification();
|
|
iv_slid_top = contentView.findViewById(R.id.iv_slid_top);
|
float hRatio = 260 / 720f;
|
float wRatio = 23 / 720f;
|
int bottomPo = (int) (DimenUtils.getScreenWidth(getActivity()) * hRatio);
|
int rightPo = (int) (DimenUtils.getScreenWidth(getActivity()) * wRatio);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(DimenUtils.dip2px(getActivity(), 40), DimenUtils.dip2px(getActivity(), 40));
|
params.setMargins(0, 0, rightPo, bottomPo);
|
params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
|
iv_slid_top.setLayoutParams(params);
|
iv_slid_top.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
rv_college.getLayoutManager().scrollToPosition(0);
|
}
|
});
|
rv_college.setLayoutManager(new LinearLayoutManager(srl_college.getContext()));
|
// DividerItemDecoration decoration = new DividerItemDecoration();
|
// decoration.setSize(DimenUtils.dip2px(rv_college.getContext(), 5));
|
// rv_college.addItemDecoration(decoration);
|
|
mCategory = (TrendsCategory) getArguments().getSerializable("category");
|
cId = getArguments().getInt("cid", 0);
|
cate = getArguments().getString("cate");
|
|
|
v_divider = contentView.findViewById(R.id.v_divider);
|
v_divider.setVisibility(StringUtils.isEmpty(cate) ? View.GONE : View.VISIBLE);
|
bottom = LayoutInflater.from(srl_college.getContext()).inflate(R.layout.item_recyclerview_bottom, null);
|
//TODO 添加加载动画
|
// adapter.addFooterView(bottom);
|
bottom.setVisibility(View.GONE);
|
tv_loading = bottom.findViewById(R.id.tv_loading);
|
iv_loading = bottom.findViewById(R.id.iv_loading);
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
iv_loading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
// Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
|
bottom.setVisibility(View.GONE);
|
srl_college.setColorSchemeColors(getResources().getColor(R.color.theme));
|
contentView.findViewById(R.id.tv_net_setting).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
srl_college.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
contentView.findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
rv_college.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
mChangeHelper = new ConnectivityChangeHelper(srl_college.getContext(),
|
new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
if (mList.size() == 0)
|
requestState(3);
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
if (mList.size() == 0 || haveCache) {
|
getCollegeList();
|
}
|
}
|
});
|
rv_college.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int first = manager.findFirstVisibleItemPosition();
|
int last = manager.findLastVisibleItemPosition();
|
int total = manager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (first == 0) {
|
iv_slid_top.setVisibility(View.GONE);
|
}
|
if (last == total - 1 && mList.size() < count && isLoad) {
|
page++;
|
isLoad = false;
|
iv_loading.setVisibility(View.VISIBLE);
|
tv_loading.setText(getResources().getString(R.string.loading_more_text));
|
getCollegeList();
|
}
|
}
|
// else {
|
// iv_slid_top.setVisibility(View.GONE);
|
// }
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
if (Math.abs(dy) > 10) {
|
if (dy > 0) {
|
iv_slid_top.setVisibility(View.GONE);
|
} else {
|
iv_slid_top.setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
});
|
srl_college.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
getCollegeList();
|
}
|
});
|
rv_college.setAdapter(adapter);
|
srl_college.setRefreshing(true);
|
// handler_timeCurrent.sendEmptyMessage(1001);
|
}
|
|
private boolean isCancel = false;//来标识是fragment否退出
|
//这里很重要,使用Handler的延时效果,每隔一秒刷新一下适配器,以此产生倒计时效果
|
@SuppressLint("HandlerLeak")
|
private Handler handler_timeCurrent = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
//isCancel,防止界面销毁时,再一次改变UI
|
if (isCancel || handler_timeCurrent == null) {
|
return;
|
}
|
if (msg.what == 1001) {
|
handler_timeCurrent.sendEmptyMessageDelayed(1001, 1000);
|
}
|
}
|
};
|
|
/**
|
* 时间添加0
|
*
|
* @param time
|
* @return
|
*/
|
private String setTimeSupplement(long time) {
|
String t = "";
|
if (time < 10 && time > 0) {
|
t = "0" + time;
|
} else if (time == 0 || time >= 10) {
|
t = "" + time;
|
}
|
return t;
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
// if (adapter != null)
|
// adapter.onActivityResume();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
}
|
|
private void getAppPageNotification() {
|
ShoppingApi.getAppPageNotification(rv_college.getContext(), TYPE, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
boolean isClose = jsonObject.optJSONObject("data").optBoolean("canClose");
|
String content = jsonObject.optJSONObject("data").optString("content");
|
final String contentUrl = jsonObject.optJSONObject("data").optString("contentUrl");
|
final String md5 = jsonObject.optJSONObject("data").optString("md5");
|
|
tv_notice_content.setVisibility(!StringUtils.isEmpty(content) ? View.VISIBLE : View.GONE);
|
tv_notice_content.setText(content);
|
final SharedPreferences sp = rv_college.getContext().getSharedPreferences("notice", Context.MODE_PRIVATE);
|
if (!sp.getBoolean(md5, false)) {//false 展示,true不展示
|
ll_notice.setVisibility(View.VISIBLE);
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
|
if (isClose) {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.ic_notice_close_red));
|
iv_close.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
ll_notice.setVisibility(View.GONE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean(md5, true);
|
editor.commit();
|
}
|
});
|
} else {
|
iv_close.setImageDrawable(getResources().getDrawable(R.drawable.icon_array_right_red));
|
}
|
ll_notice.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (StringUtils.isEmpty(contentUrl))
|
return;
|
Intent intent = new Intent(rv_college.getContext(), ShareBrowserActivity.class);
|
intent.putExtra("url", contentUrl);
|
rv_college.getContext().startActivity(intent);
|
}
|
});
|
} else {
|
ll_notice.setVisibility(View.GONE);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
});
|
}
|
|
/**
|
* 动态 活动请求
|
*/
|
private void getCollegeList() {
|
ShoppingApi.getCollegeInfo(srl_college.getContext(), page, cId + "", new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (page > 1) {
|
bottom.setVisibility(View.VISIBLE);
|
} else {
|
bottom.setVisibility(View.GONE);
|
}
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
ll_request_failture.setVisibility(View.GONE);
|
bottom.setVisibility(View.GONE);
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
JSONObject data = jsonObject.optJSONObject("data");
|
haveCache = false;
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
if (page == 1) {
|
mList.clear();
|
|
|
if (data.optJSONArray("specials") != null) {
|
List<SpecialOffer2> specialList = gson.fromJson(
|
data.optJSONArray("specials").toString(),
|
new TypeToken<List<SpecialOffer2>>() {
|
}.getType());
|
mSpecialList.clear();
|
mSpecialList.addAll(specialList);
|
}
|
|
|
if (data.optJSONArray("banners") != null) {
|
List<HomeBanner> list = gson.fromJson(
|
data.optJSONArray("banners").toString(),
|
new TypeToken<List<HomeBanner>>() {
|
}.getType());
|
mBannerList.clear();
|
mBannerList.addAll(list);
|
if (mBannerList.size() > 0)
|
mList.add(CollegeContent.createBannerList(mBannerList));
|
}
|
mList.add(CollegeContent.createSearch());
|
mList.add(CollegeContent.createSpecialList(mSpecialList));
|
}
|
|
if (data.optJSONArray("list") != null) {
|
List<CollegeArticle> articleList = gson.fromJson(
|
data.optJSONArray("list").toString(),
|
new TypeToken<List<CollegeArticle>>() {
|
}.getType());
|
if (articleList != null)
|
for (CollegeArticle ca : articleList)
|
mList.add(CollegeContent.createArticle(ca));
|
}
|
if (adapter == null) {
|
adapter = new CollegeAdapter(getActivity(), mList);
|
rv_college.setAdapter(adapter);
|
initLoadingView();
|
} else
|
adapter.notifyDataSetChanged();
|
count = data.optInt("count");
|
if (mBannerList != null && mBannerList.size() > 0)
|
count++;
|
count += 2;
|
|
if (mList.size() == count) {
|
bottom.setVisibility(View.VISIBLE);
|
iv_loading.setVisibility(View.GONE);
|
tv_loading.setText(getResources().getString(R.string.list_no_more));
|
}
|
requestState(mList.size() == 0 ? 1 : 0);
|
} else {
|
if (mList.size() == 0)
|
requestState(2);
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
srl_college.setRefreshing(false);
|
isLoad = true;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
bottom.setVisibility(View.GONE);
|
if (NetUtils.getNetworkState(srl_college.getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
Toast.makeText(srl_college.getContext(), "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
|
} else {
|
requestState(2);
|
Toast.makeText(srl_college.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
|
}
|
}
|
});
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
srl_college.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
rv_college.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);
|
}
|
|
//防止当前Activity结束以后, handler依然继续循环浪费资源
|
@Override
|
public void onDestroy() {
|
isCancel = true;
|
handler_timeCurrent.removeCallbacksAndMessages(null);
|
super.onDestroy();
|
}
|
|
|
private void initLoadingView() {
|
bottom = LayoutInflater.from(getContext()).inflate(R.layout.item_recyclerview_bottom, null);
|
tv_loading = bottom.findViewById(R.id.tv_loading);
|
iv_loading = bottom.findViewById(R.id.iv_loading);
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
iv_loading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
// Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
|
bottom.setVisibility(View.GONE);
|
adapter.addFooterView(bottom);
|
}
|
}
|