admin
2021-05-17 06a80d5c4b3a971cdc1ca3d91717ec3f6e03a443
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.tejia.lijin.app.ui.suningSpecial;
 
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.viewpagerindicator.LinePageIndicator;
import com.wpc.library.widget.RatioLayout;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.contract.SuningSpecialFgContract;
import com.tejia.lijin.app.entity.HomeBanner;
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
import com.tejia.lijin.app.presenter.SuningSpeciaFgPresenter;
import com.tejia.lijin.app.ui.jdSpecial.JdSpecialTopAdapter;
import com.tejia.lijin.app.ui.recommend.RecommendCategoryAdapter;
import com.tejia.lijin.app.view.BaseFragment;
 
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * * 京东精选 fragment
 */
 
public class SuningSpecialFrangment extends BaseFragment<SuningSpecialFgContract.SuningSpecialView, SuningSpeciaFgPresenter> implements SuningSpecialFgContract.SuningSpecialView, View.OnClickListener {
    private Bundle mBundle;//接受activity传入的值
    private SwipeRefreshLayout fragment_jdspecial_sf;
    private RecyclerView fragment_jdspecial_rv;
    //--------------Top 顶部banner-----------
    private RatioLayout jdfgspecial_topbanner;//顶部banner 布局
    private ViewPager jdfgspecial_recommend;//顶部banner viewpage
    private LinePageIndicator indicator_recommend;//顶部banner 指示器
    private JdSpecialTopAdapter bannerAdapter;
    private List<HomeBanner> mBanners = new ArrayList<>();
    //--------------------Top 拼多多专题列表
    private RatioLayout pdd_top_speciallist;//布局
    private ImageView pdd_top_speciallist1, pdd_top_speciallist2,//5张图片
            pdd_top_speciallist3, pdd_top_speciallist4, pdd_top_speciallist5;
    private List<HomeBanner> mlistSpe = new ArrayList<>();
    //---------------bottom 底部加载
    private View bottom;
    private ImageView iv_loading, ll_no_data_rf;
    private RelativeLayout ff_loading_no_data;
 
    //-----------------京东商品
    private List<TaoBaoGoodsBrief> mList = new ArrayList<>();
    //    private FirstRecommendGoodsAdapter adapter;//RecommendGoodsAdapter
    private RecommendCategoryAdapter adapter;//
    //--------------无网 无数据
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
    private TextView tv_loading; //到底了  无数据刷新
    private ImageView iv_slid_top;//置顶
 
    @Override
    protected int initFragmentView() {
        return R.layout.fragment_jdspecial;
    }
 
    @Override
    protected void initFragmentChildView(View view) {
        mBundle = getArguments();
        fragment_jdspecial_sf = view.findViewById(R.id.fragment_jdspecial_sf);
        fragment_jdspecial_rv = view.findViewById(R.id.fragment_jdspecial_rv);
        iv_slid_top = view.findViewById(R.id.iv_slid_top);
        iv_slid_top.setOnClickListener(this);
        //无数据 无网络  网络异常
        ll_no_net = view.findViewById(R.id.ll_no_net);
        ll_no_data = view.findViewById(R.id.ll_no_data);
        ll_request_failture = view.findViewById(R.id.ll_request_failture);
        ll_no_data_rf = view.findViewById(R.id.iv_refresh);
        ll_no_data_rf.setOnClickListener(this);
        fragment_jdspecial_sf.setColorSchemeColors(getResources().getColor(R.color.theme));
        fragment_jdspecial_rv.setLayoutManager(new LinearLayoutManager(fragment_jdspecial_rv.getContext()));
        //--------------Top 顶部banner-----------
        View top_view = LayoutInflater.from(fragment_jdspecial_sf.getContext()).inflate(R.layout.item_fragmentjdspecial_top, null);
        top_view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
        jdfgspecial_topbanner = top_view.findViewById(R.id.jdfgspecial_topbanner);
        jdfgspecial_recommend = top_view.findViewById(R.id.jdfgspecial_recommend);
        indicator_recommend = top_view.findViewById(R.id.jdfgspecial_indicator_recommend);
        bannerAdapter = new JdSpecialTopAdapter(mBanners, getActivity());
        jdfgspecial_recommend.setAdapter(bannerAdapter);
        indicator_recommend.setViewPager(jdfgspecial_recommend);
        //----------------------Top 顶部专题列表
        pdd_top_speciallist = top_view.findViewById(R.id.pdd_top_speciallist);
        pdd_top_speciallist1 = top_view.findViewById(R.id.pdd_top_speciallist1);
        pdd_top_speciallist2 = top_view.findViewById(R.id.pdd_top_speciallist2);
        pdd_top_speciallist3 = top_view.findViewById(R.id.pdd_top_speciallist3);
        pdd_top_speciallist4 = top_view.findViewById(R.id.pdd_top_speciallist4);
        pdd_top_speciallist5 = top_view.findViewById(R.id.pdd_top_speciallist5);
        pdd_top_speciallist1.setOnClickListener(this);
        pdd_top_speciallist2.setOnClickListener(this);
        pdd_top_speciallist3.setOnClickListener(this);
        pdd_top_speciallist4.setOnClickListener(this);
        pdd_top_speciallist5.setOnClickListener(this);
        //---------------bottom 底部加载
        bottom = LayoutInflater.from(fragment_jdspecial_sf.getContext()).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_data = bottom.findViewById(R.id.ff_loading_no_data2);
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.anim_list_loading);
        iv_loading.setImageDrawable(animationDrawable);
        animationDrawable.start();
        bottom.setVisibility(View.GONE);
 
