教育系统APP(七)

用一切抱怨的时间去努力
## 回顾上一期任务

判断题数
点击完选项之后,底部的文本控件要变换刚刚点击的题目的题号。
注意:在里面用到adapter与Activity的信息回调,具体过程参考

保存答题状态
当5道题都回答过一遍后,点击返回习题界面,习题下的文本标签要变成“已完成”。

教育系统APP(七)的发布-福利

解决上期任务

29

30

在ExercisesDetailActivity.java中添加

adapter.setOnItemListener(new ExercisesDetailListItemAdapter.OnItemListener() {
            @Override
            public void onItem(View view, int position) {
                ff=ff+1;
                tv_di.setText("第"+(position+1)+"题完成,共"+adapter.getItemCount()+"题");

                if (ff==5){
                    AnalysisUtils.saveExerciseStatus(ExercisesDetailActivity.this,id);
                    Log.i("DD",id+"");
                    setResult(RESULT_OK);
                    //ff = 0;
                }
            }

        });

别担心,添加完一定有红,是因为我们还有几个方法没写。

package cn.edu.gdmec.android.boxuegu.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.adapter.ExercisesDetailListItemAdapter;
import cn.edu.gdmec.android.boxuegu.bean.ExercisesBean;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;

public class ExercisesDetailActivity extends Activity {
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout title_bar;
    private TextView tv_save;
    private TextView tv_di;
    private int id;
    private String title;
    private RecyclerView rv_list;

    private List<ExercisesBean> ebl;
    private ExercisesDetailListItemAdapter adapter;
    //
    public  int ff = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercises_detail);
        id = getIntent().getIntExtra("id",0);
        title = getIntent().getStringExtra("title");
        ebl = new ArrayList<ExercisesBean>();
        initData();
        initView();
    }

    private void initData() {
        //从xml文件中获取习题数据
        try {
            InputStream is = getResources().getAssets().open("chapter" + id + ".xml");
            ebl = AnalysisUtils.getExercisesInfos(is);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        tv_save = findViewById(R.id.tv_save);
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText(title);
        title_bar = findViewById(R.id.title_bar);
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        //
        tv_di = findViewById(R.id.tv_di);
        rv_list = findViewById(R.id.rv_list);
        rv_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExercisesDetailActivity.this.finish();
            }
        });
        adapter = new ExercisesDetailListItemAdapter(ExercisesDetailActivity.this,
                new ExercisesDetailListItemAdapter.OnSelectListener() {
                    @Override
                    public void onSelectA(int position, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d) {
                        //判断如果答案不是1即A选项
                        if (ebl.get(position).answer != 1) {
                            ebl.get(position).select = 1;
                        } else {
                            ebl.get(position).select = 0;
                        }
                        switch (ebl.get(position).answer) {
                            case 1:
                                iv_a.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 2:
                                iv_a.setImageResource(R.drawable.exercises_error_icon);
                                iv_b.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 3:
                                iv_a.setImageResource(R.drawable.exercises_error_icon);
                                iv_c.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 4:
                                iv_a.setImageResource(R.drawable.exercises_error_icon);
                                iv_d.setImageResource(R.drawable.exercises_right_icon);
                                break;
                        }
                        AnalysisUtils.setABCDEnable(false, iv_a, iv_b, iv_c, iv_d);
                    }

                    @Override
                    public void onSelectB(int position, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d) {
                        //判断如果答案不是2即B选项
                        if (ebl.get(position).answer != 2) {
                            ebl.get(position).select = 2;
                        } else {
                            ebl.get(position).select = 0;
                        }
                        switch (ebl.get(position).answer) {
                            case 1:
                                iv_a.setImageResource(R.drawable.exercises_right_icon);
                                iv_b.setImageResource(R.drawable.exercises_error_icon);
                                break;
                            case 2:
                                iv_b.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 3:
                                iv_b.setImageResource(R.drawable.exercises_error_icon);
                                iv_c.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 4:
                                iv_b.setImageResource(R.drawable.exercises_error_icon);
                                iv_d.setImageResource(R.drawable.exercises_right_icon);
                                break;
                        }
                        AnalysisUtils.setABCDEnable(false, iv_a, iv_b, iv_c, iv_d);
                    }

                    @Override
                    public void onSelectC(int position, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d) {
                        //判断如果答案不是3即C选项
                        if (ebl.get(position).answer != 3) {
                            ebl.get(position).select = 3;
                        } else {
                            ebl.get(position).select = 0;
                        }
                        switch (ebl.get(position).answer) {
                            case 1:
                                iv_a.setImageResource(R.drawable.exercises_right_icon);
                                iv_c.setImageResource(R.drawable.exercises_error_icon);
                                break;
                            case 2:
                                iv_b.setImageResource(R.drawable.exercises_right_icon);
                                iv_c.setImageResource(R.drawable.exercises_error_icon);
                                break;
                            case 3:
                                iv_c.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 4:
                                iv_c.setImageResource(R.drawable.exercises_error_icon);
                                iv_d.setImageResource(R.drawable.exercises_right_icon);
                                break;
                        }
                        AnalysisUtils.setABCDEnable(false, iv_a, iv_b, iv_c, iv_d);
                    }

                    @Override
                    public void onSelectD(int position, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d) {
                        //判断如果答案不是4即D选项
                        if (ebl.get(position).answer != 4) {
                            ebl.get(position).select = 4;
                        } else {
                            ebl.get(position).select = 0;
                        }
                        switch (ebl.get(position).answer) {
                            case 1:
                                iv_a.setImageResource(R.drawable.exercises_right_icon);
                                iv_d.setImageResource(R.drawable.exercises_error_icon);
                                break;
                            case 2:
                                iv_d.setImageResource(R.drawable.exercises_error_icon);
                                iv_b.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 3:
                                iv_d.setImageResource(R.drawable.exercises_error_icon);
                                iv_c.setImageResource(R.drawable.exercises_right_icon);
                                break;
                            case 4:
                                iv_d.setImageResource(R.drawable.exercises_right_icon);
                                break;
                        }
                        AnalysisUtils.setABCDEnable(false, iv_a, iv_b, iv_c, iv_d);
                    }
                });
        adapter.setData(ebl);
        adapter.setOnItemListener(new ExercisesDetailListItemAdapter.OnItemListener() {
            @Override
            public void onItem(View view, int position) {
                ff=ff+1;
                tv_di.setText("第"+(position+1)+"题完成,共"+adapter.getItemCount()+"题");

                if (ff==5){
                    AnalysisUtils.saveExerciseStatus(ExercisesDetailActivity.this,id);
                    Log.i("DD",id+"");
                    setResult(RESULT_OK);
                    //ff = 0;
                }
            }

        });
        rv_list.setAdapter(adapter);
    }
}

