CurriculumScheduleFragment.java 4.4 KB
package com.hh.xuetubao.fragment;


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.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.SharedPrefrenceUtils;
import com.hh.xuetubao.activity.study.CourseDetailActivity;
import com.hh.xuetubao.adapter.CurriculumScheduleRecAdapter;
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 java.util.ArrayList;
import java.util.List;

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

/**
 * 课程列表
 */
public class CurriculumScheduleFragment extends BaseMvpFragment<CommonPresenter, LearnModel> implements ICommonView, CurriculumScheduleRecAdapter.Turn, View.OnClickListener {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    Unbinder unbinder;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.tv_none)
    TextView tvNone;
    private CurriculumScheduleRecAdapter curriculumScheduleRecAdapter;
    private String tag;
    private List<FreeCourseBean.bean> list = new ArrayList<>();
    private String accountOid;
    private FragmentTransaction transaction;

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

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        tag = getTag();

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(manager);
        curriculumScheduleRecAdapter = new CurriculumScheduleRecAdapter(list, getActivity());
        recyclerview.setAdapter(curriculumScheduleRecAdapter);
        curriculumScheduleRecAdapter.setTurn(this);

        rlTop.setOnClickListener(this);
    }

    @Override
    public void initData() {
        LearnModel model = new LearnModel();
        accountOid = SharedPrefrenceUtils.getString(getActivity(), "AccountOid");
        model.getModel(this, 1, 2, accountOid, tag);
    }

    @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 == 2) {
            FreeCourseBean courseBean = (FreeCourseBean) o;
            if (courseBean.getSuccess()) {
                List<FreeCourseBean.bean> lessonLst = courseBean.getLessonLst();
                if (lessonLst != null)
                    list.addAll(lessonLst);
                curriculumScheduleRecAdapter.notifyDataSetChanged();

                tvNone.setVisibility(View.GONE);
            } else {
                tvNone.setVisibility(View.VISIBLE);
            }
        }
    }

    @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 turnToDetail(String lessonOid) {
        Intent intent = new Intent(getActivity(), CourseDetailActivity.class);
        intent.putExtra("lessonOid", lessonOid);
        startActivity(intent);
    }

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