StudyFragment.java 9.0 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.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hh.xuetubao.MainActivity;
import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.CustomScrollView;
import com.hh.xuetubao.Utils.RecyclerViewSpacesItemDecoration;
import com.hh.xuetubao.Utils.SharedPrefrenceUtils;
import com.hh.xuetubao.activity.study.CourseDetailActivity_Video;
import com.hh.xuetubao.adapter.StudyTimeXrcAdapter;
import com.hh.xuetubao.adapter.StudyXrecAdapter;
import com.hh.xuetubao.bean.LearningRecordDataBean;
import com.hh.xuetubao.bean.MyLearningInfoBean;
import com.hh.xuetubao.bean.UserBean;
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.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StudyFragment extends BaseMvpFragment<CommonPresenter, LearnModel> implements ICommonView, CustomScrollView.OnScrollChangeListener, XRecyclerView.LoadingListener, View.OnClickListener, StudyXrecAdapter.TurnTo {

    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    @BindView(R.id.rv_readTime)
    RecyclerView rvReadTime;
    @BindView(R.id.img_user)
    ImageView imgUser;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.today_study)
    TextView todayStudy;
    @BindView(R.id.duration_study)
    TextView durationStudy;
    @BindView(R.id.total_study)
    TextView totalStudy;
    @BindView(R.id.scroll)
    CustomScrollView scroll;
    @BindView(R.id.lin_back)
    LinearLayout linBack;
    @BindView(R.id.rl_user)
    RelativeLayout rlUser;
    @BindView(R.id.rl_body)
    RelativeLayout rlBody;
    private StudyXrecAdapter adapter;
    private StudyTimeXrcAdapter xrcAdapter;
    private int pageIndex = 1;
    private int pageSize = 10;
    private List<LearningRecordDataBean.learnBean> mlist = new ArrayList<>();
    private boolean isRefresh = false;
    private List<String> day = new ArrayList<>();
    private List<String> time = new ArrayList<>();
    private String resumeOid;

    List<Integer> arr = new ArrayList<>();

    int max;

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

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

        ButterKnife.bind(getActivity());

        resumeOid = SharedPrefrenceUtils.getString(getActivity(), "AccountOid");
        xrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new StudyXrecAdapter(getActivity(), mlist);
        xrecyclerview.setAdapter(adapter);
        xrecyclerview.setLoadingListener(this);
        adapter.setTurnTo(this);

//        scroll.setNestedScrollingEnabled(false);
        xrecyclerview.setNestedScrollingEnabled(false);
        scroll.setNestedScrollingEnabled(true);

//        int max = Collections.max(arr);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvReadTime.setLayoutManager(manager);
        xrcAdapter = new StudyTimeXrcAdapter(getActivity(), day, time, max);
        rvReadTime.setAdapter(xrcAdapter);

        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 25);//右间距
        rvReadTime.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));

        scroll.setOnScrollChangeListener(this);
        linBack.setOnClickListener(this);
        rlUser.setOnClickListener(this);
        imgUser.setOnClickListener(this);
        rlBody.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mPresenter.getPresenter(4, 1, resumeOid);
        mPresenter.getPresenter(4, 2, resumeOid, String.valueOf(pageIndex), String.valueOf(pageSize));
        mPresenter.getPresenter(4, 3, resumeOid);
    }

    @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 == 4 && intent == 1) {
            MyLearningInfoBean bean = (MyLearningInfoBean) o;
            if (bean.getUserName() != null)
                tvUser.setText(bean.getUserName());
            if (bean.getHeadPic() != null && bean.getHeadPic() != "") {
                Glide.with(getActivity()).load(bean.getHeadPic()).into(imgUser);
            } else {
                String headPic = SharedPrefrenceUtils.getString(getActivity(), "HeadPic");
                Glide.with(getActivity()).load(headPic).into(imgUser);
            }

            if (bean.getTodayDuration() != null)
                todayStudy.setText(bean.getTodayDuration());
            if (bean.getMaxDuration() != null)
                durationStudy.setText(bean.getMaxDuration());
            if (bean.getTotalDuration() != null)
                totalStudy.setText(bean.getTotalDuration());
        }
        if (api == 4 && intent == 2) {
            LearningRecordDataBean bean = (LearningRecordDataBean) o;
            List<LearningRecordDataBean.learnBean> recordLst = bean.getRecordLst();
            if (isRefresh) {
                mlist.clear();
                isRefresh = false;
            }
            mlist.addAll(recordLst);
            adapter.notifyDataSetChanged();
            xrecyclerview.loadMoreComplete();
            xrecyclerview.refreshComplete();
        }
        if (api == 4 && intent == 3) {
            UserBean bean = (UserBean) o;
            List<String> categories = bean.getCategories();
            List<String> datas = bean.getDatas();

            day.addAll(datas);
            time.addAll(categories);

            for (int i = 0; i < day.size(); i++) {
                arr.add(Integer.valueOf(day.get(i).substring(0, day.get(i).length() - 2)));
            }
            max = Collections.max(arr);
            Log.e("maxValue", max + "");
            xrcAdapter = new StudyTimeXrcAdapter(getActivity(), day, time, max);
            rvReadTime.setAdapter(xrcAdapter);
//            xrcAdapter.notifyDataSetChanged();

        }
    }

    @Override
    public void onComplete(int api) {

    }

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

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_back:
                getActivity().finish();
                break;
            case R.id.img_user:
            case R.id.rl_user:
            case R.id.rl_body:
//                startActivity(new Intent(getActivity(), InfromationActivity.class));
//                MineFragment mineFragment = new MineFragment();
//                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
//                transaction.replace(R.id.frame, mineFragment);
//                transaction.commit();

//                Intent intent = new Intent(getActivity(), MainActivity.class);
//                intent.putExtra("Intenttag","4");
//                startActivity(intent);

                break;
        }
    }

    @Override
    public void onScrollToStart() {
//        scroll.setNestedScrollingEnabled(false);
        scroll.setNestedScrollingEnabled(false);
        xrecyclerview.setNestedScrollingEnabled(true);
        Log.e("zhangtao", "到顶部了");
    }

    @Override
    public void onScrollToEnd() {
//        scroll.setNestedScrollingEnabled(false);
        pageIndex++;
        mPresenter.getPresenter(4, 2, resumeOid, String.valueOf(pageIndex), String.valueOf(pageSize));
        Log.e("zhangtao", "到底部了");
    }

    @Override
    public void turn(String LessonOid) {
        Intent intent = new Intent(getActivity(), CourseDetailActivity_Video.class);
        intent.putExtra("lessonOid", LessonOid);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        isRefresh = true;
        mPresenter.getPresenter(4, 2, resumeOid, String.valueOf(pageIndex), String.valueOf(pageSize));
//        scroll.setNestedScrollingEnabled(false);
        xrecyclerview.setNestedScrollingEnabled(false);
        scroll.setNestedScrollingEnabled(true);
    }

    @Override
    public void onLoadMore() {
        pageIndex++;
        mPresenter.getPresenter(4, 2, resumeOid, String.valueOf(pageIndex), String.valueOf(pageSize));
        scroll.setNestedScrollingEnabled(true);
    }
}