ExercisesDetailListItemAdapter.java 中添加代码

package cn.edu.gdmec.android.boxuegu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.bean.ExercisesBean;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;

public class ExercisesDetailListItemAdapter extends
        RecyclerView.Adapter<ExercisesDetailListItemAdapter.ViewHolder> {
    private List<ExercisesBean> objects = new ArrayList<ExercisesBean>();
    private ArrayList<String> selectedPosition = new ArrayList<String>();
    private Context context;
    private LayoutInflater layoutInflater;
    private OnSelectListener onSelectListener;
    private OnItemListener onItemListener;

    public ExercisesDetailListItemAdapter(Context context, OnSelectListener onSelectListener) {
        this.context = context;
        this.onSelectListener = onSelectListener;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.exercises_detail_list_item, parent,
                false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        initializeViews(objects.get(position), holder, position);
    }

    public void setData(List<ExercisesBean> objects) {
        this.objects = objects;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    private void initializeViews(ExercisesBean object, final ViewHolder holder,
                                 final int position) {
        ExercisesBean bean = object;
        if (bean != null) {
            holder.tvSubject.setText(bean.subject);
            holder.tvA.setText(bean.a);
            holder.tvB.setText(bean.b);
            holder.tvC.setText(bean.c);
            holder.tvD.setText(bean.d);
        }
        if (!selectedPosition.contains("" + position)) {
            holder.ivA.setImageResource(R.drawable.exercises_a);
            holder.ivB.setImageResource(R.drawable.exercises_b);
            holder.ivC.setImageResource(R.drawable.exercises_c);
            holder.ivD.setImageResource(R.drawable.exercises_d);
            AnalysisUtils.setABCDEnable(true, holder.ivA, holder.ivB, holder.ivC,
                    holder.ivD);
        } else {
            AnalysisUtils.setABCDEnable(false, holder.ivA, holder.ivB, holder.ivC,
                    holder.ivD);
            switch (bean.select) {
                case 0:
                    if (bean.answer == 1) {
                        holder.ivA.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 2) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 3) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 4) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 1:
                    holder.ivA.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 2) {
                        holder.ivB.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 3) {
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 4) {
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 2:
                    holder.ivB.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1) {
                        holder.ivA.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 3) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivC.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 4) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                        holder.ivD.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 3:
                    holder.ivC.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1) {
                        holder.ivA.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 2) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivD.setImageResource(R.drawable.exercises_d);
                    } else if (bean.answer == 4) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivD.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;
                case 4:
                    holder.ivD.setImageResource(R.drawable.exercises_error_icon);
                    if (bean.answer == 1) {
                        holder.ivA.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                    } else if (bean.answer == 2) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_right_icon);
                        holder.ivC.setImageResource(R.drawable.exercises_c);
                    } else if (bean.answer == 3) {
                        holder.ivA.setImageResource(R.drawable.exercises_a);
                        holder.ivB.setImageResource(R.drawable.exercises_b);
                        holder.ivC.setImageResource(R.drawable.exercises_right_icon);
                    }
                    break;

            }

        }
        holder.ivA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener!=null){
                    onItemListener.onItem(v,position);
                }
                if (selectedPosition.contains("" + position)) {
                    selectedPosition.remove("" + position);
                } else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectA(position, holder.ivA,
                        holder.ivB, holder.ivC, holder.ivD);
            }
        });
        holder.ivB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener!=null){
                    onItemListener.onItem(v,position);
                }
                if (selectedPosition.contains("" + position)) {
                    selectedPosition.remove("" + position);
                } else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectB(position, holder.ivA,
                        holder.ivB, holder.ivC, holder.ivD);
            }
        });
        holder.ivC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener!=null){
                    onItemListener.onItem(v,position);
                }
                if (selectedPosition.contains("" + position)) {
                    selectedPosition.remove("" + position);
                } else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectC(position, holder.ivA,
                        holder.ivB, holder.ivC, holder.ivD);
            }
        });
        holder.ivD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener!=null){
                    onItemListener.onItem(v,position);
                }
                if (selectedPosition.contains("" + position)) {
                    selectedPosition.remove("" + position);
                } else {
                    selectedPosition.add(position + "");
                }
                onSelectListener.onSelectD(position, holder.ivA,
                        holder.ivB, holder.ivC, holder.ivD);
            }
        });

    }


    protected class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvSubject;
        private ImageView ivA;
        private TextView tvA;
        private ImageView ivB;
        private TextView tvB;
        private ImageView ivC;
        private TextView tvC;
        private ImageView ivD;
        private TextView tvD;

        public ViewHolder(View view) {
            super(view);
            tvSubject = (TextView) view.findViewById(R.id.tv_subject);
            ivA = (ImageView) view.findViewById(R.id.iv_a);
            tvA = (TextView) view.findViewById(R.id.tv_a);
            ivB = (ImageView) view.findViewById(R.id.iv_b);
            tvB = (TextView) view.findViewById(R.id.tv_b);
            ivC = (ImageView) view.findViewById(R.id.iv_c);
            tvC = (TextView) view.findViewById(R.id.tv_c);
            ivD = (ImageView) view.findViewById(R.id.iv_d);
            tvD = (TextView) view.findViewById(R.id.tv_d);
        }
    }

    public interface OnSelectListener {
        void onSelectA(int position, ImageView iv_a,
                       ImageView iv_b, ImageView iv_c,
                       ImageView iv_d);

        void onSelectB(int position, ImageView iv_a,
                       ImageView iv_b, ImageView iv_c,
                       ImageView iv_d);

        void onSelectC(int position, ImageView iv_a,
                       ImageView iv_b, ImageView iv_c,
                       ImageView iv_d);

        void onSelectD(int position, ImageView iv_a,
                       ImageView iv_b, ImageView iv_c,
                       ImageView iv_d);
    }
    public interface OnItemListener{
        void onItem(View view,int position);
    }
    public void setOnItemListener(OnItemListener onItemListener){
        this.onItemListener=onItemListener;
    }
}

