123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- 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<PDModle> mSelectedDates;
- private List<PDModle> 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<Integer> indexPosition;
- private void getBannerData() {
- OkGo.<String>get(Api.BANNER_LIST)
- .headers(Api.AUTHORIZATION, token)
- .execute(new StringCallback() {
- @Override
- public void onSuccess(Response<String> response) {
- if (response.code() == 200) {
- Log.d(TAG, "onSuccess: " + response.body());
- List<String> 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.<String>post(Api.MAIN_PINGDAOMY)
- .upJson(new JSONObject())
- .headers(Api.AUTHORIZATION, token)
- .execute(new StringCallback() {
- @Override
- public void onSuccess(Response<String> response) {
- try {
- if (response.code() == 200) {
- mSelectedDates = new Gson().fromJson(response.body(), new TypeToken<List<PDModle>>() {
- }.getType());
- if (mSelectedDates != null) {
-
- pagerAdapter = new PagerAdapter(getChildFragmentManager(), mSelectedDates);
- subHomePager.setAdapter(pagerAdapter);
- subHomePager.setOffscreenPageLimit(10);
-
- 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<String> response) {
- super.onError(response);
- }
- });
- }
- public void germUnSelectedDates() {
- mUnSelectedDates = new ArrayList<>();
- OkGo.<String>get(Api.GET_CHANNELS)
- .headers(Api.AUTHORIZATION, token)
- .execute(new StringCallback() {
- @Override
- public void onSuccess(Response<String> response) {
- if (response.code() == 200) {
- mUnSelectedDates = new Gson().fromJson(response.body(), new TypeToken<List<PDModle>>() {
- }.getType());
- } else {
- ErrorResponse errorResponse =
- new Gson().fromJson(response.body(), ErrorResponse.class);
- if (errorResponse != null)
- showToast(errorResponse.getMessage());
- }
- }
- });
- }
- @Subscriber
- public void updateChannel(MainRefushEvent event) {
- initData();
- }
- @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();
- }
- }
|