admin
2021-05-15 2aead6275fdd1bbbd778abc0e85663a2578fab06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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;//具体见源码注释
        }
    }
}