哒哒哒哒哒哒

package com.dofrom.android.payontime.fragment.paybazaar;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dofrom.android.payontime.R;
import com.dofrom.android.payontime.activity.find.paybazaar.BazaarDetailActivity;
import com.dofrom.android.payontime.activity.find.paybazaar.FuSearchActivity;
import com.dofrom.android.payontime.activity.find.paybazaar.FuSelectMarketAcitivity;
import com.dofrom.android.payontime.activity.find.paybazaar.ViewPagerDetailActivity;
import com.dofrom.android.payontime.adapter.AbstractCommonListAdapter;
import com.dofrom.android.payontime.adapter.IViewPageAdapter;
import com.dofrom.android.payontime.base.BaseTitleFragment;
import com.dofrom.android.payontime.bean.fu_bean.FuHomeListBean;
import com.dofrom.android.payontime.bean.fu_bean.HomeBanners;
import com.dofrom.android.payontime.bean.fu_bean.ReleaseHomeBean;
import com.dofrom.android.payontime.bean.request_app.ReqNull;
import com.dofrom.android.payontime.bean.request_fu.ReqHomeList_Fu;
import com.dofrom.android.payontime.holder.ViewHolder;
import com.dofrom.android.payontime.net.HttpManager_fu;
import com.dofrom.android.payontime.net.subscriber.AbstractErrorHandleSubscriber;
import com.dofrom.android.payontime.utils.Constans;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

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

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2018-09-21.
 * GitHub:https://github.com/cxydxpx
 */

public class PayBazaarHomeFragment extends BaseTitleFragment implements OnRefreshListener, OnLoadmoreListener {

    public static final int PAGE_CONTENT_INIT = 1;
    private int mCurrentPage = PAGE_CONTENT_INIT;

