admin
2020-08-08 3945f9a2c70335958c64c73894e3a1a14a7113b3
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
package com.weikou.beibeivideo.ui.common;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
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.lcjian.library.util.RefreshLayout;
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.discover.StarWorksAdapter;
import com.yeshi.buwanshequ.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 StarWorksAdapter adapter;
 
    private RefreshLayout rl_category_video;
 
    private ListView lv_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;
 
    public static VideosFragment newInstance(VideoType type) {
        VideosFragment fragment = new VideosFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("videotype", type);
        fragment.setArguments(bundle);
        return fragment;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("分类详情页");
    }
 
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("分类详情页");
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.videos_activity, null);
        rl_category_video = (RefreshLayout) view.findViewById(R.id.rl_category_video);
        lv_category_video = (ListView) view.findViewById(R.id.lv_category_video);
        rl_category_video.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                mCurrentPage++;
                getVideoList();
            }
        });
        rl_category_video.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                getVideoList();
            }
        });
        iv_download_failure = (ImageView) 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);
//        view.findViewById(R.id.tv_more_comment).setOnClickListener(this);
 
        type = (VideoType) getArguments().getSerializable("videotype");
 
        mHomeType = getActivity().getIntent().getStringExtra("home_type");
        mVideoType = getActivity().getIntent().getStringExtra("video_type");
        mStarId = getActivity().getIntent().getStringExtra("star_id");
 
        mHomeType = (mHomeType == null ? "" : mHomeType);
        mVideoType = (mVideoType == null ? "" : mVideoType);
        mStarId = (mStarId == null ? "" : mStarId);
 
        lv_category_video.addHeaderView(new View(lv_category_video.getContext()));
        ProgressBar pb = new ProgressBar(lv_category_video.getContext());
        mVideoInfos = new ArrayList<VideoInfo>();
        adapter = new StarWorksAdapter(mVideoInfos, getActivity().getApplicationContext());
        lv_category_video.setAdapter(adapter);
        rl_category_video.setFooter(pb);
 
        getVideoList();
        return view;
    }
 
    private void getVideoList() {
        SharedPreferences preferences = lv_category_video.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.getVideoList(lv_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);
                            if (mVideoInfos.size() > 0)
                                adapter.notifyDataSetChanged();
                            // 需要添加广点通原生广告的时候执行以下条件语句,否则取消
                            // if (videoInfos != null && videoInfos.size() ==
                            // 30) {
                            // loadAD();
                            // }
                            if ((videoInfos == null || videoInfos.size() == 0)
                                    && mCurrentPage > 1) {
                                Toast.makeText(lv_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);
                        rl_category_video.setLoading(false);
                    }
                });
    }
 
    int category_two = 1;
 
    @Override
    public void onClick(View v) {
 
        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);
//                view.findViewById(R.id.tv_more_comment).setBackgroundResource(R.drawable.ic_select1);
                getVideoList();
                break;
            case R.id.tv_more_new:
                mCurrentPage = 1;
                rl_category_video.setRefreshing(true);
                category_two = 1;
                view.findViewById(R.id.tv_more_new).setBackgroundResource(R.drawable.ic_select2);
                view.findViewById(R.id.tv_more_hot).setBackgroundResource(R.drawable.ic_select1);
//                view.findViewById(R.id.tv_more_comment).setBackgroundResource(R.drawable.ic_select1);
                getVideoList();
                break;
            case R.id.tv_more_hot:
                mCurrentPage = 1;
                category_two = 2;
                rl_category_video.setRefreshing(true);
                view.findViewById(R.id.tv_more_new).setBackgroundResource(R.drawable.ic_select1);
                view.findViewById(R.id.tv_more_hot).setBackgroundResource(R.drawable.ic_select2);
//                view.findViewById(R.id.tv_more_comment).setBackgroundResource(R.drawable.ic_select1);
                getVideoList();
                break;
//            case R.id.tv_more_comment:
//                mCurrentPage = 1;
//                category_two = 3;
//                rl_category_video.setRefreshing(true);
//                view.findViewById(R.id.tv_more_new).setBackgroundResource(R.drawable.ic_select1);
//                view.findViewById(R.id.tv_more_hot).setBackgroundResource(R.drawable.ic_select1);
//                view.findViewById(R.id.tv_more_comment).setBackgroundResource(R.drawable.ic_select2);
//                getVideoList();
//                break;
 
            default:
                break;
        }
    }
}