TrendsHomeFragment.java 21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package com.yidianling.dynamic.trendsHome;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ydl.ydl_image.module.GlideApp;
import com.ydl.ydlcommon.base.BaseFragment;
import com.ydl.ydlcommon.utils.BuryPointUtils;
import com.ydl.ydlcommon.utils.ScreenUtil;
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils;
import com.yidianling.common.tools.RxDeviceTool;
import com.yidianling.common.tools.RxImageTool;
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.bean.DynamicConstants;
import com.yidianling.dynamic.common.view.LooperTextView;
import com.yidianling.dynamic.dialog.TrendsAndQuestionDialog;
import com.yidianling.dynamic.dialog.listener.TrendsQuestionListener;
import com.yidianling.dynamic.model.TopicListDataBean;
import com.yidianling.dynamic.model.TrendsListBean;
import com.yidianling.dynamic.publishTrend.PublishTrendActivity;
import com.yidianling.dynamic.router.DynamicIn;
import com.yidianling.dynamic.topic.AllTopicActivity;
import com.yidianling.dynamic.topic.topicDetail.TopicDetailActivity;
import com.yidianling.dynamic.trendsHome.content.TrendsContentListFragment;
import com.yidianling.dynamic.trendsHome.view.TrendsHomeBannerView;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_ASK_PAGE_STAY_VISIT;
import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_ASK_PAGE_VISIT;
import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_ASK_QUESTIONS_BUTTON_CLICK;
import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_ASK_TYPE_CLICK;
import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_THOUGHTS_GROUP_MORE_CLICK;
import static com.yidianling.dynamic.bean.DynamicConstants.YDL_USER_THOUGHTS_GROUP_TYPE_CLICK;

/**
 * Created by Jim on 2017/9/22 0022.
 * 首页问答fragment
 */
@Route(path = "/dynamic/dynamic")
public class TrendsHomeFragment extends BaseFragment implements ITrendHomeView, SwipeRefreshLayout.OnRefreshListener {

万齐军 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    private SwipeRefreshLayout swl;

    private ViewPager vpContent;

    private ImageView topicRoundIv1;

    private ImageView topicRoundIv2;

    private ImageView topicRoundIv3;

    private ConstraintLayout mConstraintLayout;

    private TextView tvMindGroup;

    private TextView tvMindMore;

    private FrameLayout frlTopicIv3;

    private FrameLayout frlTopicIv2;

    private FrameLayout frlTopicIv1;

    private AppBarLayout mAppBarLayout;
    private TextView mItemRecommendTv;
    private TextView mItemHotTv;
    private TextView mItemFocusTv;
    private TextView trendsTopicTv3;
    private TextView trendsTopicTv2;
    private TextView trendsTopicTv1;
    private FloatingActionButton mPublishTrendButton;
    private LooperTextView mLooperTextView;
    private Toolbar mToolBarTv;
    private TrendsHomeBannerView v_banner;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    private TrendsHomePresenter mPresenter;
    private Fragment[] mFragments;
    private TrendsHomeVpAdapter vpAdapter;
    private long lastTabClick = 0;
    private int currentPostion = -1;
    String tab;
    private long startTime;
    private long endTime;
    private TrendsAndQuestionDialog trendsAndQuestionDialog = null;

    public TrendsHomeFragment setTab(String tab) {
        this.tab = tab;
        return this;
    }

    public static TrendsHomeFragment newInstance() {
        return new TrendsHomeFragment();
    }


    @Override
    public int layoutResId() {
        return R.layout.dynamic_fragment_moments;
    }

    @Override
    public void initDataAndEvent() {
万齐军 committed
120
        bindView(getView());
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        initViews();
        initDatas();

    }

    @Override
    public void initDataAndEventLazy() {
//        initDatas();
    }

