京东首页

ApiService接口类
 @GET("ad/getAd")
    Observable rxjavaIndex();

@GET("product/getCatagory")
    Observable  rxjavaCatagory();
//retrofit根目录
public class HttpConfig {
    public static final String _URL="http://120.27.23.105/";
}

/**
 *工具类
 * Description:retrofit工具类
 * 构造者模式如何封装:首先有各种builder过程,最后通过build()方法对对象进行创建
 */
public class RetrofitUtils {

    //创建私有属性
    private static RetrofitUtils mInstance;
    //创建ApiService引用
    private static ApiService apiService;

    /**
     * 声明私有构造方法,类似于单利模式,让调用者无法new出对象,只能通过构造者模式创建对象
     */
    private RetrofitUtils() {

    }

    /**
     * 先后调用的过程,只有对象给build成功之后,才能调用apiservice,这也是retrofit的几个先后的步骤
     * @return
     */
    public ApiService getApiService() {
        return apiService;
    }

    public static class Builder {

        private OkHttpClient okHttpClient;

        private List callAdapterFactorys = new ArrayList<>();
        private List convertFactorys = new ArrayList<>();

        public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
            callAdapterFactorys.add(factory);
            return this;//返回this的目的是:可以链式调用
        }

        public Builder addConverterFactory(Converter.Factory factory) {
            convertFactorys.add(factory);
            return this;
        }

        public Builder client(OkHttpClient okHttpClient) {
            this.okHttpClient = okHttpClient;
            return this;
        }

        /**
         * build方法才是真正的业务逻辑操作,才是真正构建对象的过程
         * @return
         */
        public RetrofitUtils build() {


            Retrofit.Builder builder = new Retrofit.Builder();

            if (convertFactorys != null && convertFactorys.size() == 0) {
                //如果集合中没有对象,我们添加默认的gson解析器
                builder.addConverterFactory(GsonConverterFactory.create());
            }

            for (Converter.Factory convertFactory : convertFactorys) {
                builder.addConverterFactory(convertFactory);
            }

            if (callAdapterFactorys != null && callAdapterFactorys.size() == 0) {
                builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());

            }

            for (CallAdapter.Factory callAdapterFactory : callAdapterFactorys) {
                builder.addCallAdapterFactory(callAdapterFactory);
            }

            //第一步创建retrofit对象
            Retrofit retrofit = builder.client(okHttpClient).baseUrl(HttpConfig._URL)
                    .build();
            //第二步,创建apiservice接口类
            apiService = retrofit.create(ApiService.class);
            //第三步,请求逻辑(这个供外部调用者调用)
            mInstance = new RetrofitUtils();//私有构造方法,只有本类可以new出实例
            return mInstance;
        }
    }
}

