package com.tejia.lijin.app.ui.timeLimitedSpike;
|
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import androidx.databinding.DataBindingUtil;
|
import android.graphics.Color;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.os.Bundle;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearSmoothScroller;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.ImageView;
|
import android.widget.TextView;
|
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.contract.TimeLimitedSpikeContract;
|
import com.tejia.lijin.app.databinding.ActivityTimelimitedspikeBinding;
|
import com.tejia.lijin.app.entity.SpikTime;
|
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
|
import com.tejia.lijin.app.presenter.TimeLimitedSpikePresenter;
|
import com.tejia.lijin.app.util.ToolUtil;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
import com.tejia.lijin.app.view.BaseActivity;
|
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
/**
|
* 限时秒杀
|
*/
|
@SuppressLint("Registered")
|
public class TimeLimitedSpikeActivity extends BaseActivity<TimeLimitedSpikeContract.TimeLimitedSpikeView, TimeLimitedSpikePresenter>
|
implements TimeLimitedSpikeContract.TimeLimitedSpikeView, View.OnClickListener, TimeSpikAdapter.TimeSpikCallBack {
|
private ActivityTimelimitedspikeBinding binding;
|
private TimeSpikAdapter spikAdapter;
|
private List<SpikTime> mSpikTimeList = new ArrayList<>();
|
private SpikeGoodsListAdapter goodsListAdapter;
|
private List<TaoBaoGoodsBrief> goodsBriefList = new ArrayList<>();
|
private View bottomBinding;
|
private ImageView ivLoading;
|
private TextView tvLoading;
|
private LinearLayoutManager ms;
|
|
@Override
|
protected void initActivityView(Bundle savedInstanceState) {
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_timelimitedspike);
|
}
|
|
@Override
|
protected void findViewById() {
|
TopStatusSettings.setStatusView(this, binding.includeStatusBar.vStatusBar);
|
binding.includeTopBar.tvTopBarMiddle.setText("限时秒杀");
|
binding.includeTopBar.tvTopBarLeft.setOnClickListener(this);
|
ms = new LinearLayoutManager(this);
|
ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
|
binding.timeSpikrv.setLayoutManager(ms); //给RecyClerView 添加设置好的布局样式
|
spikAdapter = new TimeSpikAdapter(this, mSpikTimeList, this);
|
binding.timeSpikrv.setAdapter(spikAdapter);
|
LinearLayoutManager ms2 = new LinearLayoutManager(this);
|
binding.timeGoodsrv.setLayoutManager(ms2); //给RecyClerView 添加设置好的布局样式
|
goodsListAdapter = new SpikeGoodsListAdapter(this, goodsBriefList, "限时秒杀");
|
binding.timeGoodsrv.setAdapter(goodsListAdapter);
|
bottomBinding = LayoutInflater.from(this).inflate(R.layout.item_recyclerview_bottom2, null);
|
goodsListAdapter.addFooterView(bottomBinding);
|
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
|
R.drawable.anim_list_loading);
|
ivLoading = bottomBinding.findViewById(R.id.iv_loading2);
|
tvLoading = bottomBinding.findViewById(R.id.tv_loading2);
|
bottomBinding.setBackgroundColor(Color.parseColor("#EEEEEE"));
|
animationDrawable.start();
|
binding.timeGoodssrl.setColorSchemeColors(getResources().getColor(R.color.theme));
|
binding.timeGoodsrv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int last = layoutManager.findLastVisibleItemPosition();
|
int total = layoutManager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if (last == total - 1 && goodsBriefList.size() < count && isLoad) {
|
page++;
|
isLoad = false;
|
ivLoading.setVisibility(View.VISIBLE);
|
tvLoading.setText("正在加载更多是数据");
|
getSpikeGoodsList(mSpikTimeList.get(position).requestTime, page);
|
}
|
}
|
// if (move) {
|
// move = false;
|
// int n = position - ms.findFirstVisibleItemPosition();
|
// if (n >= 0 && n < recyclerView.getChildCount()) {
|
// recyclerView.scrollBy( recyclerView.getChildAt(n).get,0); //滚动到顶部
|
//// setRecyclerScroll_Center(recyclerView.getFocusedChild());//滚动到屏幕中央
|
// }
|
// }
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
}
|
});
|
binding.timeGoodssrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
page = 1;
|
if (mSpikTimeList.size() > 0)
|
getSpikeGoodsList(mSpikTimeList.get(position).requestTime, page);
|
}
|
});
|
}
|
|
@Override
|
protected TimeLimitedSpikePresenter createPresenter() {
|
return new TimeLimitedSpikePresenter(this);
|
}
|
|
@Override
|
protected void getData() {
|
mPresenter.getSpikTime(this);//获取顶部限时秒杀数据
|
}
|
|
private boolean isLoad = true;
|
public int count = 0;//商品列表数据
|
private boolean move = false;
|
|
/**
|
* 限时秒杀顶部返回 数据
|
**/
|
@Override
|
public void onSpikTime(JSONObject obj) {
|
binding.timeGoodssrl.setRefreshing(true);
|
mPresenter.setSpikTime(obj, mSpikTimeList, spikAdapter);
|
binding.timeGoodssrl.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
int fir = ms.findFirstVisibleItemPosition();
|
int end = ms.findLastVisibleItemPosition();
|
final int p = position;
|
if (p <= fir) {
|
binding.timeSpikrv.scrollToPosition(p);
|
} else if (p <= end) {
|
int top = binding.timeSpikrv.getChildAt(p - fir).getTop();
|
binding.timeSpikrv.scrollBy(top, 0);
|
} else {
|
binding.timeSpikrv.scrollToPosition(position); //先让当前view滚动到列表内
|
move = true;
|
}
|
//当view滚动到列表内 就可以用layout manager来滚动
|
LinearSmoothScroller s1 = new TopSmoothScroller(binding.timeSpikrv.getContext());
|
s1.setTargetPosition(2);
|
ms.startSmoothScroll(s1);
|
}
|
}, 200);
|
}
|
|
@Override
|
public void onSpikTimeFail(String err) {
|
ToolUtil.showToast(this, "请检查网络");
|
}
|
|
private int page = 1;
|
|
/**
|
* 限时秒杀商品数据返回列表
|
**/
|
@Override
|
public void onSpikeGoodsList(JSONObject obj) {
|
binding.timeGoodssrl.setRefreshing(false);
|
mPresenter.setSpikeGoodsList(obj, goodsListAdapter, goodsBriefList, page, ivLoading, tvLoading);
|
}
|
|
@Override
|
public void onSpikeGoodsListFail(String err) {
|
binding.timeGoodssrl.setRefreshing(false);
|
ToolUtil.showToast(this, "请检查网络");
|
}
|
|
/*******************获取限时秒杀商品数据*******/
|
public void getSpikeGoodsList(String time, int page) {
|
mPresenter.getSpikeGoodsList(this, time, page + "", null);
|
}
|
|
@Override
|
public void onClick(View view) {
|
if (view.getId() == R.id.tv_top_bar_left) {
|
finish();
|
}
|
}
|
|
/*****************限时秒杀顶部选中item平移到屏幕中央********/
|
public void setRecyclerScroll_Center(final View clkView) {
|
int itemWidth = clkView.getWidth();
|
int screenWidth = getResources().getDisplayMetrics().widthPixels;
|
int scrollWidth = clkView.getLeft() - (screenWidth / 2 - itemWidth / 2);
|
binding.timeSpikrv.scrollBy(scrollWidth, 0);
|
}
|
|
public int position = 0;
|
|
/************************顶部Adapter单击事件******/
|
@Override
|
public void onTimeSpikCallBack(View view, int position) {
|
binding.timeGoodssrl.setRefreshing(true);
|
this.position = position;
|
mPresenter.setTimeSpikCallBack(mSpikTimeList, spikAdapter, page, position);
|
setRecyclerScroll_Center(view);//滚动到屏幕中央
|
}
|
|
public static class TopSmoothScroller extends LinearSmoothScroller {
|
TopSmoothScroller(Context context) {
|
super(context);
|
}
|
|
@Override
|
protected int getHorizontalSnapPreference() {
|
return SNAP_TO_START;//具体见源码注释
|
}
|
|
@Override
|
protected int getVerticalSnapPreference() {
|
return SNAP_TO_START;//具体见源码注释
|
}
|
}
|
}
|