    private void initViews() {
        initBannerView();
        swl.setOnRefreshListener(this);
        swl.setColorSchemeResources(R.color.platform_main_theme);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    swl.setEnabled(true);
                } else {
                    swl.setEnabled(false);
                }
                if (verticalOffset <= -appBarLayout.getTotalScrollRange() + mToolBarTv.getHeight()) {
                    mToolBarTv.setAlpha(1f);
                } else {
                    mToolBarTv.setAlpha(0f);
                }
            }
        });
        initStatus();
    }

    private void initStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4以下不支持状态栏变色
            int statusBarHeight = ScreenUtil.getStatusBarHeight(getActivity());
            CollapsingToolbarLayout.LayoutParams layoutParams = (CollapsingToolbarLayout.LayoutParams) mToolBarTv.getLayoutParams();
            layoutParams.height = (int) (getResources().getDimension(R.dimen.title_bar_height) + statusBarHeight);
            mToolBarTv.setPadding(0, statusBarHeight, 0, 0);
            mToolBarTv.setLayoutParams(layoutParams);

        }
    }

    private void initDatas() {
        mPresenter = new TrendsHomePresenter();
        mPresenter.callBack = this;
        mPresenter.fetchDats();
    }

    private void initBannerView() {
        int screenSize = RxDeviceTool.getScreenWidth(getActivity());
        int topicRoundIv1Width = (screenSize - RxImageTool.dip2px(31)) * 2 / 3;
        int topicRoundIv23Width = (screenSize - RxImageTool.dip2px(31)) / 3;

        int topicRoundIv1Height = (int) (topicRoundIv1Width / 1.5f);
        int topicRoundIv23Height = (int) ((topicRoundIv1Height - RxImageTool.dip2px(1)) / 2f);

        ConstraintLayout.LayoutParams params1 = new ConstraintLayout.LayoutParams(topicRoundIv1Width, topicRoundIv1Height);
        params1.topMargin = RxImageTool.dp2px(10);
        params1.leftMargin = RxImageTool.dp2px(15);
        params1.leftToRight = mConstraintLayout.getId();
        params1.topToBottom = tvMindGroup.getId();
        frlTopicIv1.setLayoutParams(params1);

        ConstraintLayout.LayoutParams params2 = new ConstraintLayout.LayoutParams(topicRoundIv23Width, topicRoundIv23Height);
        params2.topMargin = RxImageTool.dp2px(10);
        params2.leftMargin = RxImageTool.dp2px(1);
        params2.leftToRight = frlTopicIv1.getId();
        params2.topToBottom = tvMindGroup.getId();
        frlTopicIv2.setLayoutParams(params2);

        ConstraintLayout.LayoutParams params3 = new ConstraintLayout.LayoutParams(topicRoundIv23Width, topicRoundIv23Height);
        params3.topMargin = RxImageTool.dp2px(1);
        params3.leftMargin = RxImageTool.dp2px(1);
        params3.leftToRight = frlTopicIv1.getId();
        params3.topToBottom = frlTopicIv2.getId();
        frlTopicIv3.setLayoutParams(params3);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isResumed()) {
            startTime = System.currentTimeMillis();
            ActionCountUtils.Companion.count(YDL_USER_ASK_PAGE_VISIT);
        } else {
            if (startTime != 0L) {
                endTime = System.currentTimeMillis();
                ActionCountUtils.Companion.count(YDL_USER_ASK_PAGE_STAY_VISIT, String.valueOf(endTime - startTime));
            }
        }
        if (isVisibleToUser && isResumed()) {
            onResume();
        }
    }

    private void initViewPager() {
        vpAdapter = new TrendsHomeVpAdapter(getChildFragmentManager(), mFragments);
        vpContent.setAdapter(vpAdapter);
        vpContent.setOffscreenPageLimit(1);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == currentPostion) {
                    return;
                }
                currentPostion = position;
                if (position >= 1) {
                    vpContent.setOffscreenPageLimit(2);
                }

                switchTab(position);
                switch (position) {
                    case 0:
                        BuryPointUtils.getInstance().createMap()
                                .put("toptab_name", "推荐")
                                .burryPoint("topTabClick");
                        BuryPointUtils.getInstance().trackViewScreen(getActivity(), "动态-推荐", "com.cxzapp.yidianling.trends.trendList.TrendsListFragment");
                        break;
                    case 1:
                        BuryPointUtils.getInstance().createMap()
                                .put("toptab_name", "最新")
                                .burryPoint("topTabClick");
                        BuryPointUtils.getInstance().trackViewScreen(getActivity(), "动态-最新", "com.cxzapp.yidianling.trends.trendList.TrendsListFragment");
                        break;
                    case 2:
                        BuryPointUtils.getInstance().createMap()
                                .put("toptab_name", "关注")
                                .burryPoint("topTabClick");
                        if (!DynamicIn.INSTANCE.loginByOneKeyLogin(getActivity(), true)) {
                            return;
                        }
                        BuryPointUtils.getInstance().trackViewScreen(getActivity(), "动态-关注", "com.cxzapp.yidianling.trends.trendList.TrendsListFragment");
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if ("hot".equals(tab)) {
            vpContent.setCurrentItem(1);
            switchTab(1);
        } else if ("new".equals(tab)) {
            vpContent.setCurrentItem(0);
            switchTab(0);
        } else {
            vpContent.setCurrentItem(0);
            switchTab(0);
        }
    }


    private void switchTab(int index) {
        switch (index) {
            case 0:
                mItemRecommendTv.setSelected(false);
                mItemRecommendTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                mItemFocusTv.setSelected(false);
                mItemFocusTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                mItemHotTv.setSelected(true);
                mItemHotTv.setTextColor(getResources().getColor(R.color.platform_but_text_color));
                break;
            case 1:
                mItemRecommendTv.setSelected(true);
                mItemRecommendTv.setTextColor(getResources().getColor(R.color.platform_but_text_color));
                mItemFocusTv.setSelected(false);
                mItemFocusTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                mItemHotTv.setSelected(false);
                mItemHotTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                break;
            case 2:
                mItemRecommendTv.setSelected(false);
                mItemRecommendTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                mItemFocusTv.setSelected(true);
                mItemFocusTv.setTextColor(getResources().getColor(R.color.platform_but_text_color));
                mItemHotTv.setSelected(false);
                mItemHotTv.setTextColor(getResources().getColor(R.color.dynamic_text_666666));
                break;
        }
    }