//model
public class IndexModel {
    public void index(final IndexCallback indexCallback){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HttpLoggingInterceptor()
                .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(HttpConfig._URL).build();
        ApiService apiService = retrofit.create(ApiService.class);
        apiService.rxjavaIndex().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IndexBean value) {
                        if (value.getCode().equals("0")) {
                            indexCallback.indexSuccess(value);
                        }else{
                            indexCallback.indexFail(value);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        indexCallback.failure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
    public interface IndexCallback {
        void indexSuccess(IndexBean bean);//网络请求成功

        void indexFail(IndexBean bean);//网络请求失败

        void failure(Throwable t);//网络请求失败
    }
}
//------------------------------------
public class SudokuModel {
    public void sudoku(final SudokuCallback sudokuCallback){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(HttpConfig._URL).build();
        ApiService apiService = retrofit.create(ApiService.class);
        apiService.rxjavaCatagory().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CatagoryBean value) {
                        if (value.getCode().equals("0")) {
                            sudokuCallback.sudokuSuccess(value);
                        }else{
                            sudokuCallback.sudokuFail(value);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        sudokuCallback.failure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    public interface SudokuCallback {
        void sudokuSuccess(CatagoryBean bean);//网络请求成功

        void sudokuFail(CatagoryBean bean);//网络请求失败

        void failure(Throwable t);//网络请求失败
    }
}


//presenter
public class IndexPresenter {
    private final IndexView view;
    private final IndexModel indexModel;
    private final SudokuModel sudokuModel;
    public IndexPresenter(IndexView indexView) {
        indexModel = new IndexModel();
        sudokuModel = new SudokuModel();
        this.view=indexView;

    }

    public void index(){
        indexModel.index(new IndexModel.IndexCallback() {
            @Override
            public void indexSuccess(IndexBean bean) {
                view.indexSuccess(bean);
            }

            @Override
            public void indexFail(IndexBean bean) {
            view.indexFailure(bean);
            }

            @Override
            public void failure(Throwable t) {
            view.indexfailure(t);
            }
        });
    }

    public void sudoku(){

        sudokuModel.sudoku(new SudokuModel.SudokuCallback() {
            @Override
            public void sudokuSuccess(CatagoryBean bean) {
                view.sudokuSuccess(bean);
            }

            @Override
            public void sudokuFail(CatagoryBean bean) {
                view.sudokuFailure(bean);
            }

            @Override
            public void failure(Throwable t) {
                view.sudokufailure(t);
            }
        });
    }
}

//view
public interface IndexView {
    void indexSuccess(IndexBean bean);//网络请求成功
    void indexFailure(IndexBean bean);
    void indexfailure(Throwable t);//网络请求失败(断网,服务器崩溃,接口地址不正确,弱网)
    void sudokuSuccess(CatagoryBean bean);//网络请求成功
    void sudokuFailure(CatagoryBean bean);
    void sudokufailure(Throwable t);//网络请求失败(断网,服务器崩溃,接口地址不正确,弱网)
}

//首页
public class FragmentSY extends BaseFragment implements IndexView,OnBannerListener,View.OnClickListener {

    private TextView sousuo;
    private XRecyclerView xRecyler;
    private FrameLayout framelayout;
    private Banner banner;
    private List list;
    private int mDistanceY;
    private Handler handler=new Handler();
    private List imagesbanner;
    private List titlesbanner;

    @Override
    protected int bindFragmentLayout() {
        return R.layout.sy_fragment;
    }

    @Override
    protected void initViews(){
        IndexPresenter indexPresenter = new IndexPresenter(this);
        indexPresenter.index();
        indexPresenter.sudoku();
        setStatus(true);
        //setFullScreen(true);
        sousuo = getView().findViewById(R.id.sy_sousuo);
        xRecyler = getView().findViewById(R.id.index_xRecyler);
        framelayout = getView().findViewById(R.id.sy_framelayout);

    }


    @Override
    protected void initData() {
        //设置布局管理器
        xRecyler.setLayoutManager(new LinearLayoutManager(getActivity()));
        xRecyler.setPullRefreshEnabled(true);//下拉刷新
        xRecyler.setLoadingMoreEnabled(true);//上拉加载
        xRecyler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//样式
        xRecyler.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        //监听
        xRecyler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                //滑动的距离
                mDistanceY += dy;
                //toolbar的高度
                int toolbarHeight = framelayout.getBottom();

                //当滑动的距离 <= toolbar高度的时候,改变Toolbar背景色的透明度,达到渐变的效果
                if (mDistanceY <= 300) {
                    float scale = (float) mDistanceY / 300;
                    float alpha = scale * 255;
                    framelayout.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));

                } else {
                    //将标题栏的颜色设置为完全不透明状态
                    framelayout.setBackgroundResource(R.color.White);
                }
            }
        });
        xRecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                framelayout.setVisibility(View.GONE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        framelayout.setVisibility(View.VISIBLE);
                        //让进度条缩回去
                        xRecyler.refreshComplete();
                    }
                },1000);

            }

            @Override
            public void onLoadMore() {
                //让进度条缩回去
                xRecyler.refreshComplete();
            }
        }) ;
    }

    @Override
    protected void setListener() {
        sousuo.setOnClickListener(this);
    }
