package com.technology.tederen.fragment.home; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.AppBarLayout; import android.support.design.widget.CoordinatorLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.greendao.gen.ChannelDao; import com.lzy.okgo.OkGo; import com.lzy.okgo.callback.StringCallback; import com.lzy.okgo.model.Response; import com.scwang.smartrefresh.layout.SmartRefreshLayout; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import com.technology.tederen.R; import com.technology.tederen.activity.AccessToGroupActivity; import com.technology.tederen.activity.article.ArticleDetailsActivity; import com.technology.tederen.activity.group.ComInfoActivity; import com.technology.tederen.activity.maillist.modle.OtherUserDetailsModle; import com.technology.tederen.activity.search.MainBodyTextActivity; import com.technology.tederen.activity.search.SearchActivity; import com.technology.tederen.activity.xiaoZhu.JiaruxiaozuActivity; import com.technology.tederen.base.MyApplication; import com.technology.tederen.base.MyFragment; import com.technology.tederen.fragment.channel.Channel; import com.technology.tederen.fragment.channel.ChannelDialogFragment; import com.technology.tederen.fragment.channel.PingdaoFragment; import com.technology.tederen.fragment.home.adapter.PagerAdapter; import com.technology.tederen.fragment.home.bean.AccessToGroupBean; import com.technology.tederen.fragment.home.bean.ArticleList; import com.technology.tederen.fragment.home.bean.BannerBean; import com.technology.tederen.fragment.home.bean.PDModle; import com.technology.tederen.fragment.home.bean.QrCodeBean; import com.technology.tederen.fragment.home.params.ArticleBody; import com.technology.tederen.http.Api; import com.technology.tederen.http.ErrorResponse; import com.technology.tederen.http.event.MainRefushEvent; import com.technology.tederen.http.event.MessageEvent; import com.technology.tederen.http.event.NewChannelEvent; import com.technology.tederen.http.event.SelectChannelEvent; import com.technology.tederen.http.event.TopAllEvent; import com.technology.tederen.http.event.TopEvent; import com.technology.tederen.utils.GlideImageLoader; import com.technology.tederen.utils.TranslucentStatusUtil; import com.technology.tederen.widget.EmptyView; import com.uuzuche.lib_zxing.activity.CaptureActivity; import com.uuzuche.lib_zxing.activity.CodeUtils; import com.youth.banner.Banner; import com.youth.banner.listener.OnBannerListener; import net.lucode.hackware.magicindicator.MagicIndicator; import net.lucode.hackware.magicindicator.ViewPagerHelper; import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView; import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator; import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView; import org.json.JSONObject; import org.simple.eventbus.EventBus; import org.simple.eventbus.Subscriber; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import butterknife.BindView; import butterknife.OnClick; public class SubHomeFragment extends MyFragment implements OnBannerListener, OnRefreshListener { public static final String TAG = "SubHomeFragment"; @BindView(R.id.iv_add) ImageView mIvAdd; @BindView(R.id.fl_search) TextView mFlSearch; @BindView(R.id.indicator_sub_home) MagicIndicator magicIndicator; @BindView(R.id.vp_sub_home) ViewPager subHomePager; @BindView(R.id.iv_scan) TextView mIvScan; @BindView(R.id.appbar) AppBarLayout appBarLayout; @BindView(R.id.banner) Banner mBanner; @BindView(R.id.subRefresh) SmartRefreshLayout refreshLayout; @BindView(R.id.emptview) EmptyView emptyView; @BindView(R.id.fl_tittle) LinearLayout flTittle; private List mSelectedDates; private List mUnSelectedDates; BannerBean bannerBean; private PagerAdapter pagerAdapter; private int selectedIndex; @Override public void onLazyLoad() { getBannerData(); } @Override protected int getContentLayoutId() { return R.layout.fragment_sub_home; } public void toTop(){ CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior(); if (behavior instanceof AppBarLayout.Behavior) { AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior; int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset(); if (topAndBottomOffset != 0) { appBarLayoutBehavior.setTopAndBottomOffset(0); } } EventBus.getDefault().post(new TopAllEvent()); } @Override protected void initWidget(View root) { super.initWidget(root); TranslucentStatusUtil.initState(getActivity(), flTittle); EventBus.getDefault().register(this); mBanner.setOnBannerListener(this); refreshLayout.setOnRefreshListener(this); subHomePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { selectedIndex = position; } @Override public void onPageScrollStateChanged(int state) { } }); emptyView.bind(refreshLayout); setPlaceHolderView(emptyView); emptyView.triggerLoading(); } @Override protected void initData() { super.initData(); selectedIndex = 0; germSelectedDates(); germUnSelectedDates(); } private List indexPosition; private void getBannerData() { OkGo.get(Api.BANNER_LIST) .headers(Api.AUTHORIZATION, token) .execute(new StringCallback() { @Override public void onSuccess(Response response) { if (response.code() == 200) { Log.d(TAG, "onSuccess: " + response.body()); List imgs = new ArrayList<>(); indexPosition = new ArrayList(); bannerBean = new Gson().fromJson(response.body(), BannerBean.class); for (int i = 0; i < bannerBean.getItems().size(); i++) { imgs.add(bannerBean.getItems().get(i).getImageUrl()); indexPosition.add(bannerBean.getItems().get(i).getArticleId()); } if (imgs.size() == 0) { mBanner.setVisibility(View.GONE); } else { mBanner.setVisibility(View.VISIBLE); mBanner.setImageLoader(new GlideImageLoader()); mBanner.setImages(imgs); mBanner.start(); } } mPlaceHolderView.triggerOk(); refreshLayout.finishRefresh(); } }); } @Override public void OnBannerClick(int position) { Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class); intent.putExtra("id",indexPosition.get(position)); startActivity(intent); } @OnClick({R.id.iv_add, R.id.fl_search, R.id.iv_scan}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.iv_add: PingdaoFragment dialogFragment = PingdaoFragment.newInstance(mSelectedDates, mUnSelectedDates,token); dialogFragment.show(getChildFragmentManager(), "CHANNEL"); break; case R.id.fl_search: SearchActivity.tothis(getContext()); break; case R.id.iv_scan: break; } } CommonNavigator commonNavigator; public CommonNavigator setCommonNavigator() { commonNavigator = new CommonNavigator(getContext()); commonNavigator.setScrollPivotX(0.65f); commonNavigator.setAdjustMode(false); commonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { return mSelectedDates == null ? 0 : mSelectedDates.size(); } @Override public IPagerTitleView getTitleView(Context context, final int index) { SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context); simplePagerTitleView.setText(mSelectedDates.get(index).getArticleGroupName()); simplePagerTitleView.setNormalColor(getResources().getColor(R.color.text_color6)); simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.text_color)); simplePagerTitleView.setTextSize(16); simplePagerTitleView.setOnClickListener(v -> subHomePager.setCurrentItem(index, false)); return simplePagerTitleView; } @Override public IPagerIndicator getIndicator(Context context) { LinePagerIndicator indicator = new LinePagerIndicator(context); indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT); indicator.setLineHeight(0); indicator.setRoundRadius(2); indicator.setStartInterpolator(new AccelerateInterpolator()); indicator.setEndInterpolator(new DecelerateInterpolator(2.0f)); indicator.setColors(getResources().getColor(R.color.text_blue)); return indicator; } }); return commonNavigator; } public void germSelectedDates() { mSelectedDates = new ArrayList<>(); OkGo.post(Api.MAIN_PINGDAOMY) .upJson(new JSONObject()) .headers(Api.AUTHORIZATION, token) .execute(new StringCallback() { @Override public void onSuccess(Response response) { try { if (response.code() == 200) { mSelectedDates = new Gson().fromJson(response.body(), new TypeToken>() { }.getType()); if (mSelectedDates != null) { // 设置viewpager和adapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), mSelectedDates); subHomePager.setAdapter(pagerAdapter); subHomePager.setOffscreenPageLimit(10); // 设置CommonNavigator magicIndicator.setNavigator(setCommonNavigator()); ViewPagerHelper.bind(magicIndicator, subHomePager); } } else { ErrorResponse errorResponse = new Gson().fromJson(response.body(), ErrorResponse.class); if (errorResponse != null) showToast(errorResponse.getMessage()); } } catch (Exception e) { Log.e(TAG, "onSuccess: " + e); } } @Override public void onError(Response response) { super.onError(response); } }); } public void germUnSelectedDates() { mUnSelectedDates = new ArrayList<>(); OkGo.get(Api.GET_CHANNELS) .headers(Api.AUTHORIZATION, token) .execute(new StringCallback() { @Override public void onSuccess(Response response) { if (response.code() == 200) { mUnSelectedDates = new Gson().fromJson(response.body(), new TypeToken>() { }.getType()); } else { ErrorResponse errorResponse = new Gson().fromJson(response.body(), ErrorResponse.class); if (errorResponse != null) showToast(errorResponse.getMessage()); } } }); } @Subscriber public void updateChannel(MainRefushEvent event) { initData(); } // // @Subscriber // public void selectChannelEvent(SelectChannelEvent selectChannelEvent) { // if (selectChannelEvent == null) return; // List integers = new ArrayList<>(); // for (Channel channel : mSelectedDates) { // integers.add(channel.getName()); // } // setViewpagerPosition(integers, selectChannelEvent.channelName); // } // // /** // * 设置 当前选中页 // * // * @param integers // * @param channelName // */ // private void setViewpagerPosition(List integers, String channelName) { // if (TextUtils.isEmpty(channelName) || integers == null) return; // for (int j = 0; j < integers.size(); j++) { // if (integers.get(j).equals(channelName)) { // selectedChannel = integers.get(j); // selectedIndex = j; // break; // } // } // subHomePager.postDelayed(new Runnable() { // @Override // public void run() { // subHomePager.setCurrentItem(selectedIndex, false); // } // }, 100); // } @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { Log.e("mSelectedDates.size==",mSelectedDates.size()+""); if(mSelectedDates.size()==0) { refreshLayout.finishRefresh(); return; } getBannerData(); EventBus.getDefault().post(new MessageEvent("更新", mSelectedDates.get(selectedIndex).getArticleGroupId())); } @Subscriber public void Event(TopEvent messageEvent) { toTop(); } }