package com.tejia.lijin.app.ui.mine;
|
|
import android.content.SharedPreferences;
|
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.TextView;
|
|
import com.bumptech.glide.Glide;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.tejia.lijin.app.ShoppingApplication;
|
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.widget.ResizableImageView;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.BuXinConstant;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.WelfareTicket;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
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 WelfareCenterActivity extends BaseActivity implements View.OnClickListener {
|
|
private TextView tv_left, tv_middle;
|
private SwipeRefreshLayout srl_welfare_center;
|
private RecyclerView rv_welfare_center;
|
private FrameLayout fl_content;
|
LinearLayout ll_no_ticket;
|
ResizableImageView iv_top, iv_top_img;
|
int page = 1;
|
WelfareCenterAdapter adapter;
|
List<WelfareTicket> mList;
|
|
private int count = 1;
|
private ImageView iv_loading;
|
private TextView tv_loading;
|
View bottom;
|
ShapeLoadingDialog pd = null;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_welfare_center);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
initView();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
boolean isLogin = sp.getBoolean("isLogin", false);
|
if (!isLogin) {
|
UserUtil.jumpLogin(WelfareCenterActivity.this);
|
}
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
if (BuXinConstant.GO_SCAN) {
|
finish();
|
}
|
getWelfareCenterList();
|
}
|
|
/*
|
初始化View
|
*/
|
private void initView() {
|
tv_left = findViewById(R.id.tv_top_bar_left);
|
tv_middle = findViewById(R.id.tv_top_bar_middle);
|
srl_welfare_center = findViewById(R.id.srl_welfare_center);
|
rv_welfare_center = findViewById(R.id.rv_welfare_center);
|
ll_no_ticket = findViewById(R.id.ll_no_ticket);
|
iv_top_img = findViewById(R.id.iv_top_img);
|
fl_content = findViewById(R.id.fl_content);
|
|
bottom = LayoutInflater.from(this).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);
|
|
tv_middle.setText("福利中心");
|
|
LinearLayoutManager manager = new LinearLayoutManager(this);
|
rv_welfare_center.setLayoutManager(manager);
|
DividerItemDecoration decoration = new DividerItemDecoration();
|
decoration.setSize(DimenUtils.dip2px(this, 10));
|
rv_welfare_center.addItemDecoration(decoration);
|
srl_welfare_center.setColorSchemeColors(getResources().getColor(R.color.theme));
|
|
mList = new ArrayList<>();
|
adapter = new WelfareCenterAdapter(WelfareCenterActivity.this, mList);
|
adapter.setHasStableIds(true);
|
rv_welfare_center.setAdapter(adapter);
|
iv_top = new ResizableImageView(this);
|
adapter.addHeaderView(iv_top);
|
adapter.addFooterView(bottom);
|
|
srl_welfare_center.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
getWelfareCenterList();
|
}
|
});
|
|
rv_welfare_center.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int last = manager.findLastVisibleItemPosition();
|
int total = manager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (last == total - 1 && mList.size() < count) {
|
page++;
|
iv_loading.setVisibility(View.VISIBLE);
|
tv_loading.setText("正在加载更多是数据");
|
getWelfareCenterList();
|
}
|
}
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
}
|
});
|
|
tv_left.setOnClickListener(this);
|
}
|
|
/*
|
获取福利中心数据
|
*/
|
private void getWelfareCenterList() {
|
if (pd == null) {
|
pd = new ShapeLoadingDialog.Builder(this).build();
|
}
|
if (mList.size() == 0)
|
pd.show();
|
ShoppingApi.getWelfareCenterList(this, page + "", UserUtil.getUid(ShoppingApplication.application), 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 {
|
bottom.setVisibility(View.GONE);
|
if (jsonObject.optInt("code") == 0) {
|
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
|
List<WelfareTicket> list = gson.fromJson(jsonObject.optJSONObject("data")
|
.optJSONArray("list").toString(),
|
new TypeToken<List<WelfareTicket>>() {
|
}.getType());
|
if (page == 1)
|
mList.clear();
|
mList.addAll(list);
|
adapter.notifyDataSetChanged();
|
// adapter.notifyItemRangeChanged(mList.size(), list.size());//倒序批量刷新列表
|
String topImg = jsonObject.optJSONObject("data").
|
optString("topPicture");
|
count = Integer.parseInt(jsonObject.optJSONObject("data")
|
.optString("count"));
|
Glide.with(WelfareCenterActivity.this).load(topImg).into(iv_top);
|
if (mList.size() > 0 && mList.size() == count) {
|
bottom.setVisibility(View.VISIBLE);
|
iv_loading.setVisibility(View.GONE);
|
tv_loading.setText(getResources().getString(R.string.list_no_more));
|
}
|
ll_no_ticket.setVisibility(mList.size() == 0 ? View.VISIBLE : View.GONE);
|
fl_content.setBackgroundColor(getResources().getColor(R.color.register_gray));
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
srl_welfare_center.setRefreshing(false);
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
bottom.setVisibility(View.GONE);
|
if (mList.size() == 0) {
|
ll_no_ticket.setVisibility(View.VISIBLE);
|
rv_welfare_center.setVisibility(View.GONE);
|
} else {
|
ll_no_ticket.setVisibility(View.GONE);
|
rv_welfare_center.setVisibility(View.VISIBLE);
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
}
|
}
|
}
|