    private String mCityId = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_bazaar_home;
    }

    @Override
    protected void showView() {
        super.showView();
        titleBar.setTextTitle("首页");
    }


    @Override
    protected void initViews() {
        super.initViews();

        View mHeadView = View.inflate(getActivity(), R.layout.fragment_bazaar_home_header, null);
        mListView.addHeaderView(mHeadView);

        mViewPager = (ViewPager) mHeadView.findViewById(R.id.viewpager);
        tvAddress = (TextView) mHeadView.findViewById(R.id.tv_address);
        tvAddress.setOnClickListener(this);
        tvNotic = (TextView) mHeadView.findViewById(R.id.tv_notic);
        rlNotic = (RelativeLayout) mHeadView.findViewById(R.id.rl_notic);
        tvMessageNum = (TextView) mHeadView.findViewById(R.id.tv_message_num);
    }

    ViewPager mViewPager;

    TextView tvAddress;

    TextView tvNotic;

    RelativeLayout rlNotic;

    TextView tvMessageNum;

    private ArrayList<HomeBanners.BannersBean.IndexBean> mBannerList = new ArrayList<>();

    /**
     * 上一个新闻图片的索引位置
     */
    private int previousPosition;

    /**
     * 注册页面改变监听
     */
    private void registPageChangeListener() {

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            /**
             * 广告条在滑动时,该方法执行一次
             */
            public void onPageSelected(int position) {
                // 当广告条选择完时,修改开关,继续轮播广告
                int currentTitleIndex = position % mBannerList.size();
                // 状态每改变一次,重新给上一个索引赋值
                previousPosition = currentTitleIndex;
            }

            @Override
            /**
             * 广告条在滑动时,该方法多次执行.
             */
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            /**
             * 广告条在自动滑动时,该方法执行两次.
             * 状态值为:先为2再为0
             * position.
             */
            public void onPageScrollStateChanged(int state) {
                /**
                 * 解决当用户点击广告图片拖动时,广告条继续跳动的Bug
                 */
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING: // 1
                        // Log.i(tag, "DRAGGING");
                        mHandler.removeMessages(66);
                        isRunning = false;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE: // 0
                        // Log.i(tag, "IDLE");
                        if (!isRunning) {
                            isRunning = true;
                            mHandler.sendEmptyMessageDelayed(66, 2000);
                        }
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING: // 2
                        // Log.i(tag, "SETTLING");
                        // mHandler.removeMessages(66);
                        break;
                    default:
                        break;
                }
            }
        });


    }

    /**
     * 判断广告条是否继续循环的标记,默认值为true
     */
    private boolean isRunning = true;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {

            // 当退出当前应用时,结束广告轮播
            if (isRunning) {
                // 每隔2秒更新一次当前广告条目,更新条目的索引为当前索引加1,切换到下一个页面
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
                mHandler.sendEmptyMessageDelayed(66, 3000);
            }
        }
    };

    @Override
    protected void initData() {
        super.initData();
        getBannersData();

        getCommonDataFromService();
    }

    private void getCommonDataFromService() {
        HttpManager_fu
                .instance()
                .releaseConfig(new ReqNull())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbstractErrorHandleSubscriber<ReleaseHomeBean>() {
                               @Override
                               protected void onSuccess(ReleaseHomeBean bean) {
                                   parseCommonData(bean);
                               }
                           }
                );
    }

    private List<ReleaseHomeBean.CateBean> mCommonDatas = new ArrayList<>();

    private void parseCommonData(ReleaseHomeBean bean) {

        mCommonDatas.clear();
        mCommonDatas.addAll(bean.getCate());
        mCommonDatas.add(new ReleaseHomeBean.CateBean("创建名片"));

        if (null != mCommonAdapter) {
            mCommonAdapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void initListener() {
        super.initListener();

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setEnableHeaderTranslationContent(false);

        registPageChangeListener();

        initAdater();

    }

    private AbstractCommonListAdapter mCommonAdapter;

    private void initAdater() {

        mCommonAdapter = new AbstractCommonListAdapter<FuHomeListBean.DataBean>(mContext, R.layout.fragment_bazaar_home_item, mDatas) {
            @Override
            protected void convert(ViewHolder holder, FuHomeListBean.DataBean bean, final int position) {

                ImageView imageView = holder.getView(R.id.imageview);

                TextView tvName = holder.getView(R.id.tv_name);

                TextView tvDate = holder.getView(R.id.tv_date);

                TextView tvMessage = holder.getView(R.id.tv_message);

                TextView tvPrice = holder.getView(R.id.tv_price);

                String unit = bean.getUnit() == null ? "" : bean.getUnit();
                int price = bean.getPrice();

                tvPrice.setText("¥ " + price + "/" + unit);

                TextView tvContent = holder.getView(R.id.tv_content);

                TextView tvAddress = holder.getView(R.id.tv_address);

                tvAddress.setText(bean.getMarket_name() == null ? "" : bean.getMarket_name());

                tvContent.setText(bean.getBody() == null ? "" : bean.getBody());

                RecyclerView recyclerView = holder.getView(R.id.recyclerView);
                RelativeLayout rlRecycler = holder.getView(R.id.rl_recycler);

                List<String> image = bean.getImage();

                if (image.size() > 0) {
                    rlRecycler.setVisibility(View.VISIBLE);
                    setRecylerAdater(recyclerView, image, position);
                } else {
                    rlRecycler.setVisibility(View.GONE);
                }

                // type 	Int 	类型,1出售51DD9E  ,2求购 5EC9FF

                for (int i = 0; i < mCommonDatas.size(); i++) {
                    ReleaseHomeBean.CateBean bean1 = mCommonDatas.get(i);
                    if (bean1.getId() == bean.getCate_id()) {
                        tvMessage.setText(bean1.getName() == null ? "" : bean1.getName());
                    }
                }
                tvDate.setText(bean.getAdd_date() == null ? "" : bean.getAdd_date());
                tvName.setText(bean.getNickname() == null ? "" : bean.getNickname());


                Glide
                        .with(mActivity)
                        .load(bean.getAvatarUrl())
                        .into(imageView);

            }
        };

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FuHomeListBean.DataBean bean = mDatas.get(position);
                startActivity(new Intent(mContext, BazaarDetailActivity.class)
                        .putExtra(Constans.extra_fu_bazaar_detail_activity, bean));
            }
        });

        mListView.setAdapter(mCommonAdapter);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refresh();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mCurrentPage++;
        homeList(mCurrentPage);
    }

    @BindView(R.id.listview)
    ListView mListView;

    private void setRecylerAdater(RecyclerView mRecyclerView, List<String> image, final int posi) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        CommonAdapter commonAdapter = new CommonAdapter<String>(mContext, R.layout.fragment_bazaar_home_recyler_item, image) {
            @Override
            protected void convert(com.zhy.adapter.recyclerview.base.ViewHolder holder, String str, int position) {

                ImageView imageView = holder.getView(R.id.imageview);
                Glide
                        .with(mActivity)
                        .load(Constans.FU_STATIC_IMAGE + str)
                        .into(imageView);
            }
        };

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                FuHomeListBean.DataBean bean = mDatas.get(posi);
                startActivity(new Intent(mContext, BazaarDetailActivity.class)
                        .putExtra(Constans.extra_fu_bazaar_detail_activity, bean));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        mRecyclerView.setAdapter(commonAdapter);

    }

    private void getBannersData() {
        HttpManager_fu
                .instance()
                .homeBannersList(new ReqNull())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbstractErrorHandleSubscriber<HomeBanners>() {
                               @Override
                               protected void onSuccess(HomeBanners bean) {
                                   HomeBanners.BannersBean banners = bean.getBanners();
                                   List<HomeBanners.BannersBean.IndexBean> indexList = banners.getIndex() == null ? new ArrayList<HomeBanners.BannersBean.IndexBean>() : banners.getIndex();
                                   parseIndexList(indexList);

                                   HomeBanners.NoticeBean notice = bean.getNotice() == null ? new HomeBanners.NoticeBean() : bean.getNotice();

                                   String str = notice.getContent() == null ? "" : notice.getContent();

                                   if ("".equals(str)) {
                                       rlNotic.setVisibility(View.GONE);
                                   } else {
                                       rlNotic.setVisibility(View.VISIBLE);
                                       tvNotic.setText(str);
                                   }

                               }
                           }
                );
    }


    private void parseIndexList(List<HomeBanners.BannersBean.IndexBean> list) {
        mBannerList.clear();
        mBannerList.addAll(list);

        if (mBannerList.size() > 0) {
            IViewPageAdapter mAdapter = new IViewPageAdapter(mContext, mBannerList);
            mViewPager.setAdapter(mAdapter);

            mViewPager.setCurrentItem(10000000);

            mHandler.sendEmptyMessageDelayed(66, 3000);

            mAdapter.notifyDataSetChanged();

            mAdapter.setOnItemClickListener(
                    new IViewPageAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(HomeBanners.BannersBean.IndexBean bean) {
                            startActivity(new Intent(mContext, ViewPagerDetailActivity.class)
                                    .putExtra(Constans.extra_fu_viewpager, bean));
                        }
                    }
            );
        }
    }

    @Override
    protected void updateData() {
        super.updateData();

        homeMessageNum();

        refresh();
    }

    private void homeMessageNum() {
        HttpManager_fu
                .instance()
                .homeMessageNum(new ReqNull())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbstractErrorHandleSubscriber<String>() {
                               @Override
                               protected void onSuccess(String value) {
                                   int v = Integer.parseInt(value == null ? "0" : value);
                                   if (v > 0) {
                                       tvMessageNum.setText(v);
                                   }
                               }
                           }
                );
    }


    private void refresh() {
        mCurrentPage = PAGE_CONTENT_INIT;
        homeList(mCurrentPage);
    }

    private void homeList(int page) {
        HttpManager_fu
                .instance()
                .homeList(new ReqHomeList_Fu(String.valueOf(page), mCityId))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbstractErrorHandleSubscriber<FuHomeListBean>() {
                               @Override
                               protected void onSuccess(FuHomeListBean bean) {
                                   parseList(bean);
                               }
                           }
                );
    }

    private List<FuHomeListBean.DataBean> mDatas = new ArrayList<>();

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private void parseList(FuHomeListBean bean) {
        List<FuHomeListBean.DataBean> list = bean.getData() == null ? new ArrayList<FuHomeListBean.DataBean>() : bean.getData();
        if (mCurrentPage == PAGE_CONTENT_INIT) {
            mDatas.clear();
        }

        if (mCurrentPage > 1 && (list.size() == 0 || null == list)) {
            mCurrentPage--;
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadmore();
            return;
        }

        mDatas.addAll(list);

        mCommonAdapter.notifyDataSetChanged();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();


        if (mDatas.size() == 0) {
//            rlGone.setVisibility(View.VISIBLE);
        } else {
//            rlGone.setVisibility(View.GONE);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            if (requestCode == Constans.REQUEST_FU_SELECT_ADDRESS) {
                String name = data.getStringExtra(Constans.extra_fu_select_market_name);
                String id = data.getStringExtra(Constans.extra_fu_select_market_id);

                mCityId = id;

                tvAddress.setText(name);


                refresh();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void processClick(View view) {
        super.processClick(view);
        switch (view.getId()) {
            case R.id.iv_search:
                startActivityForResult(new Intent(mContext, FuSearchActivity.class), Constans.request_fu_search);
                break;
            case R.id.tv_address:
                startActivityForResult(new Intent(mContext, FuSelectMarketAcitivity.class), Constans.REQUEST_FU_SELECT_ADDRESS);
                break;
            default:
                break;
        }
    }


    @Override
    // 当退出应用时,结束循环
    public void onDestroy() {
        super.onDestroy();
        // 修改标记值
        isRunning = false;
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值