ExercisesListItemAdapter.java

package cn.edu.gdmec.android.boxuegu.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

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

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.activity.ExercisesDetailActivity;
import cn.edu.gdmec.android.boxuegu.bean.ExercisesBean;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;

public class ExercisesListItemAdapter extends BaseAdapter {

    private List<ExercisesBean> objects = new ArrayList<ExercisesBean>();
    private Context context;
    private LayoutInflater layoutInflater;

    public ExercisesListItemAdapter(Context context){
        this.context=context;
        this.layoutInflater=LayoutInflater.from(context);
    }
    /**
     * 设置数据更新界面
     */
    public void setData(List<ExercisesBean> objects){
        this.objects = objects;
        notifyDataSetChanged();
    }
    //
    public void updateView(List<ExercisesBean> objects){
        this.objects = objects;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return objects == null ? 0 : objects.size();
    }

    /**
     * 根据position得到对应的Item的对象
     */
    @Override
    public ExercisesBean getItem(int position) {
        return objects.get(position);
    }

    /**
     * 根据position得到对应Item的对象
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 得到相应position对应的Item视图,position是当前Item的位置
     * convertView参数就是滚出屏幕的Item的View
     * 第一次进入或滑动屏幕时候被调用
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.exercises_list_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((ExercisesBean) getItem(position), (ViewHolder) convertView.getTag(),
                position, convertView);
        return convertView;
    }

    private void initializeViews(ExercisesBean object, ViewHolder holder,
                                 int position, View convertView) {
        final ExercisesBean bean = getItem(position);
        if (bean != null) {
            holder.tvOrder.setText(position + 1 + "");
            holder.tvTitle.setText(bean.title);
            Log.i("readExercises",AnalysisUtils.readExerciseStatus(context,position+1)+"");
            if (AnalysisUtils.readExerciseStatus(context,position+1)){
                holder.tvContent.setText("已完成");
            }else{
                holder.tvContent.setText(bean.content);
            }
            holder.tvOrder.setBackgroundResource(bean.background);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean == null) {
                        return;
                    }
                    //跳转到习题界面
                    Intent intent = new Intent(context, ExercisesDetailActivity.class);
                    intent.putExtra("id", bean.id);
                    intent.putExtra("title", bean.title);
                    ((Activity) context).startActivityForResult(intent, 000);
                }
            });
        }
    }

    protected class ViewHolder {
        private TextView tvOrder;
        private TextView tvTitle;
        private TextView tvContent;

        public ViewHolder(View view) {
            tvOrder = (TextView) view.findViewById(R.id.tv_order);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
        }
    }
}

AnalysisUtils.java 中添加代码

    public static boolean readExerciseStatus(Context context,int i){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isDone = sharedPreferences.getBoolean("isDone"+i, false);
        return isDone;
    }
    public static void saveExerciseStatus(Context context,int i){
        SharedPreferences sp = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isDone"+i, true);
        editor.commit();
    }
package cn.edu.gdmec.android.boxuegu.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Xml;
import android.widget.ImageView;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.boxuegu.bean.ExercisesBean;

public class AnalysisUtils {
    //读取用户名
    public static String readLoginUserName(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        String userName=sharedPreferences.getString("loginUserName","");
        return userName;
    }

    //读取登录状态
    public static boolean readLoginStatus(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
        return isLogin;
    }

    //清除登录状态
    public static void cleanLoginStatus(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isLogin",false);
        editor.putString("loginUserName","");
        editor.commit();
    }
    public static boolean readExerciseStatus(Context context,int i){
        SharedPreferences sharedPreferences = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isDone = sharedPreferences.getBoolean("isDone"+i, false);
        return isDone;
    }
    public static void saveExerciseStatus(Context context,int i){
        SharedPreferences sp = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isDone"+i, true);
        editor.commit();
    }

    public static List<ExercisesBean> getExercisesInfos(InputStream is) throws Exception{
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is,"utf-8");
        List<ExercisesBean> exercisesInfos=null;
        ExercisesBean exercisesInfo=null;
        int type = parser.getEventType();
        while (type!=XmlPullParser.END_DOCUMENT){
            switch (type){
                case XmlPullParser.START_TAG:
                    if ("infos".equals(parser.getName())){
                        exercisesInfos=new ArrayList<ExercisesBean>();
                    }else if ("exercises".equals(parser.getName())){
                        exercisesInfo=new ExercisesBean();
                        String ids=parser.getAttributeValue(0);
                        exercisesInfo.subjectId = Integer.parseInt(ids);
                    }else if ("subject".equals(parser.getName())){
                        String subject=parser.nextText();
                        exercisesInfo.subject = subject;
                    }else if ("a".equals(parser.getName())){
                        String a=parser.nextText();
                        exercisesInfo.a = a;
                    }else if ("b".equals(parser.getName())){
                        String b=parser.nextText();
                        exercisesInfo.b = b;
                    }else if ("c".equals(parser.getName())){
                        String c=parser.nextText();
                        exercisesInfo.c = c;
                    }else if ("d".equals(parser.getName())){
                        String d=parser.nextText();
                        exercisesInfo.d = d;
                    }else if ("answer".equals(parser.getName())){
                        String answer=parser.nextText();
                        exercisesInfo.answer = Integer.parseInt(answer);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("exercises".equals(parser.getName())){
                        exercisesInfos.add(exercisesInfo);
                        exercisesInfo=null;
                    }
                    break;
            }
            type=parser.next();
        }
        return exercisesInfos;
    }
    public static void setABCDEnable(boolean value, ImageView iv_a, ImageView iv_b, ImageView iv_c, ImageView iv_d){
        iv_a.setEnabled(value);
        iv_b.setEnabled(value);
        iv_c.setEnabled(value);
        iv_d.setEnabled(value);
    }
}

Ok,大功告成!

教育系统APP(七)

掌握课程模块的开发,实现用ViewPager完成广告轮播图的展示
掌握课程模块的开发,实现用RecyclerView完成课程列表的展示

项目实操

课程界面-广告轮播图
课程界面-课程列表

任务实施

课程模块主要用于展示课程中的视频信息,当点击课程列表时会跳转到课程详情界面,在该界面中可以播放相应章节的视频。
同时为了方便用户查看已学习的视频,还在“我”的界面中添加了一个播放记录。本章将对课程界面进行详细讲解。

课程界面-广告轮播图

广告布局
导入图片文件

34

广告轮播图底部有三个可变色的小圆点,我们先把小圆点的选择器给做了。
在drawable文件夹下分别创建indicator_on.xml和indecator_off.xml。

indicator_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="6dp" android:width="6dp"></size>
    <solid android:color="#00ABF8"></solid>
</shape>

indicator_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="6dp" android:width="6dp"></size>
    <solid android:color="#737373"></solid>
</shape>

新建一个布局文件,命名为main_adbanner.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_adBanner"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="#eeeeee">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp_advertBanner"
        android:background="@drawable/default_img"
        android:gravity="center"
        android:layout_marginBottom="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">
    </android.support.v4.view.ViewPager>

        <LinearLayout
            android:gravity="center_horizontal"
            android:layout_alignParentBottom="true"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <View
                android:background="@drawable/indicator_off"
                android:layout_marginLeft="5dp"
                android:id="@+id/dots_1"
                android:layout_width="5dp"
                android:layout_height="5dp"/>
            <View
                android:background="@drawable/indicator_off"
                android:layout_marginLeft="5dp"
                android:id="@+id/dots_2"
                android:layout_width="5dp"
                android:layout_height="5dp"/>
            <View
                android:background="@drawable/indicator_off"
                android:layout_marginLeft="5dp"
                android:id="@+id/dots_3"
                android:layout_width="5dp"
                android:layout_height="5dp"/>

        </LinearLayout>

</RelativeLayout>

课程界面布局
写完了轮播图的布局,我们来完成课程页面的布局。
修改fragment_course.xml。
fragment_course.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white">

    <include layout="@layout/main_adbanner"></include>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/course_intro_icon"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:text="Android 基础教程1-10章视频"
            android:textSize="16sp"
            android:textColor="@android:color/black"
            android:textStyle="bold"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="#E4E4E4">
    </View>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="55dp"
        android:scrollbars="none">
        <!--android:divider="@null"
        android:id="@+id/lv_list"
        android:dividerHeight="0dp"-->
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

ViewPager
先给ViewPager写一个适配器:
在Adapter文件夹下新建一个java文件,命名为ADViewPagerAdapter.java。
ADViewPagerAdapter.java

package cn.edu.gdmec.android.boxuegu.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

public class ADViewPagerAdapter extends PagerAdapter {
    private Context context;
    private List<ImageView> list;

    public ADViewPagerAdapter(Context context, List<ImageView> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView(list.get(position));
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position){
        container.addView(list.get(position));
        return list.get(position);
    }
}

接下来是CourseFragment.java
CourseFragment.java

package cn.edu.gdmec.android.boxuegu.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

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

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.adapter.ADViewPagerAdapter;

public class CourseFragment extends Fragment {
    private ViewPager vp_advertBanner;
    private View dots_1;
    private View dots_2;
    private View dots_3;
    private RelativeLayout rl_adBanner;
    private RecyclerView rv_list;

    public static final int MSG_AD_SLID = 002;
    private List<ImageView> viewList;
    private ADViewPagerAdapter viewPagerAdapter;
    private List<View> dots;
    private int oldPoints=0;
    private  Thread thread;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_AD_SLID:
                    if (viewPagerAdapter.getCount() > 0) {
                        if (vp_advertBanner.getCurrentItem() == viewList.size() - 1) {
                            vp_advertBanner.setCurrentItem(0);
                        } else {
                            vp_advertBanner.setCurrentItem(vp_advertBanner.getCurrentItem() + 1);
                        }

                    }
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_course, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setViewPager();
    }

    private void setViewPager(){
        thread=new Thread(){
            @Override
            public void run(){
                super.run();
                while(true){
                    try{
                        sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(MSG_AD_SLID);
                }
            }
        };
        thread.start();
    }

    private void initView(View view ){

        vp_advertBanner=view.findViewById(R.id.vp_advertBanner);
        rl_adBanner=view.findViewById(R.id.rl_adBanner);

        viewList = new ArrayList<>();
        ImageView imageView1=new ImageView(getActivity());
        ImageView imageView2=new ImageView(getActivity());
        ImageView imageView3=new ImageView(getActivity());

        imageView1.setImageResource(R.drawable.banner_1);
        imageView2.setImageResource(R.drawable.banner_2);
        imageView3.setImageResource(R.drawable.banner_3);

        viewList.add(imageView1);
        viewList.add(imageView2);
        viewList.add(imageView3);
        viewPagerAdapter = new ADViewPagerAdapter(getActivity(),viewList);
        vp_advertBanner.setAdapter(viewPagerAdapter);

        //圆点
        dots = new ArrayList<View>();
        dots_1 = (View)view.findViewById(R.id.dots_1);
        dots_2 = (View)view.findViewById(R.id.dots_2);
        dots_3 = (View)view.findViewById(R.id.dots_3);

        dots.add(dots_1);
        dots.add(dots_2);
        dots.add(dots_3);

        //第一个圆点设为黑色
        dots.get(0).setBackgroundResource(R.drawable.indicator_on);
        vp_advertBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                dots.get(oldPoints).setBackgroundResource(R.drawable.indicator_off);
                dots.get(position).setBackgroundResource(R.drawable.indicator_on);
                oldPoints=position;
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        resetSize();
    }

    private void resetSize(){
        int sw = getScreenWidth(getActivity());
        int adLheight = sw / 2;
        ViewGroup.LayoutParams adlp = rl_adBanner.getLayoutParams();
        adlp.width = sw;
        adlp.height = adLheight;
        rl_adBanner.setLayoutParams(adlp);
    }
    private int getScreenWidth(Activity mContext){
        DisplayMetrics metrics = new DisplayMetrics();
        Display display = mContext.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        return metrics.widthPixels;
    }
}

课程界面-课程列表

课程列表item
新建布局文件,命名为course_list_item.xml。

course_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="115dp">

            <ImageView
                android:id="@+id/iv_course_img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="4dp"
                android:paddingLeft="8dp"
                android:paddingRight="4dp"
                android:paddingTop="8dp"
                android:src="@drawable/chapter_1_icon"/>
            <TextView
                android:id="@+id/tv_course_img_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="6dp"
                android:background="#30000000"
                android:paddingBottom="2dp"
                android:paddingRight="5dp"
                android:paddingLeft="5dp"
                android:paddingTop="2dp"
                android:text="Android 基础入门"
                android:textSize="12sp"
                android:textColor="@android:color/white"/>
        </RelativeLayout>
        <TextView
            android:id="@+id/tv_course_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Android 基础视频第一章"
            android:textColor="@android:color/black"
            android:textSize="14sp"/>
</LinearLayout>

属性类

在Bean包下新建一个Java文件,命名为CourseBean.java

CourseBean.java

public class CourseBean {
    public int id;
    public String imgTitle;
    public String title;
    public String intro;
    public String icon;
}

课程item的适配器

右键course_list_item.xml,用Genrate Code生成adapter文件,把它放进adapter包下。
CourseListItemAdapter.java

package cn.edu.gdmec.android.boxuegu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.bean.CourseBean;

public class CourseListItemAdapter extends RecyclerView.Adapter<CourseListItemAdapter.ViewHolder> {

    private List<CourseBean> objects = new ArrayList<CourseBean>();
    private Context context;
    private LayoutInflater layoutInflater;

    public CourseListItemAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view=inflater.inflate(R.layout.course_list_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        initializeViews(objects.get(position), holder);
    }

    public void setData(List<CourseBean> objects){
        this.objects=objects;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    private void initializeViews(CourseBean object, ViewHolder holder) {
        if (object != null){
            holder.tvCourseImgTitle.setText(object.imgTitle);
            holder.tvCourseTitle.setText(object.title);
            switch (object.id){
                case 1:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_1_icon);
                    break;
                case 2:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_2_icon);
                    break;
                case 3:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_3_icon);
                    break;
                case 4:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_4_icon);
                    break;
                case 5:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_5_icon);
                    break;
                case 6:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_6_icon);
                    break;
                case 7:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_7_icon);
                    break;
                case 8:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_8_icon);
                    break;
                case 9:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_9_icon);
                    break;
                case 10:
                    holder.ivCourseImg.setImageResource(R.drawable.chapter_10_icon);
                    break;
            }
        }
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivCourseImg;
        private TextView tvCourseImgTitle;
        private TextView tvCourseTitle;

        public ViewHolder(View view) {
            super(view);
            ivCourseImg = (ImageView) view.findViewById(R.id.iv_course_img);
            tvCourseImgTitle = (TextView) view.findViewById(R.id.tv_course_img_title);
            tvCourseTitle = (TextView) view.findViewById(R.id.tv_course_title);
        }
    }
}

课程数据

在assets文件夹中放入chaptertitle.xml,显然之前放过了
到Utils包下的AnalysisUtils.java中添加解析课程数据的方法。

AnalysisUtils.java

 public static List<CourseBean> getCourseInfos(InputStream is) throws  Exception{
        XmlPullParser parser=Xml.newPullParser();
        parser.setInput(is,"utf-8");
        List<CourseBean> courseList=null;
        CourseBean courseInfo=null;
        int type=parser.getEventType();
        while(type !=XmlPullParser.END_DOCUMENT){
            switch (type){
                case XmlPullParser.START_TAG:
                    if ("infos".equals(parser.getName())){
                        courseList=new ArrayList<CourseBean>();
                    }else if("course".equals(parser.getName())){
                        courseInfo=new CourseBean();
                        String ids=parser.getAttributeValue(0);
                        courseInfo.id=Integer.parseInt(ids);

                    }
                    else if("imgtitle".equals(parser.getName())){

                        String imgtitle=parser.nextText();
                        courseInfo.imgTitle=imgtitle;

                    }
                    else if("title".equals(parser.getName())){
                        String title=parser.nextText();
                        courseInfo.title=title;

                    }
                    else if("intro".equals(parser.getName())){
                        String intro=parser.nextText();
                        courseInfo.intro=intro;

                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("course".equals(parser.getName())){
                        courseList.add(courseInfo);
                        courseInfo=null;
                    }
                    break;
            }
            type=parser.next();

        }
        return courseList;
    }

课程列表逻辑代码
回到CourseFragment.java,把获取数据和recyclerView的逻辑代码补上

CourseFragment.java

package cn.edu.gdmec.android.boxuegu.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.edu.gdmec.android.boxuegu.R;
import cn.edu.gdmec.android.boxuegu.adapter.ADViewPagerAdapter;
import cn.edu.gdmec.android.boxuegu.adapter.CourseListItemAdapter;
import cn.edu.gdmec.android.boxuegu.bean.CourseBean;
import cn.edu.gdmec.android.boxuegu.utils.AnalysisUtils;

public class CourseFragment extends Fragment {
    private ViewPager vp_advertBanner;
    private View dots_1;
    private View dots_2;
    private View dots_3;
    private RelativeLayout rl_adBanner;
    private RecyclerView rv_list;

    public static final int MSG_AD_SLID = 002;
    private List<ImageView> viewList;
    private ADViewPagerAdapter viewPagerAdapter;
    private List<View> dots;
    private int oldPoints=0;
    private  Thread thread;
    private List<CourseBean> rList;
    private CourseListItemAdapter adapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_AD_SLID:
                    if (viewPagerAdapter.getCount() > 0) {
                        if (vp_advertBanner.getCurrentItem() == viewList.size() - 1) {
                            vp_advertBanner.setCurrentItem(0);
                        } else {
                            vp_advertBanner.setCurrentItem(vp_advertBanner.getCurrentItem() + 1);
                        }

                    }
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_course, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        getCourseData();
        initView(view);
        setViewPager();
    }

    private void getCourseData(){
        try {
            InputStream is = getActivity().getResources().getAssets().open("chaptertitle.xml");
            rList = AnalysisUtils.getCourseInfos(is);
        }catch (IOException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setViewPager(){
        thread=new Thread(){
            @Override
            public void run(){
                super.run();
                while(true){
                    try{
                        sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(MSG_AD_SLID);
                }
            }
        };
        thread.start();
    }

    private void initView(View view ){
        rv_list=view.findViewById(R.id.rv_list);
        adapter=new CourseListItemAdapter(getActivity());
        adapter.setData(rList);
        rv_list.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rv_list.setAdapter(adapter);

        vp_advertBanner=view.findViewById(R.id.vp_advertBanner);
        rl_adBanner=view.findViewById(R.id.rl_adBanner);

        viewList = new ArrayList<>();
        ImageView imageView1=new ImageView(getActivity());
        ImageView imageView2=new ImageView(getActivity());
        ImageView imageView3=new ImageView(getActivity());

        imageView1.setImageResource(R.drawable.banner_1);
        imageView2.setImageResource(R.drawable.banner_2);
        imageView3.setImageResource(R.drawable.banner_3);

        viewList.add(imageView1);
        viewList.add(imageView2);
        viewList.add(imageView3);
        viewPagerAdapter = new ADViewPagerAdapter(getActivity(),viewList);
        vp_advertBanner.setAdapter(viewPagerAdapter);

        //圆点
        dots = new ArrayList<View>();
        dots_1 = (View)view.findViewById(R.id.dots_1);
        dots_2 = (View)view.findViewById(R.id.dots_2);
        dots_3 = (View)view.findViewById(R.id.dots_3);

        dots.add(dots_1);
        dots.add(dots_2);
        dots.add(dots_3);

        //第一个圆点设为黑色
        dots.get(0).setBackgroundResource(R.drawable.indicator_on);
        vp_advertBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                dots.get(oldPoints).setBackgroundResource(R.drawable.indicator_off);
                dots.get(position).setBackgroundResource(R.drawable.indicator_on);
                oldPoints=position;
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        resetSize();
    }

    private void resetSize(){
        int sw = getScreenWidth(getActivity());
        int adLheight = sw / 2;
        ViewGroup.LayoutParams adlp = rl_adBanner.getLayoutParams();
        adlp.width = sw;
        adlp.height = adLheight;
        rl_adBanner.setLayoutParams(adlp);
    }
    private int getScreenWidth(Activity mContext){
        DisplayMetrics metrics = new DisplayMetrics();
        Display display = mContext.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        return metrics.widthPixels;
    }
}

补充完成后,大功告成!

任务实施-福利

把课程列表显示从2列变为3列

由于任务过简单,就告诉你们~
CourseFragment.java,其实你们会发现运行完就变3列了,那么是哪里改的呢?

rv_list=view.findViewById(R.id.rv_list);
        adapter=new CourseListItemAdapter(getActivity());
        adapter.setData(rList);
        rv_list.setLayoutManager(new GridLayoutManager(getActivity(),3));
        rv_list.setAdapter(adapter);

总结

大家是不是觉得之前的任务有点难呢?就给任务福利了,岁月静好~


文章作者: Jeskson
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jeskson !
评论
 上一篇
教育系统APP(八) 教育系统APP(八)
用一切抱怨的时间去努力 项目“课程”的模块2目录 学习目标1.1 掌握课程模块的开发,实现用VideoView完成视频的播放1.2 掌握课程模块的开发,实现用JSONObject完成Json数据的解析1.3 掌握课程模块的开发,实现
2018-04-28
下一篇 
教育系统APP(六) 教育系统APP(六)
用一切抱怨的时间去努力 ## 回顾上一期任务 任务实施个人资料加上QQ号属性在个人资料下添加一个“QQ号”属性,初始状态文字显示“未添加”,点击进入修改页面,输入框的键盘类型只能为数字。(提示:editText.setInputType
2018-04-26
  目录