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
package com.tejia.lijin.app.ui.sellwellcommodity;
 
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.provider.Settings;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wpc.library.RetainViewFragment;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.common.DimenUtils;
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.recommend.SearchResultAdapter;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 今日热销
 */
public class TodayChlidFragment extends RetainViewFragment implements View.OnClickListener {
    private SwipeRefreshLayout realtimechild_sf;
    private RecyclerView realtimechild_rv;//RecyclerView
 
    private Bundle mBundle;//获取Activity传递过来的参数
    private SearchResultAdapter adapter;//淘宝商品适配
    private List<TaoBaoGoodsBrief> mList = new ArrayList<>();
    private View bottom;
    private TextView tv_loading;
    private ImageView iv_loading;
 
    private int page = 1;
    private boolean isLoad = true;
    private int count = 1;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
    private ImageView ll_no_data_rf;
    private RelativeLayout ff_loading_no_data;//无数据
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_realtimechild;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        realtimechild_sf = contentView.findViewById(R.id.realtimechild_sf);
        realtimechild_rv = contentView.findViewById(R.id.realtimechild_rv);
        //获取Activity传递过来的参数
        mBundle = getArguments();
 
        realtimechild_rv.setLayoutManager(new LinearLayoutManager(getContext()));
        realtimechild_rv.setBackgroundColor(getResources().getColor(R.color.register_gray));
        adapter = new SearchResultAdapter(getContext(), mList, null, "热销榜单_今日");
        adapter.isHorizontal = true;
        adapter.isFirst = true;
        realtimechild_rv.setAdapter(adapter);
        View top_view = LayoutInflater.from(getContext()).inflate(R.layout.item_ware, null);
        TextView tv_test = top_view.findViewById(R.id.tv_test);
        top_view.setBackgroundColor(Color.parseColor("#f3f3f3"));
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv_test.getLayoutParams();
//获取当前控件的布局对象
        params.height = DimenUtils.dip2px(getContext(), 10);//设置当前控件布局的高度
        params.setMargins(0, 0, 0, 0);
        tv_test.setLayoutParams(params);//将设置好的布局参数应用到控件中
 
