CourseListFragment.java 10.6 KB
Newer Older
严久程 committed
1 2 3
package com.yidianling.course;


严久程 committed
4
import android.annotation.SuppressLint;
严久程 committed
5 6 7 8 9 10 11 12
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
严久程 committed
13 14 15 16 17 18 19
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydlcommon.base.BaseFragment;
import com.ydl.ydlcommon.bean.ShareData;
import com.ydl.ydlcommon.data.http.RxUtils;
import com.ydl.ydlcommon.utils.BuryPointUtils;
import com.ydl.ydlcommon.utils.NetworkParamsUtils;
严久程 committed
20 21
import com.yidianling.common.tools.LogUtil;
import com.yidianling.course.courseNew.LoadMoreView;
严久程 committed
22
import com.yidianling.course.net.CourseRetrofitUtils;
严久程 committed
23
import com.yidianling.course.bean.Course;
严久程 committed
24 25 26 27 28 29 30 31 32 33 34
import com.yidianling.course.router.CourseIn;
import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

严久程 committed
35 36 37 38 39 40
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

严久程 committed
41 42 43 44
/**
 * A simple {@link Fragment} subclass.
 * 咨询师课程列表页
 */
严久程 committed
45
public class CourseListFragment extends BaseFragment implements PtrHandler, LoadMoreHandler {
严久程 committed
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

    ListView lvContent;
    LoadMoreListViewContainer loadMoreListViewContainer;
    PtrFrameLayout storeHousePtrFrame;

    TextView emptyTv;
    ImageView ivEmptyView;


    private int page = 0;
    private boolean hasMore = true;
    private int tagId;
    private CourseListAdapter adapter;
    /**
     * 是否免费(默认付费)
     */
    private int childType = TYPE_PAY;
    /**
     * 1.免费 2.付费
     */
    private static final int TYPE_PAY = 2;
    private static final int TYPE_FREE = 1;
    private boolean isPrepared;

    public CourseListFragment() {
        // Required empty public constructor
    }

    public static CourseListFragment newInstance(int tagId) {
        Bundle args = new Bundle();
        args.putInt("tagId", tagId);
        CourseListFragment fragment = new CourseListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static CourseListFragment newInstance(int tagId, int childType) {
        Bundle args = new Bundle();
        args.putInt("tagId", tagId);
        args.putInt("childType", childType);
        CourseListFragment fragment = new CourseListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tagId = getArguments().getInt("tagId");
            childType = getArguments().getInt("childType");
        }
    }


    @Override
严久程 committed
102
    public int layoutResId() {
严久程 committed
103 104 105 106
        return R.layout.fragment_course_list;
    }

    @Override
严久程 committed
107 108 109 110 111 112 113 114 115
    public void initDataAndEvent() {
        lvContent = rootView.findViewById(R.id.lv_content);
        loadMoreListViewContainer = rootView.findViewById(R.id.load_more_list_view_container);
        storeHousePtrFrame = rootView.findViewById(R.id.store_house_ptr_frame);
        emptyTv = rootView.findViewById(R.id.tv_course_list_empty_prompt);
        ivEmptyView = rootView.findViewById(R.id.iv_course_list_empty_view);


        lvContent.setOnItemClickListener((parent, view, position, id) -> itemClick(view, position));
严久程 committed
116 117 118
    }

    @Override
严久程 committed
119 120
    public void initDataAndEventLazy() {
        init();
严久程 committed
121 122 123 124 125
    }

    private void init() {
        // header
        final MaterialHeader header = new MaterialHeader(getActivity());
严久程 committed
126
        int[] colors = getResources().getIntArray(R.array.course_colors);
严久程 committed
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
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        int padding = (int) getActivity().getResources().getDimension(R.dimen.default_dis_size);
        header.setPadding(0, padding, 0, padding);
        header.setPtrFrameLayout(storeHousePtrFrame);
        storeHousePtrFrame.setHeaderView(header);
        storeHousePtrFrame.setPtrHandler(this);
        storeHousePtrFrame.addPtrUIHandler(header);

        // 使用默认样式
//        loadMoreListViewContainer.useDefaultHeader();
        LoadMoreView footerView = new LoadMoreView(getContext());
        loadMoreListViewContainer.setLoadMoreView(footerView);
        loadMoreListViewContainer.setLoadMoreUIHandler(footerView);
        footerView.onLoading(loadMoreListViewContainer);
        loadMoreListViewContainer.setLoadMoreHandler(this);
        adapter = new CourseListAdapter(getActivity());
        lvContent.setAdapter(adapter);
        getData(false);
    }

