RecommendFragment.java 14.2 KB
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 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 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 120 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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
package com.wd.capability.layout.fragment;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.wd.capability.layout.R;
import com.wd.capability.layout.comp.layoutdata.Page;
import com.wd.capability.layout.comp.layoutmanager.BaseAdapter;
import com.wd.capability.layout.comp.layoutmanager.ILayoutRender;
import com.wd.capability.layout.comp.layoutmanager.ItemLayoutManager;
import com.wd.capability.layout.comp.layoutmanager.LayoutAdapter;
import com.wd.capability.layout.ui.commonpage.TemplatePageDataFetcher;
import com.wd.capability.layout.ui.commonpage.TemplatePageDataListener;
import com.wd.capability.layout.ui.commonpage.TemplatePageDataViewModel;
import com.wd.capability.layout.ui.widget.ColumnRecyclerView;
import com.wd.capability.layout.ui.widget.itemhelp.ItemStateChangeListener;
import com.wd.capability.layout.uitls.CompentLogicUtil;
import com.wd.capability.network.constant.EventConstants;
import com.wd.common.base.BaseLazyFragment;
import com.wd.common.decoration.Decoration;
import com.wd.foundation.bean.RecListBean;
import com.wd.foundation.bean.custom.MenuBean;
import com.wd.foundation.bean.custom.comp.CompDataSourceBean;
import com.wd.foundation.bean.custom.comp.PageBean;
import com.wd.foundation.bean.livedate.EventMessage;
import com.wd.foundation.wdkit.constant.IntentConstants;
import com.wd.foundation.wdkit.utils.SafeBundleUtil;
import com.wd.foundation.wdkitcore.livedata.LiveDataBus;
import com.wd.foundation.wdkitcore.tools.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Description: 信息流页面一个模板(相关推荐列表 , 便于使用组件和稿件资源)
 * @Author: Li Yubing
 * @Email: liyubing@wondert.com.cn
 * @CreateDate: 2023/8/14 18:46
 * @Version: 1.0
 * 数据请求:{@link TemplatePageDataFetcher}
 */
public class RecommendFragment extends BaseLazyFragment {
    private static final String TAG = "RecommendFragment";

    private ILayoutRender layoutRender;

    /**
     * 总页面容器
     */
    private FrameLayout mFrameLayout;

    /**
     * 信息流页面容器
     */
    private FrameLayout contentFrameLayout;

    /**
     * recyclerview背景
     */
    protected View viewBg;

    /**
     * 当前页数据
     */
    private PageBean mPageBean;
    private Page mPage;
    /**
     * 页面是否已经有数据显示
     */
    private boolean isHashLoadDataInPage = false;

    /**
     * 内容存储器
     */
    private ColumnRecyclerView mRecyclerView;

    private int pageNum = 1;

    private MenuBean menuBean;

    private String channelId;

    TemplatePageDataViewModel templatePageDataViewModel;

    /**
     * 来源类型
     */
    private String dataSourceType = "";

