admin
2021-10-16 988a99f8f8f8e1e123efd29e108dcd0240ad8a33
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
package com.weikou.beibeivideo.ui.common;
 
import android.os.Bundle;
import androidx.fragment.app.Fragment;
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.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.entity.VideoInfo;
import com.weikou.beibeivideo.entity.VideoType;
import com.weikou.beibeivideo.ui.video.VideoColumn2Adapter;
import com.weikou.beibeivideo.util.DimenUtils;
import com.weikou.beibeivideo.util.UserUtil;
import com.weikou.beibeivideo.util.ui.DividerItemDecoration;
import com.weikou.beibeivideo.R;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
public class VideosFragment extends Fragment implements
        OnClickListener {
 
    private VideoColumn2Adapter adapter;
 
    private SwipeRefreshLayout rl_category_video;
 
    private RecyclerView rv_category_video;
 
    private List<VideoInfo> mVideoInfos;
 
    private String mHomeType;
 
    private String mVideoType;
 
    private String mStarId;
 
    private int mCurrentPage = 1;
 
    private VideoType type;
    View view;
 
    private ImageView iv_download_failure;
 
    private boolean isLoad;
 
    private View loading;
 
    public static VideosFragment newInstance(VideoType type, String homeType, String videoType, String starId) {
        VideosFragment fragment = new VideosFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("videotype", type);
        if (homeType != null)
            bundle.putString("home_type", homeType);
        if (videoType != null)
            bundle.putString("video_type", videoType);
        if (starId != null)
            bundle.putString("star_id", starId);
 
        fragment.setArguments(bundle);
        return fragment;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
 
    @Override
    public void onResume() {
        super.onResume();
 
    }
 
    @Override
    public void onPause() {
        super.onPause();
 
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.videos_activity, null);
        rl_category_video = view.findViewById(R.id.rl_category_video);
        rv_category_video = view.findViewById(R.id.rv_category_video);
        rv_category_video.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration decoration = new DividerItemDecoration();
        decoration.setSize(DimenUtils.dip2px(rv_category_video.getContext(), 6));
        rv_category_video.addItemDecoration(decoration);
 
 
        rv_category_video.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 ((!isLoad) && last == total - 1 && mVideoInfos.size() < 1000) {
                        mCurrentPage++;
                        isLoad = true;
//                        tv_loading.setText("正在加载更多数据");
//                        iv_loading.setVisibility(View.VISIBLE);
                        loading.setVisibility(View.VISIBLE);
                        getVideoList();
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
 
 
        rl_category_video.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                getVideoList();
            }
        });
        iv_download_failure = view
                .findViewById(R.id.iv_videosfragment_download_failure);
        iv_download_failure.setOnClickListener(this);
        view.findViewById(R.id.tv_more_new).setOnClickListener(this);
        view.findViewById(R.id.tv_more_hot).setOnClickListener(this);
 
        type = (VideoType) getArguments().getSerializable("videotype");
 
 