//---------------------------------------------首页秒杀 轮播图
    @Override
    public void indexSuccess(IndexBean bean) {
        getBanner(bean);
        List Miaosha = bean.getMiaosha().getList();
        List Tuijian = bean.getTuijian().getList();
        xRecyler.setAdapter(new IndexAdapter(getActivity(),list,Miaosha,Tuijian,this));
    }

    @Override
    public void indexFailure(IndexBean bean) {

    }

    @Override
    public void indexfailure(Throwable t) {
        Log.d("首页", t.toString());
    }


    @Override
    public void OnBannerClick(int position) {
    showToast(imagesbanner.get(position));
    }

    //------------------------------------九宫格
    @Override
    public void sudokuSuccess(CatagoryBean bean) {
        list = bean.getData();
    }

    @Override
    public void sudokuFailure(CatagoryBean bean) {

    }

    @Override
    public void sudokufailure(Throwable t) {

    }
//-----------------------------------------------------------
    private void getBanner(IndexBean bean) {
        View inflate = View.inflate(getContext(), R.layout.index_title_banner, null);
        banner = inflate.findViewById(R.id.index_banner);
        xRecyler.addHeaderView(inflate);
        List data = bean.getData();
        imagesbanner = new ArrayList();
        titlesbanner = new ArrayList();
        for (IndexBean.DataBean datum : data) {
            imagesbanner.add(datum.getIcon());
            titlesbanner.add(datum.getTitle());
        }
        //设置内置样式,共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器,图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(imagesbanner);
        //设置轮播的动画效果,内含多种特效,可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
        banner.setBannerTitles(titlesbanner);
        //设置轮播间隔时间
        banner.setDelayTime(1000);
        //设置是否为自动轮播,默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置,小点点,左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局,这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(this)
                //必须最后调用的方法,启动轮播图。
                .start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sy_sousuo:
                Intent intent = new Intent(getActivity(), SousuoGGActivity.class);
                startActivity(intent);
                break;
        }
    }

    //banner加载图片
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setImageURI(Uri.parse((String) path));
        }
        @Override
        public ImageView createImageView(Context context) {
            //使用fresco,需要创建它提供的ImageView,当然你也可以用自己自定义的具有图片加载功能的ImageView
            SimpleDraweeView simpleDraweeView=new SimpleDraweeView(context);
            return simpleDraweeView;
        }
    }
}

//首页适配器
public class IndexAdapter extends RecyclerView.Adapter{
    private final Context context;
    private final List miaosha;
    private final List tuijian;
    private final IndexView indexView;
    private Handler handler = new Handler();
    private long time=1000;
    private final int TYPE_ONE=1;
    private final int TYPE_TWO=2;
    private final int TYPE_THREE =3;
    private final int TYPE_FOUR =4;
    private final int TYPE_FIVE =5;
    private final List list;
    private SudokuViewHolder sudokuViewHolder;
    private KuaibaoViewHolder kuaibaoViewHolder;
    private MiaoshaViewHolder miaoshaViewHolder;
    private TuijianViewHolder tuijianViewHolder;

    public IndexAdapter(Context context,List list, List miaosha, List tuijian, IndexView indexView) {
    this.context=context;
    this.miaosha=miaosha;
    this.list=list;
    this.tuijian=tuijian;
    this.indexView=indexView;

    }
//----------------------------------------------根据多条目返回值加载布局
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_ONE:
                View view_one = View.inflate(context, R.layout.index_sudoku_adapter, null);
                sudokuViewHolder = new SudokuViewHolder(view_one);
                return sudokuViewHolder;
            case TYPE_TWO:
                View view_two = View.inflate(context, R.layout.index_kuaibao_item, null);
                kuaibaoViewHolder = new KuaibaoViewHolder(view_two);
                return kuaibaoViewHolder;
            case TYPE_THREE:
                View view_three = View.inflate(context, R.layout.index_miaosha_adapter, null);
                miaoshaViewHolder = new MiaoshaViewHolder(view_three);
                return miaoshaViewHolder;
            case TYPE_FOUR:
                View view_four = View.inflate(context, R.layout.index_tuijian_adapter, null);
                tuijianViewHolder = new TuijianViewHolder(view_four);
                return tuijianViewHolder;
        }
        return null;
    }