        bottom = LayoutInflater.from(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.addHeaderView(top_view);
        adapter.addFooterView(bottom);
 
        //无数据 无网络  网络异常
        ll_no_net = contentView.findViewById(R.id.ll_no_net);
        ll_no_data = contentView.findViewById(R.id.ll_no_data);
        ll_request_failture = contentView.findViewById(R.id.ll_request_failture);
        contentView.findViewById(R.id.tv_net_setting).setOnClickListener(this);
        contentView.findViewById(R.id.tv_refresh).setOnClickListener(this);
        ll_no_data_rf = contentView.findViewById(R.id.iv_refresh);
        ll_no_data_rf.setOnClickListener(this);
 
 
        realtimechild_sf.setRefreshing(true);
        realtimechild_sf.setColorSchemeColors(getResources().getColor(R.color.theme));
        ((SimpleItemAnimator) realtimechild_rv.getItemAnimator()).setSupportsChangeAnimations(false); //取消RecyclerView的动画效果
        realtimechild_sf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getGoodsList();
            }
        });
        realtimechild_rv.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 (last == total - 1 && mList.size() < count && isLoad) {
                        page++;
                        isLoad = false;
                        tv_loading.setText(getResources().getString(R.string.loading_more_text));
                        iv_loading.setVisibility(View.VISIBLE);
                        getGoodsList();
                    }
                }
            }
        });
 
        getGoodsList();
    }
 
    private void getGoodsList() {
        ShoppingApi.getSellshopList(getContext(), page + "", "3", mBundle.getString("id"), new BasicTextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                if (page > 1) {
                    bottom.setVisibility(View.VISIBLE);
                } else {
                    if (page == 1) {
                        bottom.setVisibility(View.VISIBLE);
                        iv_loading.setVisibility(View.GONE);
                        tv_loading.setText(" ");
                    } else {
                        bottom.setVisibility(View.GONE);
                    }
                }
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    List<TaoBaoGoodsBrief> list = gson.fromJson(jsonObject.optJSONObject("data").
                            optJSONArray("list").toString(), new TypeToken<List<TaoBaoGoodsBrief>>() {
                    }.getType());
 
//                    List<JSONObject> list = new ArrayList<>();
//                    for (int i = 0; i < jsonObject.getJSONObject("data").optJSONArray("list").length(); i++) {
//                        list.add((JSONObject) jsonObject.getJSONObject("data").optJSONArray("list").get(i));
//                    }
                    if (page == 1 && mList.size() > 0)//第一页的时候表示刷新 需要情况list
                        mList.clear();
                    if (list.size() > 0)
                        mList.addAll(list);
 
 
                    count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
                    isLoad = true;
                    if (page == 1 && mList.size() > 0)//第一页的时候表示刷新 需要情况list
                        mList.clear();
                    if (!list.toString().equals("[]")) {//判断是否数据为空
                        mList.addAll(list);
                    }
                    if (mList.size() >= count && mList.size() > 5) { //下拉数据请求完 到底了
                        bottom.setVisibility(View.VISIBLE);
                        iv_loading.setVisibility(View.GONE);
                        tv_loading.setText(getResources().getString(R.string.list_no_more));
                    } else {  //下拉还有数据时
                        if (bottom.getVisibility() == View.VISIBLE) {
                            bottom.setVisibility(View.GONE);
                        }
                    }
                    if (mList.size() == 0) { //没有数据情况
                        bottom.setVisibility(View.VISIBLE);
                        ff_loading_no_data.setVisibility(View.VISIBLE); //展示 无数据页
                    } else {
                        ff_loading_no_data.setVisibility(View.GONE); //隐藏无数据页
                    }
 
                    adapter.notifyItemRangeChanged(0, mList.size()); //进行列表全部刷新
//                    if (page == 1) { //第一页的情况下 加载店铺足迹 /or 店铺数据
//                        if (!mBundle.getString("id").equals("0")) {// 不是精选情况 精选在 onResume生命周期刷新
//                            getCateGorySecond(jsonObject.getJSONObject("data").optJSONArray("listShop"));//設置 店鋪数据
//                        }
//                    }
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                realtimechild_sf.setRefreshing(false);
                bottom.setVisibility(View.GONE);
                if (mList != null && mList.size() == 0) {
                    if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        requestState(3);
                    } else {
                        requestState(2);
                    }
                } else {
                    if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        Toast.makeText(getContext(), "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(bottom.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
                    }
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                realtimechild_sf.setRefreshing(false);
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_net_setting:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.tv_refresh:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.iv_refresh:
//                fragment_brand_fragment_sf.setRefreshing(false);
                if (!realtimechild_sf.isRefreshing()) {
                    realtimechild_sf.setRefreshing(true);
                }
                Toast.makeText(getContext(), "" + realtimechild_sf.isRefreshing(), Toast.LENGTH_LONG).show();
                break;
        }
    }
 
    /**
     * 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
     *
     * @param state
     */
    private void requestState(int state) {
//        fragment_brand_fragment_sf.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
//        fragment_brand_fragment_rv.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        ll_no_data_rf.setVisibility(state == 1 ? 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);
    }
 
    @Override
    public void onPause() {
        super.onPause();
        realtimechild_sf.setRefreshing(false);
        realtimechild_sf.destroyDrawingCache();
        realtimechild_sf.clearAnimation();
//        realtimechild_rv.removeOnScrollListener(mOnScrollListener);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
//        mBundle = null;//获取Activity传递过来的参数
//        mList = null;
//        adapter = null;//淘宝商品适配
//        page = 0;
//        count = 0;
//
//        bottom = null;
    }
}