        adapter = new RecommendCategoryAdapter(getActivity(), mList, "Jd", "京东专题");
        fragment_jdspecial_rv.setAdapter(adapter);
//        goodsAdapter.addFooterView(bottom);
        adapter.addHeaderView(top_view);
        adapter.addFooterView(bottom);
        ((SimpleItemAnimator) fragment_jdspecial_rv.getItemAnimator()).setSupportsChangeAnimations(false); //取消RecyclerView的动画效果
        //下拉刷新
        fragment_jdspecial_sf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getGoodsInfo(page + "", mBundle.getString("id"), bottom, tv_loading, "");
            }
        });
        //RecyclerView 滑动监听(判断滑动到底部)
        fragment_jdspecial_rv.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 first = manager.findFirstVisibleItemPosition();
                int total = manager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (first >= 3) {
                        iv_slid_top.setVisibility(View.GONE);
                    }
                    if (last == total - 1 && mList.size() < count && isLoad) {
                        page++;
                        isLoad = false;
                        mPresenter.getGoodsInfo(page + "", mBundle.getString("id"), bottom, tv_loading, getResources().getString(R.string.loading_more_text));
                    }
                } 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) > 30) {
                    if (dy > 0) {
                        iv_slid_top.setVisibility(View.GONE);
                    } else {
                        iv_slid_top.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        //viewpager 轮播滚动
        jdfgspecial_recommend.postDelayed(mAutoScroller, 2000);
    }
 
    private Runnable mAutoScroller = new Runnable() {
 
        @Override
        public void run() {
            PagerAdapter adapter = jdfgspecial_recommend.getAdapter();
            if (adapter != null && adapter.getCount() != 0) {
                jdfgspecial_recommend.setCurrentItem((jdfgspecial_recommend.getCurrentItem() + 1)
                        % adapter.getCount(), true);
            }
            jdfgspecial_recommend.postDelayed(this, 5000);
        }
    };
 
    private boolean isLoad = true;
    private int count = 0;
 
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {//可见状态
            if (mList.size() == 0 || mBanners.size() == 0) {
                if (fragment_jdspecial_sf != null) {
                    mPresenter.getGoodsInfo(page + "", mBundle.getString("id"), bottom, tv_loading, "");
                }
            }
        }
    }
 
    @Override
    protected void initFragmentData(Bundle savedInstanceState) {//初始化请求数据
        fragment_jdspecial_sf.setRefreshing(true);
        mPresenter.getGoodsInfo(page + "", mBundle.getString("id"), bottom, tv_loading, "");
    }
 
    @Override
    protected SuningSpeciaFgPresenter createPresenter() {
        return new SuningSpeciaFgPresenter(getContext());
    }
 
    int page = 1;//页码
 
    /**
     * 网络请求成功
     *
     * @param obj
     */
    @Override
    public void onSearchSuccess(JSONObject obj) {
        isLoad = true;
        if (page == 1) {
            //顶部banner
            mPresenter.setBanner(obj, indicator_recommend, jdfgspecial_topbanner, mBanners, bannerAdapter, page, indicator_recommend);
            mPresenter.setlistSpe(obj, page, pdd_top_speciallist, pdd_top_speciallist1, pdd_top_speciallist2, pdd_top_speciallist3, pdd_top_speciallist4,
                    pdd_top_speciallist5, mlistSpe);
        }
        //商品加载
        mPresenter.setList(obj, mList, page, fragment_jdspecial_sf, bottom, iv_loading, tv_loading, ff_loading_no_data);
        count = obj.optJSONObject("data").optInt("count");//商品总数量
        adapter.notifyDataSetChanged();
        fragment_jdspecial_sf.setRefreshing(false);
        mPresenter.requestState(0, ll_no_data_rf, ll_no_net, ll_no_data, ll_request_failture);
    }
 
    /**
     * 网络请求失败
     *
     * @param errorInfo
     */
    @Override
    public void onSearchFail(String errorInfo) {
        isLoad = true;
        fragment_jdspecial_sf.setRefreshing(false);
        bottom.setVisibility(View.GONE);
        //请求失败 判断请求状态:  返回数据为空; 网络请求失败;  没有连接网络
        mPresenter.error(mList, ll_no_data_rf, ll_no_net, ll_no_data, ll_request_failture);
    }
 
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //刷新数据
            case R.id.iv_refresh:
                if (fragment_jdspecial_sf != null && fragment_jdspecial_sf.isRefreshing()) {
                    fragment_jdspecial_sf.setRefreshing(false);
                }
                mPresenter.getGoodsInfo(page + "", mBundle.getString("id"), bottom, tv_loading, "");
                fragment_jdspecial_sf.setRefreshing(true);
                break;
            case R.id.iv_slid_top://置顶
                fragment_jdspecial_rv.getLayoutManager().scrollToPosition(0);
                break;
            case R.id.pdd_top_speciallist1://图1
                mPresenter.setClickSpe(mlistSpe, 0);
                break;
            case R.id.pdd_top_speciallist2://图2
                mPresenter.setClickSpe(mlistSpe, 1);
                break;
            case R.id.pdd_top_speciallist3://图3
                mPresenter.setClickSpe(mlistSpe, 2);
                break;
            case R.id.pdd_top_speciallist4://图4
                mPresenter.setClickSpe(mlistSpe, 3);
                break;
            case R.id.pdd_top_speciallist5://图5
                mPresenter.setClickSpe(mlistSpe, 4);
                break;
            default:
                break;
        }
    }
 
    @Override
    public void onPause() {
        super.onPause();
        if (fragment_jdspecial_sf != null) {
            fragment_jdspecial_sf.setRefreshing(false);
        }
        isLoad = true;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (fragment_jdspecial_sf != null) {
            fragment_jdspecial_sf.setRefreshing(false);
        }
        if (jdfgspecial_recommend != null) {
            jdfgspecial_recommend.removeCallbacks(mAutoScroller);
        }
    }
}