SubHomeFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.technology.tederen.fragment.home;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.design.widget.AppBarLayout;
  8. import android.support.design.widget.CoordinatorLayout;
  9. import android.support.v4.app.Fragment;
  10. import android.support.v4.view.ViewPager;
  11. import android.text.TextUtils;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.animation.AccelerateInterpolator;
  15. import android.view.animation.DecelerateInterpolator;
  16. import android.widget.FrameLayout;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. import com.google.gson.Gson;
  22. import com.google.gson.reflect.TypeToken;
  23. import com.greendao.gen.ChannelDao;
  24. import com.lzy.okgo.OkGo;
  25. import com.lzy.okgo.callback.StringCallback;
  26. import com.lzy.okgo.model.Response;
  27. import com.scwang.smartrefresh.layout.SmartRefreshLayout;
  28. import com.scwang.smartrefresh.layout.api.RefreshLayout;
  29. import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
  30. import com.technology.tederen.R;
  31. import com.technology.tederen.activity.AccessToGroupActivity;
  32. import com.technology.tederen.activity.article.ArticleDetailsActivity;
  33. import com.technology.tederen.activity.group.ComInfoActivity;
  34. import com.technology.tederen.activity.maillist.modle.OtherUserDetailsModle;
  35. import com.technology.tederen.activity.search.MainBodyTextActivity;
  36. import com.technology.tederen.activity.search.SearchActivity;
  37. import com.technology.tederen.activity.xiaoZhu.JiaruxiaozuActivity;
  38. import com.technology.tederen.base.MyApplication;
  39. import com.technology.tederen.base.MyFragment;
  40. import com.technology.tederen.fragment.channel.Channel;
  41. import com.technology.tederen.fragment.channel.ChannelDialogFragment;
  42. import com.technology.tederen.fragment.channel.PingdaoFragment;
  43. import com.technology.tederen.fragment.home.adapter.PagerAdapter;
  44. import com.technology.tederen.fragment.home.bean.AccessToGroupBean;
  45. import com.technology.tederen.fragment.home.bean.ArticleList;
  46. import com.technology.tederen.fragment.home.bean.BannerBean;
  47. import com.technology.tederen.fragment.home.bean.PDModle;
  48. import com.technology.tederen.fragment.home.bean.QrCodeBean;
  49. import com.technology.tederen.fragment.home.params.ArticleBody;
  50. import com.technology.tederen.http.Api;
  51. import com.technology.tederen.http.ErrorResponse;
  52. import com.technology.tederen.http.event.MainRefushEvent;
  53. import com.technology.tederen.http.event.MessageEvent;
  54. import com.technology.tederen.http.event.NewChannelEvent;
  55. import com.technology.tederen.http.event.SelectChannelEvent;
  56. import com.technology.tederen.http.event.TopAllEvent;
  57. import com.technology.tederen.http.event.TopEvent;
  58. import com.technology.tederen.utils.GlideImageLoader;
  59. import com.technology.tederen.utils.TranslucentStatusUtil;
  60. import com.technology.tederen.widget.EmptyView;
  61. import com.uuzuche.lib_zxing.activity.CaptureActivity;
  62. import com.uuzuche.lib_zxing.activity.CodeUtils;
  63. import com.youth.banner.Banner;
  64. import com.youth.banner.listener.OnBannerListener;
  65. import net.lucode.hackware.magicindicator.MagicIndicator;
  66. import net.lucode.hackware.magicindicator.ViewPagerHelper;
  67. import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
  68. import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
  69. import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
  70. import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
  71. import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
  72. import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
  73. import org.json.JSONObject;
  74. import org.simple.eventbus.EventBus;
  75. import org.simple.eventbus.Subscriber;
  76. import java.util.ArrayList;
  77. import java.util.Arrays;
  78. import java.util.List;
  79. import butterknife.BindView;
  80. import butterknife.OnClick;
  81. public class SubHomeFragment extends MyFragment implements OnBannerListener, OnRefreshListener {
  82. public static final String TAG = "SubHomeFragment";
  83. @BindView(R.id.iv_add)
  84. ImageView mIvAdd;
  85. @BindView(R.id.fl_search)
  86. TextView mFlSearch;
  87. @BindView(R.id.indicator_sub_home)
  88. MagicIndicator magicIndicator;
  89. @BindView(R.id.vp_sub_home)
  90. ViewPager subHomePager;
  91. @BindView(R.id.iv_scan)
  92. TextView mIvScan;
  93. @BindView(R.id.appbar)
  94. AppBarLayout appBarLayout;
  95. @BindView(R.id.banner)
  96. Banner mBanner;
  97. @BindView(R.id.subRefresh)
  98. SmartRefreshLayout refreshLayout;
  99. @BindView(R.id.emptview)
  100. EmptyView emptyView;
  101. @BindView(R.id.fl_tittle)
  102. LinearLayout flTittle;
  103. private List<PDModle> mSelectedDates;
  104. private List<PDModle> mUnSelectedDates;
  105. BannerBean bannerBean;
  106. private PagerAdapter pagerAdapter;
  107. private int selectedIndex;
  108. @Override
  109. public void onLazyLoad() {
  110. getBannerData();
  111. }
  112. @Override
  113. protected int getContentLayoutId() {
  114. return R.layout.fragment_sub_home;
  115. }
  116. public void toTop(){
  117. CoordinatorLayout.Behavior behavior =
  118. ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
  119. if (behavior instanceof AppBarLayout.Behavior) {
  120. AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
  121. int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset();
  122. if (topAndBottomOffset != 0) {
  123. appBarLayoutBehavior.setTopAndBottomOffset(0);
  124. }
  125. }
  126. EventBus.getDefault().post(new TopAllEvent());
  127. }
  128. @Override
  129. protected void initWidget(View root) {
  130. super.initWidget(root);
  131. TranslucentStatusUtil.initState(getActivity(), flTittle);
  132. EventBus.getDefault().register(this);
  133. mBanner.setOnBannerListener(this);
  134. refreshLayout.setOnRefreshListener(this);
  135. subHomePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  136. @Override
  137. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  138. }
  139. @Override
  140. public void onPageSelected(int position) {
  141. selectedIndex = position;
  142. }
  143. @Override
  144. public void onPageScrollStateChanged(int state) {
  145. }
  146. });
  147. emptyView.bind(refreshLayout);
  148. setPlaceHolderView(emptyView);
  149. emptyView.triggerLoading();
  150. }
  151. @Override
  152. protected void initData() {
  153. super.initData();
  154. selectedIndex = 0;
  155. germSelectedDates();
  156. germUnSelectedDates();
  157. }
  158. private List<Integer> indexPosition;
  159. private void getBannerData() {
  160. OkGo.<String>get(Api.BANNER_LIST)
  161. .headers(Api.AUTHORIZATION, token)
  162. .execute(new StringCallback() {
  163. @Override
  164. public void onSuccess(Response<String> response) {
  165. if (response.code() == 200) {
  166. Log.d(TAG, "onSuccess: " + response.body());
  167. List<String> imgs = new ArrayList<>();
  168. indexPosition = new ArrayList();
  169. bannerBean = new Gson().fromJson(response.body(), BannerBean.class);
  170. for (int i = 0; i < bannerBean.getItems().size(); i++) {
  171. imgs.add(bannerBean.getItems().get(i).getImageUrl());
  172. indexPosition.add(bannerBean.getItems().get(i).getArticleId());
  173. }
  174. if (imgs.size() == 0) {
  175. mBanner.setVisibility(View.GONE);
  176. } else {
  177. mBanner.setVisibility(View.VISIBLE);
  178. mBanner.setImageLoader(new GlideImageLoader());
  179. mBanner.setImages(imgs);
  180. mBanner.start();
  181. }
  182. }
  183. mPlaceHolderView.triggerOk();
  184. refreshLayout.finishRefresh();
  185. }
  186. });
  187. }
  188. @Override
  189. public void OnBannerClick(int position) {
  190. Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class);
  191. intent.putExtra("id",indexPosition.get(position));
  192. startActivity(intent);
  193. }
  194. @OnClick({R.id.iv_add, R.id.fl_search, R.id.iv_scan})
  195. public void onViewClicked(View view) {
  196. switch (view.getId()) {
  197. case R.id.iv_add:
  198. PingdaoFragment dialogFragment = PingdaoFragment.newInstance(mSelectedDates, mUnSelectedDates,token);
  199. dialogFragment.show(getChildFragmentManager(), "CHANNEL");
  200. break;
  201. case R.id.fl_search:
  202. SearchActivity.tothis(getContext());
  203. break;
  204. case R.id.iv_scan:
  205. break;
  206. }
  207. }
  208. CommonNavigator commonNavigator;
  209. public CommonNavigator setCommonNavigator() {
  210. commonNavigator = new CommonNavigator(getContext());
  211. commonNavigator.setScrollPivotX(0.65f);
  212. commonNavigator.setAdjustMode(false);
  213. commonNavigator.setAdapter(new CommonNavigatorAdapter() {
  214. @Override
  215. public int getCount() {
  216. return mSelectedDates == null ? 0 : mSelectedDates.size();
  217. }
  218. @Override
  219. public IPagerTitleView getTitleView(Context context, final int index) {
  220. SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
  221. simplePagerTitleView.setText(mSelectedDates.get(index).getArticleGroupName());
  222. simplePagerTitleView.setNormalColor(getResources().getColor(R.color.text_color6));
  223. simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.text_color));
  224. simplePagerTitleView.setTextSize(16);
  225. simplePagerTitleView.setOnClickListener(v -> subHomePager.setCurrentItem(index, false));
  226. return simplePagerTitleView;
  227. }
  228. @Override
  229. public IPagerIndicator getIndicator(Context context) {
  230. LinePagerIndicator indicator = new LinePagerIndicator(context);
  231. indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
  232. indicator.setLineHeight(0);
  233. indicator.setRoundRadius(2);
  234. indicator.setStartInterpolator(new AccelerateInterpolator());
  235. indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
  236. indicator.setColors(getResources().getColor(R.color.text_blue));
  237. return indicator;
  238. }
  239. });
  240. return commonNavigator;
  241. }
  242. public void germSelectedDates() {
  243. mSelectedDates = new ArrayList<>();
  244. OkGo.<String>post(Api.MAIN_PINGDAOMY)
  245. .upJson(new JSONObject())
  246. .headers(Api.AUTHORIZATION, token)
  247. .execute(new StringCallback() {
  248. @Override
  249. public void onSuccess(Response<String> response) {
  250. try {
  251. if (response.code() == 200) {
  252. mSelectedDates = new Gson().fromJson(response.body(), new TypeToken<List<PDModle>>() {
  253. }.getType());
  254. if (mSelectedDates != null) {
  255. // 设置viewpager和adapter
  256. pagerAdapter = new PagerAdapter(getChildFragmentManager(), mSelectedDates);
  257. subHomePager.setAdapter(pagerAdapter);
  258. subHomePager.setOffscreenPageLimit(10);
  259. // 设置CommonNavigator
  260. magicIndicator.setNavigator(setCommonNavigator());
  261. ViewPagerHelper.bind(magicIndicator, subHomePager);
  262. }
  263. } else {
  264. ErrorResponse errorResponse =
  265. new Gson().fromJson(response.body(), ErrorResponse.class);
  266. if (errorResponse != null)
  267. showToast(errorResponse.getMessage());
  268. }
  269. } catch (Exception e) {
  270. Log.e(TAG, "onSuccess: " + e);
  271. }
  272. }
  273. @Override
  274. public void onError(Response<String> response) {
  275. super.onError(response);
  276. }
  277. });
  278. }
  279. public void germUnSelectedDates() {
  280. mUnSelectedDates = new ArrayList<>();
  281. OkGo.<String>get(Api.GET_CHANNELS)
  282. .headers(Api.AUTHORIZATION, token)
  283. .execute(new StringCallback() {
  284. @Override
  285. public void onSuccess(Response<String> response) {
  286. if (response.code() == 200) {
  287. mUnSelectedDates = new Gson().fromJson(response.body(), new TypeToken<List<PDModle>>() {
  288. }.getType());
  289. } else {
  290. ErrorResponse errorResponse =
  291. new Gson().fromJson(response.body(), ErrorResponse.class);
  292. if (errorResponse != null)
  293. showToast(errorResponse.getMessage());
  294. }
  295. }
  296. });
  297. }
  298. @Subscriber
  299. public void updateChannel(MainRefushEvent event) {
  300. initData();
  301. }
  302. //
  303. // @Subscriber
  304. // public void selectChannelEvent(SelectChannelEvent selectChannelEvent) {
  305. // if (selectChannelEvent == null) return;
  306. // List<String> integers = new ArrayList<>();
  307. // for (Channel channel : mSelectedDates) {
  308. // integers.add(channel.getName());
  309. // }
  310. // setViewpagerPosition(integers, selectChannelEvent.channelName);
  311. // }
  312. //
  313. // /**
  314. // * 设置 当前选中页
  315. // *
  316. // * @param integers
  317. // * @param channelName
  318. // */
  319. // private void setViewpagerPosition(List<String> integers, String channelName) {
  320. // if (TextUtils.isEmpty(channelName) || integers == null) return;
  321. // for (int j = 0; j < integers.size(); j++) {
  322. // if (integers.get(j).equals(channelName)) {
  323. // selectedChannel = integers.get(j);
  324. // selectedIndex = j;
  325. // break;
  326. // }
  327. // }
  328. // subHomePager.postDelayed(new Runnable() {
  329. // @Override
  330. // public void run() {
  331. // subHomePager.setCurrentItem(selectedIndex, false);
  332. // }
  333. // }, 100);
  334. // }
  335. @Override
  336. public void onRefresh(@NonNull RefreshLayout refreshLayout) {
  337. Log.e("mSelectedDates.size==",mSelectedDates.size()+"");
  338. if(mSelectedDates.size()==0)
  339. {
  340. refreshLayout.finishRefresh();
  341. return;
  342. }
  343. getBannerData();
  344. EventBus.getDefault().post(new MessageEvent("更新", mSelectedDates.get(selectedIndex).getArticleGroupId()));
  345. }
  346. @Subscriber
  347. public void Event(TopEvent messageEvent) {
  348. toTop();
  349. }
  350. }