    /**
     * 推荐列表数据
     */
    private RecListBean recListBean;
    /**
     * 获取Fragment实例对象
     *
     * @param menuBean 数据id
     * @return Fragment实例
     */
    public static RecommendFragment newInstance(MenuBean menuBean) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentConstants.PAGE_INFOR_DATA, menuBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 获取Fragment实例对象
     *
     * @param menuBean 数据id
     * @return Fragment实例
     */
    public static RecommendFragment newInstance(MenuBean menuBean, RecListBean recListBean) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentConstants.PAGE_INFOR_DATA, menuBean);
        bundle.putSerializable(IntentConstants.OPER_DATA_LIST, recListBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 获取Fragment实例对象
     *
     * @param menuBean 数据id
     * @return Fragment实例
     */
    public static RecommendFragment newInstance(MenuBean menuBean, int topMarginInt, String channelId) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentConstants.PAGE_INFOR_DATA, menuBean);
        bundle.putInt(IntentConstants.PARAM_TOPMARGININT, topMarginInt);
        bundle.putString(IntentConstants.PARAM_CHANNEL_ID, channelId);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void perCreate() {
        super.perCreate();
        //首页启动速度用户,使用java布局
        setIsjava(true);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int topMarginInt = SafeBundleUtil.getInt(getArguments(), IntentConstants.PARAM_TOPMARGININT, 0);
        if (topMarginInt != 0) {
            mFrameLayout.setPadding(0, topMarginInt, 0, 0);
        }
    }

    @Override
    protected String getLogTag() {
        return TAG;
    }

    @Override
    protected int getLayout() {
        return 0;
    }

    @Override
    protected void initView(View rootView) {
        initViewModel();
        registerBus();
    }
    /**
     * 注册LiveBus
     */
    private void registerBus() {
        //接受关注事件
        LiveDataBus.getInstance().with(EventConstants.FRESH_FOLLOW_CREATOR_EVENT, EventMessage.class).observe(getViewLifecycleOwner(), mEventMessage -> {
            if (mEventMessage == null) {
                return;
            }
            if (null != mRecyclerView && null != mRecyclerView.getAdapter()) {
                // 同步关注信息
                List<ItemLayoutManager> layoutManagers = layoutRender.getAllSectionLayoutManager();
                CompentLogicUtil.updateUserFollowInforLayout(mEventMessage, layoutManagers);
            }
        });

        //接受点赞事件
        LiveDataBus.getInstance().with(EventConstants.FRESH_ZAN_CREATOR_EVENT, EventMessage.class).observe(getViewLifecycleOwner(), mEventMessage -> {
            if (mEventMessage == null) {
                return;
            }
            if (null != mRecyclerView && null != mRecyclerView.getAdapter()) {
                // 同步关注信息
                List<ItemLayoutManager> layoutManagers = layoutRender.getAllSectionLayoutManager();
                CompentLogicUtil.updateUserZanInforLayout(mEventMessage, layoutManagers);
            }
        });

        //接受预约状态事件
        LiveDataBus.getInstance().with(EventConstants.FRESH_APPONINTMENT_STATUS_EVENT, EventMessage.class).observe(getViewLifecycleOwner(), mEventMessage -> {
            if (mEventMessage == null) {
                return;
            }
            if (null != mRecyclerView && null != mRecyclerView.getAdapter()) {
                // 同步关注信息
                List<ItemLayoutManager> layoutManagers = layoutRender.getAllSectionLayoutManager();
                CompentLogicUtil.updateCompLiveAppointStatus(mEventMessage, layoutManagers);
            }
        });
        //设置文字大小发消息通知刷新
        LiveDataBus.getInstance().with(EventConstants.FONT_SIZE_SET_SUCCESS, Boolean.class).observe(getViewLifecycleOwner(), aBoolean -> {
            if (aBoolean) {
                if (null != mRecyclerView && null != mRecyclerView.getAdapter()) {
                    mRecyclerView.getAdapter().notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //适老化-设置文字大小
        LiveDataBus.getInstance()
                .with(EventConstants.FONT_SIZE_SET_SUCCESS, Boolean.class).removeObservers(this);
    }

    private void initViewModel() {
        templatePageDataViewModel = getViewModelThis(TemplatePageDataViewModel.class);
        templatePageDataViewModel.setChannelId(channelId);
        templatePageDataViewModel.observeTemplatePageDataListener(this, new TemplatePageDataListener() {

            @Override
            public void onPageDataSuccess(Page page, PageBean data) {
                mPage = page;
                mPageBean = data;
                isHashLoadDataInPage = true;
                doPageDataSuccess();
            }

            @Override
            public void onInsertDataToPage() {

            }

            @Override
            public void onPageDataSetChanged(int startIndex, int totalNum) {

            }

            @Override
            public void onGetFailed(int code, String failedMsg) {

            }
        });

    }

    @Override
    protected View getJavaLayout() {
        menuBean = (MenuBean) SafeBundleUtil.getSerializable(getArguments(),
                IntentConstants.PAGE_INFOR_DATA);
        channelId = SafeBundleUtil.getString(getArguments(),
                IntentConstants.PARAM_CHANNEL_ID,"");
        if(menuBean != null){
            dataSourceType = menuBean.dataSourceType;
        }
        if(StringUtils.isEqual(CompDataSourceBean.RECOMMEND_LIST,dataSourceType)){
            recListBean = (RecListBean) SafeBundleUtil.getSerializable(getArguments(),
                    IntentConstants.OPER_DATA_LIST);
        }
        mFrameLayout = new FrameLayout(activity);
        mFrameLayout.setBackgroundColor(0x00FFFFFF);
        FrameLayout subLayout = getSubLayout();
        mFrameLayout.addView(subLayout);

        return mFrameLayout;
    }

    private FrameLayout getSubLayout() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        contentFrameLayout = new FrameLayout(activity);
        contentFrameLayout.setLayoutParams(params);
        viewBg = new View(activity);
        viewBg.setAlpha(0);
        viewBg.setBackgroundColor(ContextCompat.getColor(activity, R.color.res_color_common_C8));
        contentFrameLayout.addView(viewBg);
        mRecyclerView = (ColumnRecyclerView) LayoutInflater.from(activity).
                inflate(R.layout.page_layout_columnrecyclerview, null);
        contentFrameLayout.addView(mRecyclerView);
        return contentFrameLayout;
    }

    /**
     * 处理数据获取成功后续逻辑
     */
    private void doPageDataSuccess() {

        if (mPage == null) {
            return;
        }
        mPage.setFragment(RecommendFragment.this);

        initCommonRecyclerView();

        if (layoutRender != null) {
            layoutRender.renderPage(mPage, true);
            layoutRender.notifyDataSetChanged();
        }

    }

    private void initCommonRecyclerView() {

        mRecyclerView.setNestedScrollingEnabled(true);

        if (layoutRender != null) {
            layoutRender.releaseLayoutManagers();
        }
        layoutRender = new LayoutAdapter();
        // 组件内的滚动事件
        mRecyclerView.setItemViewCacheSize(4);
        mRecyclerView.setDrawingCacheEnabled(true);
        mRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 12, RecyclerView.VERTICAL, false);
        //  mRecyclerView.setOnAllowEnableLoadMoreListener(onAllowEnableLoadMoreListener);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (layoutRender.getAllSectionLayoutManager().size() == 0) {
                    return 0;
                }
                return 12 / layoutRender.getAllSectionLayoutManager().get(position).getItemSpan();
            }
        });
        //  mRecyclerView.setItemAnimator(new NoAlphaItemAnimator());
        // 关闭动画,防止刷新闪烁
        RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }

        RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool();
        pool.setMaxRecycledViews(0, 10);
        mRecyclerView.setRecycledViewPool(pool);
        mRecyclerView.getItemAnimator().setChangeDuration(0);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new Decoration());
        mRecyclerView.setAdapter((BaseAdapter) layoutRender);

        mRecyclerView.openGlobalLayout();
        mRecyclerView.itemVisibilityHelper.attachToRecyclerView(mRecyclerView, R.id.player_container,
                true, RecyclerView.VERTICAL, false, new ItemStateChangeListener() {

            @Override
            public void onActivateItem(@NotNull View view, int position) {
                super.onActivateItem(view, position);
                ((LayoutAdapter) layoutRender).onItemVisible(position);
            }

            @Override
            public void onDeactivateItem(@NotNull View view, int position) {
                super.onDeactivateItem(view, position);
                ((LayoutAdapter) layoutRender).onItemInVisible(position);
            }

            @Override
            public void onPauseItem(@NotNull View view, int position) {
                super.onPauseItem(view, position);
                ((LayoutAdapter) layoutRender).onItemInVisible(position);
            }

            @Override
            public void onResumeItem(@NotNull View view, int position) {
                super.onResumeItem(view, position);
                ((LayoutAdapter) layoutRender).onItemVisible(position);
            }
        });
    }


    @Override
    protected void lazyLoadData() {
        refreshData();
    }

    /**
     * 刷新请求数据
     */
    private void refreshData() {
        pageNum = 1;
        templatePageDataViewModel.sendPageDataRequest(getContext(),pageNum,20,menuBean,recListBean);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (layoutRender != null){
            layoutRender.notifyDataSetChanged();
        }
    }
}