//-----------------------------------------------根据返回值 配置适配器
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SudokuViewHolder) {
            if (sudokuViewHolder.sudoku_recycler != null) {
                sudokuViewHolder.sudoku_recycler.setLayoutManager(new GridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false));
                sudokuViewHolder.sudoku_recycler.setAdapter(new SudokuAdapter(context, list));
            }
        }
        if (holder instanceof KuaibaoViewHolder) {
            List notices = new ArrayList<>();
            notices.add("大促销下单拆福袋,亿万新年红包随便拿");
            notices.add("家电五折团,抢十亿无门槛现金红包");
            notices.add("星球大战剃须刀首发送200元代金券");
            kuaibaoViewHolder.notice_view.addNotice(notices);
            kuaibaoViewHolder.notice_view.startFlipping();

        }
        if (holder instanceof MiaoshaViewHolder) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    time--;
                    String formatLongToTimeStr = formatLongToTimeStr(time);
                    String[] split = formatLongToTimeStr.split(":");
                    for (int i = 0; i < split.length; i++) {
                        if(i==0){
                            miaoshaViewHolder.miaosha_time_hour.setText(split[0]+"小时");
                        }
                        if(i==1){
                            miaoshaViewHolder.miaosha_time_minute.setText(split[1]+"分钟");
                        }
                        if(i==2){
                            miaoshaViewHolder.miaosha_time_seconds.setText(split[2]+"秒");
                        }
                    }
                    if(time>0){
                        handler.postDelayed(this, 1000);
                    }
                }
            }, 1000);

            miaoshaViewHolder.miaosha_recycler.setLayoutManager(new GridLayoutManager(context,1,GridLayoutManager.HORIZONTAL,false));
            miaoshaViewHolder.miaosha_recycler.setAdapter(new MiaoshaAdapter(context, miaosha));
        }

        if (holder instanceof TuijianViewHolder) {
            tuijianViewHolder.tuijian_recycler.setLayoutManager(new GridLayoutManager(context,2,GridLayoutManager.VERTICAL,false));
            tuijianViewHolder.tuijian_recycler.setAdapter(new TuijianAdapter(context, tuijian));

        }
    }
//-----------------------多条目加载
    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE_ONE;
        } if (position==1){
            return TYPE_TWO;
        } if (position==2){
            return TYPE_THREE;
        } if (position==3){
            return TYPE_FOUR;
        }
        return 0;
    }
//----------------------条目的数量
    @Override
    public int getItemCount() {
        return 4;
    }

    //--------------------------九宫格
    class SudokuViewHolder extends RecyclerView.ViewHolder{
        private final RecyclerView sudoku_recycler;

        public SudokuViewHolder(View itemView) {
            super(itemView);
            sudoku_recycler = itemView.findViewById(R.id.sudoku_recycler);
        }
    }
    //-------------------------快报
    class KuaibaoViewHolder extends RecyclerView.ViewHolder{
        private final NoticeView notice_view;

        public KuaibaoViewHolder(View itemView) {
            super(itemView);
            notice_view = itemView.findViewById(R.id.notice_view);
        }
    }
//------------------------------秒杀
class MiaoshaViewHolder extends RecyclerView.ViewHolder{

    private final TextView miaosha_time_hour;
    private final TextView miaosha_time_minute;
    private final TextView miaosha_time_seconds;
    private final RecyclerView miaosha_recycler;

    public MiaoshaViewHolder(View itemView) {
        super(itemView);
        //时
        miaosha_time_hour = itemView.findViewById(R.id.miaosha_time_hour);
        //分
        miaosha_time_minute = itemView.findViewById(R.id.miaosha_time_minute);
        //秒
        miaosha_time_seconds = itemView.findViewById(R.id.miaosha_time_seconds);
        //recyclerview
        miaosha_recycler = itemView.findViewById(R.id.miaosha_recycler);
    }
}
    public String formatLongToTimeStr(Long l) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l.intValue() ;
        if (second > 60) {
            minute = second / 60;   //取整
            second = second % 60;   //取余
        }

        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        String strtime = hour+":"+minute+":"+second;
        return strtime;

    }
//-----------------------------------------推荐
class TuijianViewHolder extends RecyclerView.ViewHolder{

    private final RecyclerView tuijian_recycler;

    public TuijianViewHolder(View itemView) {
        super(itemView);
        tuijian_recycler = itemView.findViewById(R.id.tuijian_recycler);
    }
}
}

