CourseFragment.java 5.8 KB
package com.hh.xuetubao.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.CustomScrollView;
import com.hh.xuetubao.activity.study.CourseDetailActivity_Video;
import com.hh.xuetubao.adapter.CourseXrecAdapter;
import com.hh.xuetubao.bean.FreeCourseBean;
import com.hh.xuetubao.model.LearnModel;
import com.hh.xuetubao.mvp.BaseMvpFragment;
import com.hh.xuetubao.mvp.CommonPresenter;
import com.hh.xuetubao.mvp.ICommonView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 课程中心
 */
public class CourseFragment extends BaseMvpFragment<CommonPresenter, LearnModel> implements ICommonView, XRecyclerView.LoadingListener, CustomScrollView.OnScrollChangeListener, View.OnClickListener, CourseXrecAdapter.Turn {

    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    @BindView(R.id.scroll)
    CustomScrollView scroll;
    @BindView(R.id.img_top)
    ImageView imgTop;
    private Unbinder unbinder;
    private FragmentTransaction transaction;
    private CertificationCourseFragment certificationCourseFragment;
    private List<FreeCourseBean.bean> xList = new ArrayList<>();
    private int pageSize = 10;
    private int pageNum = 0;
    private boolean isRefresh = false;
    private LearnModel model;
    private CourseXrecAdapter xrecAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_course;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

        xrecAdapter = new CourseXrecAdapter(xList, getActivity());
        xrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        xrecyclerview.setAdapter(xrecAdapter);
        xrecAdapter.setTurn(this);

        xrecyclerview.setNestedScrollingEnabled(false);
        xrecyclerview.setLoadingMoreEnabled(false);
        xrecyclerview.setPullRefreshEnabled(false);

        xrecyclerview.setLoadingListener(this);
        scroll.setOnScrollChangeListener(this);
        imgTop.setOnClickListener(this);

    }

    @Override
    public void initData() {
        model = new LearnModel();
        model.getModel(this, 1, 1, String.valueOf(pageNum), String.valueOf(pageSize));
    }

    @Override
    public CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    public LearnModel getModel() {
        return new LearnModel();
    }

    @Override
    public void getData(Object o, int api, int intent) {
        if (api == 1 && intent == 1) {
            FreeCourseBean freeCourseBean = (FreeCourseBean) o;
            List<FreeCourseBean.bean> lessonLst = freeCourseBean.getLessonLst();
            if (isRefresh) {
                xList.clear();
                isRefresh = false;
            }
            xList.addAll(lessonLst);
            xrecAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onComplete(int api) {
    }

    @Override
    public void onError(Throwable e, int api) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onScrollToStart() {
        Log.e("zhangtao", "到顶部了");
    }

    @SuppressLint("NewApi")
    @Override
    public void onScrollToEnd() {
        xrecyclerview.setPullRefreshEnabled(true);
        xrecyclerview.setLoadingMoreEnabled(true);
        xrecyclerview.setNestedScrollingEnabled(true);
        scroll.setNestedScrollingEnabled(false);
        Log.e("zhangtao", "到底部了");
    }

    @SuppressLint("NewApi")
    @Override
    public void onRefresh() {
        xrecyclerview.setPullRefreshEnabled(false);
        xrecyclerview.setLoadingMoreEnabled(false);
        xrecyclerview.setNestedScrollingEnabled(false);
        scroll.setNestedScrollingEnabled(true);
        isRefresh = true;
        model.getModel(this, 1, 1, String.valueOf(1), String.valueOf(pageSize));
        xrecyclerview.refreshComplete();
    }

    @SuppressLint("NewApi")
    @Override
    public void onLoadMore() {
        xrecyclerview.setPullRefreshEnabled(false);
        xrecyclerview.setLoadingMoreEnabled(false);
        xrecyclerview.setNestedScrollingEnabled(false);
        scroll.setNestedScrollingEnabled(true);
        pageNum++;
        model.getModel(this, 1, 1, String.valueOf(1), String.valueOf(pageSize));
        xrecyclerview.loadMoreComplete();
    }

    @Override
    public void onClick(View v) {
        transaction = getActivity().getSupportFragmentManager().beginTransaction();
        certificationCourseFragment = new CertificationCourseFragment();
        switch (v.getId()) {
            case R.id.img_top:
                transaction.replace(R.id.frame, certificationCourseFragment);
                transaction.commit();
                break;
        }
    }

    @Override
    public void turnTo(String lessonOid,String title) {
        Intent intent = new Intent(getActivity(), CourseDetailActivity_Video.class);  //CourseDetailActivity
        intent.putExtra("lessonOid", lessonOid);
        intent.putExtra("title",title);
        startActivity(intent);
    }
}