万齐军 committed
312
    private void doClick(View view) {
313 314 315 316 317 318 319 320 321 322 323
        if (view.getId() == R.id.item_recommend_tv) {
            ActionCountUtils.Companion.count(YDL_USER_ASK_TYPE_CLICK, "推荐");
            vpContent.setCurrentItem(1);
        } else if (view.getId() == R.id.item_hot_tv) {
            ActionCountUtils.Companion.count(YDL_USER_ASK_TYPE_CLICK, "最新");
            vpContent.setCurrentItem(0);
        } else if (view.getId() == R.id.item_focus_tv) {
            ActionCountUtils.Companion.count(YDL_USER_ASK_TYPE_CLICK, "关注");
            vpContent.setCurrentItem(2);
        } else if (view.getId() == R.id.publish_trend_btn) {
            ActionCountUtils.Companion.count(YDL_USER_ASK_QUESTIONS_BUTTON_CLICK);
霍志良 committed
324 325 326 327 328
            if (DynamicIn.INSTANCE.loginByOneKeyLogin(getActivity(), true)) {
                trendsAndQuestionDialog = new TrendsAndQuestionDialog(requireContext(), new TrendsQuestionListener() {
                    @Override
                    public void publishTrends() {
                        //发布动态
329 330 331 332 333 334
                        Intent intent = new Intent(getActivity().getApplicationContext(), PublishTrendActivity.class);
                        //默认改为今日打卡
                        Bundle bundle = new Bundle();
                        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
                        bundle.putString("topic_id", "44");
                        bundle.putString("topic_title", "今日打卡");
霍志良 committed
335
                        bundle.putInt("dynamic_question", 1);
336 337 338 339 340
                        intent.putExtra("bundle", bundle);
                        startActivity(intent);
//                        PublishTrendActivityTrends.Companion.start(requireContext(),"44","今日打卡");
                    }

霍志良 committed
341 342
                    @Override
                    public void publishQuestion() {
霍志良 committed
343
                        //提问题
344 345 346 347 348 349
                        Intent intent = new Intent(getActivity().getApplicationContext(), PublishTrendActivity.class);
                        //默认改为今日打卡
                        Bundle bundle = new Bundle();
                        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
                        bundle.putString("topic_id", "44");
                        bundle.putString("topic_title", "今日打卡");
霍志良 committed
350
                        bundle.putInt("dynamic_question", 2);
351 352 353
                        intent.putExtra("bundle", bundle);
                        startActivity(intent);
                    }
霍志良 committed
354 355 356
                });
                trendsAndQuestionDialog.show();
            }
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
        } else if (view.getId() == R.id.trends_tv_mind_more) {
            ActionCountUtils.Companion.count(YDL_USER_THOUGHTS_GROUP_MORE_CLICK);
            Intent intent = new Intent(getActivity(), AllTopicActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
//        else if (view.getId() == R.id.iv_background) {
//            YDLRouterManager.Companion.router(IYDLRouterConstant.ROUTER_FM_LIST);
//        }
    }

    @Override
    public void onFetchedDatas(Fragment[] fragments) {
        mFragments = fragments;
        initViewPager();
    }

    public void setBannerData(List<TrendsListBean.Banner> list) {
        v_banner.initData(list);
    }

    @Override
    public void onFetchedDataFailed() {

    }

    public void displayShowCase() {
    }

    @Override
    public void onRefresh() {
        swl.setRefreshing(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ((TrendsContentListFragment) mFragments[vpContent.getCurrentItem()]).onRefresh();
                swl.setRefreshing(false);
            }
        }, 300);
    }

    public void showMindsGroup(TopicListDataBean topicListDatas) {
        if (topicListDatas.list == null || topicListDatas.list.size() <= 0) {
            return;
        }
        for (int i = 0; i < 3 && i < topicListDatas.list.size(); i++) {
            switch (i) {
                case 0:
                    GlideApp.with(this)
                            .load(topicListDatas.list.get(i).getImg_bg_url())
                            .centerCrop()
                            .transform(new RoundedCornersTransformation(20, 0, RoundedCornersTransformation.CornerType.LEFT))
                            .into(topicRoundIv1);
                    trendsTopicTv1.setText(topicListDatas.list.get(i).getTopic_title());
                    frlTopicIv1.setOnClickListener(view -> {
                        mindTopicClick(topicListDatas.list.get(0).getTopic_title(), topicListDatas.list.get(0).getTopic_id(), trendsTopicTv1.getText().toString());
                    });
                    break;
                case 1:
                    GlideApp.with(this)
                            .load(topicListDatas.list.get(i).getImg_bg_url())
                            .centerCrop()
                            .transform(new RoundedCornersTransformation(20, 0, RoundedCornersTransformation.CornerType.TOP_RIGHT))
                            .into(topicRoundIv2);
                    trendsTopicTv2.setText(topicListDatas.list.get(i).getTopic_title());
                    frlTopicIv2.setOnClickListener(view -> {
                        mindTopicClick(topicListDatas.list.get(1).getTopic_title(), topicListDatas.list.get(1).getTopic_id(), trendsTopicTv2.getText().toString());
                    });
                    break;
                case 2:
                    GlideApp.with(this)
                            .load(topicListDatas.list.get(i).getImg_bg_url())
                            .centerCrop()
                            .transform(new RoundedCornersTransformation(20, 0, RoundedCornersTransformation.CornerType.BOTTOM_RIGHT))
                            .into(topicRoundIv3);
                    trendsTopicTv3.setText(topicListDatas.list.get(i).getTopic_title());
                    frlTopicIv3.setOnClickListener(view -> {
                        mindTopicClick(topicListDatas.list.get(2).getTopic_title(), topicListDatas.list.get(2).getTopic_id(), trendsTopicTv3.getText().toString());
                    });
                    break;
            }
        }


    }

    private void mindTopicClick(String title, String id, String imgName) {
        BuryPointUtils.getInstance().createMap()
                .put("theme_name", title)
                .put("theme_ID", id)
                .burryPoint("Theme_click");
        ActionCountUtils.Companion.count(YDL_USER_THOUGHTS_GROUP_TYPE_CLICK, imgName);
        Intent intent = new Intent();
        intent.setClass(getActivity(), TopicDetailActivity.class);
        intent.putExtra("topic_id", id);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getActivity().startActivity(intent);
    }

    public void showTopTrend(List<TrendsListBean.ExtData> extDatas) {
        mLooperTextView.setTipList(extDatas);
    }

    public void setGoneLooperTextView() {
        mLooperTextView.setVisibility(View.GONE);
    }