//九宫格适配器
public class SudokuAdapter extends RecyclerView.Adapter{

    private final Context context;
    private final List list;

    public SudokuAdapter(Context context, List list) {
        this.context=context;
        this.list=list;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.index_sudoku_adapter_item, null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.sudoku_textview.setText(list.get(position).getName());
        holder.sudoku_fresco.setImageURI(Uri.parse(list.get(position).getIcon()));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView sudoku_textview;
        public SimpleDraweeView sudoku_fresco;

        public MyViewHolder(View itemView) {
            super(itemView);
            sudoku_textview = itemView.findViewById(R.id.sudoku_textview);
            sudoku_fresco = itemView.findViewById(R.id.sudoku_fresco);
        }
    }
}

//---------秒杀适配器
public class MiaoshaAdapter extends RecyclerView.Adapter{
    private final Context context;
    private final List miaosha;

    public MiaoshaAdapter(Context context, List miaosha) {
        this.context=context;
        this.miaosha=miaosha;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.index_miaosha_adapter_item, null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        String images = miaosha.get(position).getImages();
        String[] imgs = images.split("\\|");
        holder.miaosha_fresco.setImageURI(Uri.parse(imgs[0]));
        holder.miaosha_bargainprice.setText("原价:"+miaosha.get(position).getBargainPrice());
        holder.miaosha_bargainprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );//这句话就是给TextView加删除线
        holder.miaosha_price.setText("抢购价:"+miaosha.get(position).getPrice());
    }

    @Override
    public int getItemCount() {
        return miaosha.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView miaosha_bargainprice;
        public TextView miaosha_price;
        public SimpleDraweeView miaosha_fresco;

        public MyViewHolder(View itemView) {
            super(itemView);
            miaosha_bargainprice = itemView.findViewById(R.id.miaosha_bargainprice);
            miaosha_price = itemView.findViewById(R.id.miaosha_price);
            miaosha_fresco = itemView.findViewById(R.id.miaosha_fresco);
        }
    }
}

//-----------------推荐适配器
public class TuijianAdapter extends RecyclerView.Adapter{
    private final Context context;
    private final List tuijian;

    public TuijianAdapter(Context context, List tuijian) {
        this.context=context;
        this.tuijian=tuijian;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.index_tuijian_adapter_item, null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        String images = tuijian.get(position).getImages();
        String[] imgs = images.split("\\|");
        holder.tuijian_fresco.setImageURI(Uri.parse(imgs[0]));
        holder.tuijian_title.setText(tuijian.get(position).getTitle());
        holder.tuijian_subhead.setText(tuijian.get(position).getSubhead());
    }

    @Override
    public int getItemCount() {
        return tuijian.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView tuijian_title;
        public TextView tuijian_subhead;
        public SimpleDraweeView tuijian_fresco;

        public MyViewHolder(View itemView) {
            super(itemView);
            tuijian_title = itemView.findViewById(R.id.tuijian_title);
            tuijian_subhead = itemView.findViewById(R.id.tuijian_subhead);
            tuijian_fresco = itemView.findViewById(R.id.tuijian_fresco);
        }
    }
}

//首页上下移动轮播图
public class NoticeView extends ViewFlipper implements View.OnClickListener {

    private Context mContext;
    private List mNotices;

    public NoticeView(Context context) {
        super(context);
    }

