package com.tejia.lijin.app.ui.recommend;
|
|
import android.content.Intent;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.os.Bundle;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.RelativeLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.bumptech.glide.Glide;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.content.ConnectivityChangeHelper;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.wpc.library.util.NetUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 自购立减acitivty
|
*/
|
public class SelfBuyAndReductionActivity extends BaseActivity implements View.OnClickListener {
|
|
private TextView tv_left, tv_middle, tv_right, tv_loading;
|
private ImageView iv_slid_top, iv_loading;
|
private SwipeRefreshLayout srl_selfbuy_and_reduction;
|
private RecyclerView rv_selfbuy_and_reduction;
|
private ImageView iv_top_img;
|
private View bottom;
|
private FrameLayout fl_content;
|
private LinearLayout ll_no_data, ll_request_failture, ll_no_net;
|
private RelativeLayout ff_loading_no_data2;//无数据
|
|
private int page = 1;
|
private boolean isLoad = false;
|
private int count = 0;
|
private String ruleUrl = "";
|
|
private ConnectivityChangeHelper mChangeHelper;
|
|
private List<TaoBaoGoodsBrief> mList = new ArrayList<>();
|
FirstRecommendGoodsAdapter adapter;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_selfbuy_and_reduction);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
init();
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
mChangeHelper.registerReceiver();
|
}
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
mChangeHelper.unregisterReceiver();
|
}
|
|
private void init() {
|
tv_left = findViewById(R.id.tv_top_bar_left);
|
tv_middle = findViewById(R.id.tv_top_bar_middle);
|
tv_right = findViewById(R.id.tv_top_bar_right);
|
tv_loading = findViewById(R.id.tv_loading);
|
iv_loading = findViewById(R.id.iv_loading);
|
iv_slid_top = findViewById(R.id.iv_slid_top);
|
fl_content = findViewById(R.id.fl_content);
|
ll_no_data = findViewById(R.id.ll_no_data);
|
ll_no_net = findViewById(R.id.ll_no_net);
|
ll_request_failture = findViewById(R.id.ll_request_failture);
|
srl_selfbuy_and_reduction = findViewById(R.id.srl_selfbuy_and_reduction);
|
rv_selfbuy_and_reduction = findViewById(R.id.rv_selfbuy_and_reduction);
|
|
bottom = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom2, null);
|
tv_loading = bottom.findViewById(R.id.tv_loading2);
|
iv_loading = bottom.findViewById(R.id.iv_loading2);
|
ff_loading_no_data2 = bottom.findViewById(R.id.ff_loading_no_data2);//无数据
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
iv_loading.setImageDrawable(animationDrawable);
|
animationDrawable.start();
|
|
srl_selfbuy_and_reduction.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
getData(1);
|
}
|
});
|
|
srl_selfbuy_and_reduction.setColorSchemeColors(getResources().getColor(R.color.theme));
|
rv_selfbuy_and_reduction.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 ((!isLoad) && last == total - 1 && mList.size() < count) {
|
page++;
|
isLoad = true;
|
tv_loading.setText(getResources().getString(R.string.loading_more_text));
|
iv_loading.setVisibility(View.VISIBLE);
|
getData(page);
|
}
|
}
|
// 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) {
|
findViewById(R.id.iv_slid_top).setVisibility(View.GONE);
|
} else {
|
findViewById(R.id.iv_slid_top).setVisibility(View.VISIBLE);
|
}
|
}
|
}
|
});
|
|
mChangeHelper = new ConnectivityChangeHelper(this, new ConnectivityChangeHelper.OnConnectivityChangeListener() {
|
@Override
|
public void onNetworkUnAvailable() {
|
if (mList.size()==0)
|
requestState(3);
|
}
|
|
@Override
|
public void onNetworkAvailable() {
|
requestState(0);
|
if (mList.size() == 0) {
|
getData(page);
|
}
|
}
|
});
|
tv_middle.setText("自购立减");
|
tv_right.setText("活动规则");
|
|
rv_selfbuy_and_reduction.setLayoutManager(new LinearLayoutManager(this));
|
DividerItemDecoration decoration = new DividerItemDecoration();
|
rv_selfbuy_and_reduction.addItemDecoration(decoration);
|
|
iv_top_img = new ImageView(this);
|
adapter = new FirstRecommendGoodsAdapter(this, mList, "taolijin_buy");
|
rv_selfbuy_and_reduction.setAdapter(adapter);
|
adapter.addFooterView(bottom);
|
adapter.addHeaderView(iv_top_img);
|
|
tv_left.setOnClickListener(this);
|
tv_right.setOnClickListener(this);
|
iv_slid_top.setOnClickListener(this);
|
}
|
|
private void getData(final int page) {
|
ShoppingApi.getBuyGoods(SelfBuyAndReductionActivity.this,
|
page + "", new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (page > 1) {
|
bottom.setVisibility(View.VISIBLE);
|
} else {
|
srl_selfbuy_and_reduction.setRefreshing(true);
|
bottom.setVisibility(View.GONE);
|
}
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
bottom.setVisibility(View.GONE);
|
srl_selfbuy_and_reduction.setRefreshing(false);
|
if (jsonObject.optInt("code") == 0) {
|
count = jsonObject.optJSONObject("data").optInt("count");
|
ruleUrl = jsonObject.optJSONObject("data").optString("ruleUrl");
|
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
List<TaoBaoGoodsBrief> list = gson.fromJson(jsonObject.optJSONObject("data").optJSONArray("list").toString(), new TypeToken<List<TaoBaoGoodsBrief>>() {
|
}.getType());
|
if (page == 1 && mList.size() > 0)
|
mList.clear();
|
mList.addAll(list);
|
adapter.notifyDataSetChanged();
|
if (page == 1 && mList.size() > 0) {
|
rv_selfbuy_and_reduction.getLayoutManager().scrollToPosition(0);
|
}
|
if (mList.size() == count) {
|
bottom.setVisibility(View.VISIBLE);
|
iv_loading.setVisibility(View.GONE);
|
tv_loading.setText(getResources().getString(R.string.list_no_more));
|
}
|
String topPicture = jsonObject.optJSONObject("data").optString("topPicture");//banner图片
|
//显示 无数据图片
|
if (topPicture != null && ruleUrl != null && mList.size() == 0) {
|
bottom.setVisibility(View.VISIBLE);
|
ff_loading_no_data2.setVisibility(View.VISIBLE);
|
rv_selfbuy_and_reduction.setBackgroundColor(getResources().getColor(R.color.white));//更改背景色为白色
|
} else {
|
ff_loading_no_data2.setVisibility(View.GONE);
|
requestState(mList.size() == 0 ? 1 : 0);
|
rv_selfbuy_and_reduction.setBackgroundColor(getResources().getColor(R.color.bg));//更改背景色为灰色
|
}
|
if (topPicture != null && page == 1) {
|
Glide.with(SelfBuyAndReductionActivity.this)
|
.load(topPicture).into(iv_top_img);
|
}
|
} else {
|
if (mList.size() == 0)
|
requestState(2);
|
}
|
isLoad = false;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
bottom.setVisibility(View.GONE);
|
srl_selfbuy_and_reduction.setRefreshing(false);
|
isLoad = false;
|
if (mList.size() == 0) {
|
if (NetUtils.getNetworkState(SelfBuyAndReductionActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
requestState(3);
|
} else {
|
requestState(2);
|
}
|
} else {
|
if (NetUtils.getNetworkState(SelfBuyAndReductionActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
// SingleToast.showToast(SearchResultActivity.this, "网络未连接,请检测网络设置");
|
Toast.makeText(SelfBuyAndReductionActivity.this, "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
|
} else {
|
// SingleToast.showToast(SearchResultActivity.this, "网络连接异常,请检测网络设置");
|
Toast.makeText(SelfBuyAndReductionActivity.this, "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
});
|
}
|
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.tv_top_bar_right:
|
Intent intent = new Intent(SelfBuyAndReductionActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", ruleUrl);
|
startActivity(intent);
|
break;
|
case R.id.iv_slid_top:
|
rv_selfbuy_and_reduction.getLayoutManager().scrollToPosition(0);
|
break;
|
}
|
}
|
|
/**
|
* 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
|
*
|
* @param state
|
*/
|
private void requestState(int state) {
|
fl_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);
|
}
|
|
}
|