万齐军 committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

    private void bindView(View bindSource) {
        swl = bindSource.findViewById(R.id.trend_home_swl);
        vpContent = bindSource.findViewById(R.id.vp_content);
        topicRoundIv1 = bindSource.findViewById(R.id.trends_topic_iv1);
        topicRoundIv2 = bindSource.findViewById(R.id.trends_topic_iv2);
        topicRoundIv3 = bindSource.findViewById(R.id.trends_topic_iv3);
        mConstraintLayout = bindSource.findViewById(R.id.trends_home_csl);
        tvMindGroup = bindSource.findViewById(R.id.trends_tv_mind_group);
        tvMindMore = bindSource.findViewById(R.id.trends_tv_mind_more);
        frlTopicIv3 = bindSource.findViewById(R.id.frl_topic_iv3);
        frlTopicIv2 = bindSource.findViewById(R.id.frl_topic_iv2);
        frlTopicIv1 = bindSource.findViewById(R.id.frl_topic_iv1);
        mAppBarLayout = bindSource.findViewById(R.id.trends_home_appBar);
        mItemRecommendTv = bindSource.findViewById(R.id.item_recommend_tv);
        mItemHotTv = bindSource.findViewById(R.id.item_hot_tv);
        mItemFocusTv = bindSource.findViewById(R.id.item_focus_tv);
        trendsTopicTv3 = bindSource.findViewById(R.id.trends_topic_tv3);
        trendsTopicTv2 = bindSource.findViewById(R.id.trends_topic_tv2);
        trendsTopicTv1 = bindSource.findViewById(R.id.trends_topic_tv1);
        mPublishTrendButton = bindSource.findViewById(R.id.publish_trend_btn);
        mLooperTextView = bindSource.findViewById(R.id.ternd_loop_tv);
        mToolBarTv = bindSource.findViewById(R.id.toolbar_tv);
        v_banner = bindSource.findViewById(R.id.v_banner);
        tvMindMore.setOnClickListener(v -> {
            doClick(v);
        });
        mPublishTrendButton.setOnClickListener(v -> {
            doClick(v);
        });
        mItemRecommendTv.setOnClickListener(v -> {
            doClick(v);
        });
        mItemHotTv.setOnClickListener(v -> {
            doClick(v);
        });
        mItemFocusTv.setOnClickListener(v -> {
            doClick(v);
        });
    }
503
}