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.TrendsCategory;
|
import com.tejia.lijin.app.entity.TrendsInfo;
|
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
|
import com.tejia.lijin.app.util.user.LoginAndInviteStatusUtil;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 动态--活动
|
*/
|
|
public class Trends2Fragmet extends RetainViewFragment {
|
private RefreshLayout srl_trends;
|
private RecyclerView rv_trends;
|
private List<TrendsInfo> mList = new ArrayList<>();
|
private int page = 1;
|
private ImageView iv_slid_top;
|
private View v_divider;
|
private int count = 0;
|
private Trends2Adapter 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 = "activity";
|
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;
|
|
public static Trends2Fragmet newInstance(int cId, String cate, TrendsCategory category) {
|
Trends2Fragmet fragment = new Trends2Fragmet();
|
Bundle bundle = new Bundle();
|
bundle.putSerializable("category", category);
|
bundle.putInt("cid", cId);
|
bundle.putString("cate", cate);
|
fragment.setArguments(bundle);
|
return fragment;
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_trends;
|
}
|
|
private ShapeLoadingDialog pd;
|
|
@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_trends = contentView.findViewById(R.id.srl_trends);
|
rv_trends = contentView.findViewById(R.id.lv_trends);
|
|
tv_notice_content = contentView.findViewById(R.id.tv_notice_content);
|
ll_notice = contentView.findViewById(R.id.ll_notice);
|
iv_close = contentView.findViewById(R.id.iv_close);
|
tv_no_data_hint = contentView.findViewById(R.id.tv_no_data_hint);
|
iv_no_data = contentView.findViewById(R.id.iv_no_data);
|
/**小黄条*/
|
// getAppPageNotification();
|
|
pd = new ShapeLoadingDialog.Builder(getContext()).build();
|
|
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_trends.getLayoutManager().scrollToPosition(0);
|
iv_slid_top.setVisibility(View.GONE);
|
}
|
});
|
rv_trends.setLayoutManager(new LinearLayoutManager(srl_trends.getContext()));
|
// DividerItemDecoration decoration = new DividerItemDecoration();
|
// decoration.setSize(DimenUtils.dip2px(rv_trends.getContext(), 5));
|
// rv_trends.addItemDecoration(decoration);
|
|
mCategory = (TrendsCategory) getArguments().getSerializable("category");
|
cId = getArguments().getInt("cid", 0);
|
cate = getArguments().getString("cate");
|
adapter = new Trends2Adapter(getActivity(), cate, mList, new SendCircleAdapter.CommentClickListener() {
|
@Override
|
public void onTagClick(int position) {
|
|
}
|
|
@Override
|
public void onCopyClick(int position) {
|
if (LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()), true)) {
|
//复制
|
ShoppingApi.copySpecialComment(getContext(), UserUtil.getUid(getContext()), mList.get(position).getId(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (pd != null && !pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (jsonObject.optInt("code") == 0) {
|
String text = jsonObject.optJSONObject("data").optString("text");
|
ClipboardUtil.copy(getContext(), text);
|
Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd != null && pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
}
|
});
|
rv_trends.setAdapter(adapter);
|
|
v_divider = contentView.findViewById(R.id.v_divider);
|
v_divider.setVisibility(StringUtils.isEmpty(cate) ? View.GONE : View.VISIBLE);
|
bottom = LayoutInflater.from(srl_trends.getContext()).inflate(R.layout.item_recyclerview_bottom, null);
|
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_trends.setColorSchemeColors(getResources().getColor(R.color.theme));
|
contentView.findViewById(R.id.tv_net_setting).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
srl_trends.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
contentView.findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
rv_trends.getContext().startActivity(new Intent(Settings.ACTION_SETTINGS));
|
}
|
});
|
mChangeHelper = new ConnectivityChangeHelper(srl_trends.getContext(),
|
new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
|
@Override
|
public void onNetworkUnAvailable() {
|
if (mList.size() == 0)
|
requestState(3);
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
if (mList.size() == 0 || haveCache) {
|
getTrendsList();
|
}
|
}
|
});
|
rv_trends.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));
|
getTrendsList();
|
}
|
}
|
// 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_trends.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
getTrendsList();
|
}
|
});
|
rv_trends.setAdapter(adapter);
|
srl_trends.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;
|
}
|
setCountdown();//计算倒计时
|
if (msg.what == 1001) {
|
handler_timeCurrent.sendEmptyMessageDelayed(1001, 1000);
|
}
|
}
|
};
|
|
/**
|
* 计算倒计时
|
*/
|
private void setCountdown() {
|
if (mList.size() > 0) {
|
for (int i = 0; i < mList.size(); i++) {
|
TrendsInfo tr = mList.get(i);
|
long time = tr.getCountDownTime();
|
if (time > 0) {
|
long days = time / (24 * 60 * 60);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
|
long hours = (time / (60 * 60) - days * 24);//这个时间相差的减去天数的小时数
|
long min = ((time / (60)) - days * 24 * 60 - hours * 60);//
|
// long s = (time - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
|
mList.get(i).setDays(setTimeSupplement(days));
|
mList.get(i).setHours(setTimeSupplement(hours));
|
mList.get(i).setMin(setTimeSupplement(min));
|
mList.get(i).setCountDownTime((tr.getCountDownTime() - 1L));
|
} else {
|
mList.get(i).setDays(setTimeSupplement(0));
|
mList.get(i).setHours(setTimeSupplement(0));
|
mList.get(i).setMin(setTimeSupplement(0));
|
}
|
}
|
adapter.notifyDataSetChanged();
|
}
|
}
|
|
/**
|
* 时间添加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_trends.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_trends.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));
|
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.ic_notice_input));
|
}
|
ll_notice.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (StringUtils.isEmpty(contentUrl))
|
return;
|
Intent intent = new Intent(rv_trends.getContext(), ShareBrowserActivity.class);
|
intent.putExtra("url", contentUrl);
|
rv_trends.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 getTrendsList() {
|
System.out.println("getTrendsList" + page);
|
ShoppingApi.getRecommendActivity(srl_trends.getContext(), page + "", cId + "",
|
mCategory != null ? mCategory.getId() + "" : "", new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (page > 0) {
|
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")) {
|
haveCache = false;
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<TrendsInfo> list = gson.fromJson(
|
jsonObject.optJSONObject("data").optJSONArray("list").toString(),
|
new TypeToken<List<TrendsInfo>>() {
|
}.getType());
|
if (page == 1)
|
mList.clear();
|
mList.addAll(list);
|
adapter.notifyDataSetChanged();
|
setCountdown();//计算倒计时
|
// adapter.notifyItemRangeChanged(mList.size(), mList.size() - list.size() + 1);//倒序批量刷新列表
|
count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
|
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_trends.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_trends.getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
Toast.makeText(srl_trends.getContext(), "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
|
} else {
|
requestState(2);
|
Toast.makeText(srl_trends.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
|
}
|
}
|
});
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
srl_trends.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
|
rv_trends.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();
|
}
|
}
|