MyCourseActivity.java 5.25 KB
Newer Older
严久程 committed
1 2 3 4 5
package com.yidianling.course.courseNew.mine;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
YKai committed
6 7 8 9 10
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
严久程 committed
11 12 13 14 15 16
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

严久程 committed
17
import com.ydl.ydlcommon.base.BaseActivity;
18
import com.ydl.ydlcommon.bean.StatusBarOptions;
严久程 committed
19
import com.ydl.ydlcommon.utils.DisplayUtils;
严久程 committed
20 21
import com.yidianling.course.CourseListFragment;
import com.yidianling.course.R;
严久程 committed
22 23
import com.yidianling.course.widget.CourseRedPacketView;
import com.yidianling.course.widget.SearchBarView;
严久程 committed
24

25 26
import org.jetbrains.annotations.NotNull;

严久程 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import java.lang.reflect.Field;

/**
 * Created by Jim on 2018/7/16 0016.
 * 心理课程
 */

public class MyCourseActivity extends BaseActivity {

    public final static String TAB_KEY = "tab_index";
    public final static int TAB_PAIED = 2;
    public final static int TAB_COLLECT = 1;

    private int tabIndex;

42 43 44 45 46
    @NotNull
    @Override
    public StatusBarOptions getStatusViewOptions() {
        return new StatusBarOptions(true,true);
    }
严久程 committed
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

    public static void start(Context context) {
        Intent i = new Intent(context, MyCourseActivity.class);
        i.putExtra(TAB_KEY, TAB_PAIED);
        context.startActivity(i);
    }

    public static void start(Context context, int index) {
        Intent i = new Intent(context, MyCourseActivity.class);
        i.putExtra(TAB_KEY, index);
        context.startActivity(i);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_my_course;
    }

    @Override
    protected void initDataAndEvent() {
        tabIndex = getIntent().getIntExtra(TAB_KEY, -1);
        TextView tv = findViewById(R.id.tv_title);
        tv.setText("我的课程");
        ImageView iv = findViewById(R.id.iv_back);
        findViewById(R.id.tv_left_menu).setVisibility(View.GONE);
        iv.setOnClickListener(view -> {
            finish();
        });
        SearchBarView searchBarView = findViewById(R.id.search_bar);
        searchBarView.initBarType(SearchBarView.JUMP_TO_SEARCH_COURSE, null);
        TabLayout tabLayout = findViewById(R.id.my_courses_tabs);
        ViewPager viewPager = findViewById(R.id.my_course_vp);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(new MyCourseFragmentAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(2);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 15, 15);
            }
        });

        //初始化倾诉红包
        initConfideRedPacketView();
    }

    private void initConfideRedPacketView() {
严久程 committed
94 95 96
        CourseRedPacketView courseRedPacketView = findViewById(R.id.courseRedPacketView);
        courseRedPacketView.initData(View.VISIBLE);
        courseRedPacketView.setConfide();
严久程 committed
97 98 99 100 101 102 103
    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            tabStrip = tabLayout.getClass().getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout llTab = null;
            try {
                llTab = (LinearLayout) tabStrip.get(tabs);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
            int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

            for (int i = 0; i < llTab.getChildCount(); i++) {
                View child = llTab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                params.leftMargin = DisplayUtils.dip2px(this, left);
                params.rightMargin = DisplayUtils.dip2px(this, right);
                child.setLayoutParams(params);
                child.invalidate();
            }
严久程 committed
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
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }


    }

    static class MyCourseFragmentAdapter extends FragmentPagerAdapter {

        private Fragment[] mFragments;
        private String[] titles = new String[]{"收藏", "已购"};

        public MyCourseFragmentAdapter(FragmentManager fm) {
            super(fm);
            mFragments = new Fragment[]{CourseListFragment.newInstance(2), CourseListFragment.newInstance(1)};
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
            } else {
            }

            return mFragments[position];
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}