    public NoticeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        // 轮播间隔时间为3s
        setFlipInterval(3000);
        // 内边距5dp
        setPadding(dp2px(5f), dp2px(5f), dp2px(5f), dp2px(5f));
        // 设置enter和leave动画
        setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notify_in));
        setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.notify_out));
    }

    /**
     * 添加需要轮播展示的公告
     *
     * @param notices
     */
    public void addNotice(List notices) {
        mNotices = notices;
        removeAllViews();
        for (int i = 0; i < mNotices.size(); i++) {
            // 根据公告内容构建一个TextView
            String notice = notices.get(i);
            TextView textView = new TextView(mContext);
            textView.setSingleLine();
            textView.setText(notice);
            textView.setTextSize(16f);
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setTextColor(Color.RED);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            // 将公告的位置设置为textView的tag方便点击是回调给用户
            textView.setTag(i);
            textView.setOnClickListener(this);
            // 添加到ViewFlipper
            NoticeView.this.addView(textView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        }
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        String notice = (String) mNotices.get(position);
        if (mOnNoticeClickListener != null) {
            mOnNoticeClickListener.onNotieClick(position, notice);
        }
    }

    /**
     * 通知点击监听接口
     */
    public interface OnNoticeClickListener {
        void onNotieClick(int position, String notice);
    }

    private OnNoticeClickListener mOnNoticeClickListener;

    /**
     * 设置通知点击监听器
     *
     * @param onNoticeClickListener 通知点击监听器
     */
    public void setOnNoticeClickListener(OnNoticeClickListener onNoticeClickListener) {
        mOnNoticeClickListener = onNoticeClickListener;
    }

    private int dp2px(float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpValue,
                mContext.getResources().getDisplayMetrics());
    }
}

//XML文件


    
    
    
        

            


            

                

                

                
            

            
        
    

//RecyclerView
 
    

//index_sudoku_adapter_item.xml


    
    

//index_kuaibao_item.xml


    
    
    
    
    
    
    

//秒杀index_miaosha_adapter.xml



    

    
    

    
    
    
    

    
    

    
    


//轮播图index_title_banner.xml


    

//初始化Fresc
public class MApp extends Application{
   
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);       
    }
}

//依赖库
 //依赖
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'  //Rxjava
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'  //Rx安卓
    compile 'com.squareup.retrofit2:retrofit:2.3.0' //Retrofit请求网络
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'//Gson
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'//拦截器
    compile 'com.jcodecraeer:xrecyclerview:1.5.8'//xrecyclerview依赖
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'//recyclerview依赖
    compile 'com.youth.banner:banner:1.4.9' //Banner依赖
    //图片的依赖
    compile 'com.facebook.fresco:fresco:0.14.1'
    compile 'com.facebook.fresco:animated-base-support:0.14.1'
    compile 'com.facebook.fresco:animated-gif:0.14.1'
    compile 'com.facebook.fresco:webpsupport:0.14.1'
    compile 'com.facebook.fresco:animated-webp:0.14.1'
    compile 'com.facebook.fresco:imagepipeline-okhttp3:0.14.1'
    compile 'com.android.support:design:26+' //TabLayout的依赖
    compile 'org.greenrobot:greendao:3.2.2'//greenDao依赖
    compile 'de.greenrobot:eventbus:3.0.0-beta1'

//封装Fragment基类
public abstract class BaseFragment extends Fragment{
    protected Context mContext;
    private boolean isStatus = false;//沉浸式透明状态栏标示
    private boolean isFullScreen = false;
    /**
     * 当fragment与activity发生关联时调用(比如跳转页面)
     * @param context  与之相关联的activity
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(bindFragmentLayout(), container, false);
        return view;
    }

    protected abstract int bindFragmentLayout();//绑定布局

    //onCreateView是创建的时候调用,onViewCreated是在onCreateView后被触发的事件,前后关系
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
    }

    protected abstract void initViews();//初始化组件

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        setListener();
    }

    protected abstract void initData();//初始化数据

    protected abstract void setListener();//初始化点击事件

    /**
     * intent跳转
     * context 当前类的上下文对象(getActivity())
     * @param clazz
     */
    protected void toClass(Context context,Class clazz) {
        //方法的重载 调用
        toClass(context, clazz,null);
    }
    /**
     * intent跳转 传值
     * context 当前类的上下文对象(getActivity())
     * @param clazz
     */
    protected void toClass(Context context,Class clazz,Bundle bundle) {
        Intent intent = new Intent(context, clazz);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
    public void setStatus(boolean status) {
        isStatus = status;
        if (isStatus){
            //判断当前设备的版本号》=19的时候,走这个代码块,这个用于版本适配
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){

                getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
    }

    public void setFullScreen(boolean fullScreen) {
        isFullScreen = fullScreen;
        if (isFullScreen){//是全屏的时候
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    public void showToast(String msg){
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}





 


你可能感兴趣的:(京东首页)