    private void getData(boolean loadMore) {
        switch (tagId) {
            case 1:
            case 2:
                if (!CourseIn.INSTANCE.isLogin()) {
                    return;
                }
                break;
            default:
                break;
        }
        if (loadMore && !hasMore) {
            loadMoreListViewContainer.loadMoreFinish(false, false);
            return;
        }
        if (loadMore) {
            page++;
        } else {
            page = 1;
            hasMore = true;
        }
        getCourses(loadMore);
    }

严久程 committed
172
    @SuppressLint("CheckResult")
严久程 committed
173 174 175 176
    private void getCourses(final boolean loadMore) {
        StringBuffer sb = new StringBuffer();
        if (tagId >= 10) {
            sb.append("&page=").append(page).append("&listType=").append(tagId);
严久程 committed
177
            if (childType != -1) {
严久程 committed
178 179 180 181
                sb.append("&isFree=").append(childType);
            }
        } else {
            sb.append("&page=").append(page)
严久程 committed
182
                    .append("&listType=").append(tagId + 1);
严久程 committed
183
        }
严久程 committed
184
        String urlParams = NetworkParamsUtils.getCommonParam() + sb.toString();
严久程 committed
185
        CourseRetrofitUtils.getCourseList(urlParams).compose(RxUtils.resultJavaData()).subscribeOn(Schedulers.io())
严久程 committed
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
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    lvContent.setVisibility(View.VISIBLE);
                    emptyTv.setVisibility(View.GONE);
                    ivEmptyView.setVisibility(View.GONE);
                    if (loadMoreListViewContainer != null) {
                        loadMoreListViewContainer.loadMoreFinish(false, true);
                    }
                    try {
                        List<Course> courses = new ArrayList<>();
                        courses.addAll(resp.getList());
                        if (courses == null || courses.size() < CourseConstants.COURSE_PAGE_SIZE) {
                            hasMore = false;
                        }
                        if (!loadMore) {
                            if (courses != null) {
                                //更新缓存
                                adapter.setDataList(courses);
                                adapter.notifyDataSetChanged();
                            }
                        } else if (courses != null) {
                            adapter.addDataList(courses);
                            adapter.notifyDataSetChanged();
                        }
                        isShowEmptyView();

                    } catch (Exception e) {
                        LogUtil.e(e.toString());
                    }
                }, throwable -> {
                    if (throwable instanceof TimeoutException || throwable instanceof ConnectException || throwable instanceof UnknownHostException) {
                        if (adapter.mDataList == null || adapter.mDataList.size() <= 0) {
                            emptyTv.setVisibility(View.VISIBLE);
                            ivEmptyView.setVisibility(View.VISIBLE);
                            lvContent.setVisibility(View.GONE);
                            emptyTv.setText("网络不给力哦,下拉刷新重试");
严久程 committed
222
                            ivEmptyView.setImageResource(R.drawable.course_no_net);
严久程 committed
223
                        } else {
严久程 committed
224
                            CourseRetrofitUtils.handleError(getActivity(), throwable);
严久程 committed
225 226
                        }
                    } else {
严久程 committed
227
                        CourseRetrofitUtils.handleError(getActivity(), throwable);
严久程 committed
228 229 230 231 232 233 234 235 236 237
                        isShowEmptyView();
                    }
                });
    }

    private void isShowEmptyView() {
        if (adapter.mDataList == null || adapter.mDataList.size() <= 0) {
            emptyTv.setVisibility(View.VISIBLE);
            ivEmptyView.setVisibility(View.VISIBLE);
            lvContent.setVisibility(View.GONE);
严久程 committed
238
            ivEmptyView.setImageResource(R.drawable.course_blank);
严久程 committed
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
            emptyTv.setText("还没有相应的课程哦~");
        } else {
            lvContent.setVisibility(View.VISIBLE);
            emptyTv.setVisibility(View.GONE);
            ivEmptyView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, lvContent, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        frame.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (storeHousePtrFrame != null) {
                    storeHousePtrFrame.refreshComplete();
                }
            }
        }, 1800);
        getData(false);
    }


    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        getData(true);
    }

严久程 committed
271
    private void itemClick(View v, int position) {
严久程 committed
272 273 274
        Course course = adapter.getDataList().get(position);
        if (course != null) {

严久程 committed
275
            H5Params h5Params = new H5Params(CourseConstants.Companion.getCOURSE_DETAIL_H5() + course.id, "课程详情");
严久程 committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289
            if (null != course.share && !TextUtils.isEmpty(course.share.getShare_url())) {
                ShareData shareData = new ShareData(course.share.getShare_url(), course.share.getTitle(), course.share.getCover(), course.share.getDesc());
                h5Params.setShareData(shareData);
            }

            NewH5Activity.start(getActivity(), h5Params);
            BuryPointUtils.getInstance().createMap()
                    .put("curriculum_name", course.title == null ? "" : course.title)
                    .put("curriculum_ID", course.id)
                    .burryPoint("Curriculum_click");
        }
    }

}