admin
2020-10-13 1bd61d859d83297f431ce1155f5a76fae03a86e4
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
package com.weikou.beibeivideo.ui.baidu;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;
 
import com.androidquery.AQuery;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.RetainViewFragment;
import com.viewpagerindicator.FoundTabPageIndicator;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.BeibeiVideoApplication;
import com.weikou.beibeivideo.ui.MyRetainViewFragment;
import com.weikou.beibeivideo.util.cache.DiskLruCacheManager;
import com.weikou.beibeivideo.util.downutil.StringUtils;
import com.yeshi.ec.library_baiducpu.R;
import com.yeshi.ec.library_baiducpu.entity.BaiDuCPUType;
import com.yeshi.ec.library_baiducpu.fragment.BaiDuCPUContentFragment;
import com.yeshi.ec.library_baiducpu.fragment.BaiDuShortVideosFragment;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 百度资讯
 */
public class BaiDuVideosFragment extends MyRetainViewFragment {
 
    private ViewPager viewPager;
    private ContentAdapter contentAdapter;
    private List<BaiDuCPUType> itemList = new ArrayList<>();
    private FoundTabPageIndicator indicator;
    private AQuery mAQuery;
 
    public static BaiDuVideosFragment newInstance() {
        return new BaiDuVideosFragment("百度视频");
    }
 
    public BaiDuVideosFragment(String name) {
        super(name);
    }
 
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_baidu_news_root;
    }
 
    private void initView(View contentView) {
        viewPager = contentView.findViewById(R.id.vp_news);
    }
 
    private String getCacheKey() {
        return StringUtils.MD5("getBaiDuNewsTypeList");
    }
 
    private void initData(View contentView) {
        mAQuery = new AQuery(contentView);
        //获取缓存
        String value = DiskLruCacheManager.getInstance(BeibeiVideoApplication.application).getCache(getCacheKey());
        if (!StringUtils.isNullOrEmpty(value)) {
            setData(value);
        }
    }
 
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        initView(contentView);
        initData(contentView);
        getTypeList();
    }
 
 
    /**
     * 获取类型列表
     */
    private void getTypeList() {
        BeibeiVideoAPI.getBaiDuVideoTypeList(getContext(), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optBoolean("IsPost")) {
                    String data = jsonObject.optJSONArray("Data").toString();
                    setData(data);
                    DiskLruCacheManager.getInstance(BeibeiVideoApplication.application).cache(getCacheKey(), data);
                } else {
                    Toast.makeText(getContext(), jsonObject.optString("Msg"), Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });
    }
 
    private synchronized void setData(String data) {
        List<BaiDuCPUType> list = new Gson().fromJson(data, new TypeToken<List<BaiDuCPUType>>() {
        }.getType());
        //数据一致就不必更新
        if (itemList != null) {
            String data1 = new Gson().toJson(itemList);
            String data2 = new Gson().toJson(list);
            if (data1.equalsIgnoreCase(data2)) {
                return;
            }
        }
        itemList.clear();
        itemList.addAll(list);
        setData();
    }
 
 
    public void setData() {
        contentAdapter = new BaiDuVideosFragment.ContentAdapter(
                getChildFragmentManager(), itemList);
        viewPager.setAdapter(contentAdapter);
        ((FoundTabPageIndicator) mAQuery.id(R.id.indicator).getView()).setViewPager(viewPager);
    }
 
    class ContentAdapter extends FragmentStatePagerAdapter {
        private FragmentManager fm;
 
        private List<BaiDuCPUType> mlist;
 
        public ContentAdapter(FragmentManager fm, List<BaiDuCPUType> list) {
            super(fm);
            this.fm = fm;
            this.mlist = list;
        }
 
        @Override
        public Fragment getItem(int position) {
            if (mlist.get(position).getId() == 1085)
                return BaiDuShortVideosFragment.newInstance(mlist.get(position));
            else
                return BaiDuCPUContentFragment.newInstance(mlist.get(position), mlist.get(position).getId() == 1085 ? BaiDuCPUContentFragment.VIEW_TYPE_SMALLVIDEO : BaiDuCPUContentFragment.VIEW_TYPE_VIDEO);
        }
 
        @Override
        public int getCount() {
            return mlist.size();
        }
 
 
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mlist.get(position).getName();
        }
    }
 
}