//        if (getArguments() != null)
//            getArguments().clear();
 
        mHomeType = getArguments().getString("home_type");
        try {
            mVideoType = getArguments().getString("video_type");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mStarId = getArguments().getString("star_id");
 
        mHomeType = (mHomeType == null ? "" : mHomeType);
        mVideoType = (mVideoType == null ? "" : mVideoType);
        mStarId = (mStarId == null ? "" : mStarId);
 
 
        mVideoInfos = new ArrayList<>();
        int columns = 2;
        if (type != null && type.getCategoryType() != null && type.getCategoryType().contains("area")) {
            columns = 3;
        }
 
        adapter = new VideoColumn2Adapter(getActivity(), getActivity().getApplicationContext(), mVideoInfos, false, columns, "list", null);
        if (columns == 2)
            adapter.initRecyclerViewDisplayWidthColumn2(rv_category_video);
        else
            adapter.initRecyclerViewDisplayWidthColumn3(rv_category_video);
        loading = LayoutInflater.from(getContext()).inflate(R.layout.item_loading, null);
        loading.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        loading.setVisibility(View.GONE);
        adapter.setFooterView(loading);
        rv_category_video.setAdapter(adapter);
        rl_category_video.setRefreshing(true);
        getVideoList();
        return view;
    }
 
    private void getVideoList() {
        String uid = UserUtil.getUid(rv_category_video.getContext());
        if (type == null)
            return;
        BeibeiVideoAPI.getVideoList(rv_category_video.getContext(), uid, mStarId, mHomeType,
                type.getId(), String.valueOf(mCurrentPage), category_two + "", type.getCategoryType(),
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            iv_download_failure.setVisibility(View.GONE);
                            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                    .create();
                            List<VideoInfo> videoInfos = gson.fromJson(
                                    jsonObject.getJSONObject("Data")
                                            .getJSONArray("data").toString(),
                                    new TypeToken<List<VideoInfo>>() {
                                    }.getType());
 
                            if (mCurrentPage == 1) {
                                mVideoInfos.clear();
                            }
 
                            mVideoInfos.addAll(videoInfos);
                            adapter.setVideoList(mVideoInfos);
                            if (mVideoInfos.size() > 0)
                                adapter.notifyDataSetChanged();
                            // 需要添加广点通原生广告的时候执行以下条件语句,否则取消
                            // if (videoInfos != null && videoInfos.size() ==
                            // 30) {
                            // loadAD();
                            // }
                            if ((videoInfos == null || videoInfos.size() == 0)
                                    && mCurrentPage > 1) {
                                Toast.makeText(rv_category_video.getContext(), "没有更多了",
                                        Toast.LENGTH_SHORT).show();
                                mCurrentPage--;
                            }
                        } else {
                            iv_download_failure.setVisibility(View.VISIBLE);
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                                          String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
                        iv_download_failure.setVisibility(View.VISIBLE);
                    }
 
                    @Override
                    public void onFinish() {
                        rl_category_video.setRefreshing(false);
                        isLoad = false;
                        loading.setVisibility(View.GONE);
                    }
                });
    }
 
    int category_two = 1;
 
    @Override
    public void onClick(View v) {
        TextView tv_new = view.findViewById(R.id.tv_more_new);
        TextView tv_hot = view.findViewById(R.id.tv_more_hot);
 
        switch (v.getId()) {
            case R.id.iv_videosfragment_download_failure:
                mCurrentPage = 1;
                category_two = 1;
                rl_category_video.setRefreshing(true);
                view.findViewById(R.id.tv_more_new).setBackgroundResource(R.drawable.ic_select2);
                view.findViewById(R.id.tv_more_hot).setBackgroundResource(R.drawable.ic_select1);
                getVideoList();
                break;
            case R.id.tv_more_new:
                mCurrentPage = 1;
                rl_category_video.setRefreshing(true);
                category_two = 1;
 
                tv_new.setBackground(getResources().getDrawable(R.drawable.shape_video_list_nav_highlight_bg));
                tv_hot.setBackground(getResources().getDrawable(R.drawable.shape_video_list_nav_bg));
 
                tv_new.setTextColor(getResources().getColor(R.color.video_list_nav_hightlight_text_color));
                tv_hot.setTextColor(getResources().getColor(R.color.video_list_nav_text_color));
 
                getVideoList();
                break;
            case R.id.tv_more_hot:
                mCurrentPage = 1;
                category_two = 2;
                rl_category_video.setRefreshing(true);
                tv_hot.setBackground(getResources().getDrawable(R.drawable.shape_video_list_nav_highlight_bg));
                tv_new.setBackground(getResources().getDrawable(R.drawable.shape_video_list_nav_bg));
                tv_hot.setTextColor(getResources().getColor(R.color.video_list_nav_hightlight_text_color));
                tv_new.setTextColor(getResources().getColor(R.color.video_list_nav_text_color));
                getVideoList();
                break;
 
            default:
                break;
        }
    }
}