| | |
| | | package com.hdl.photovoltaic.ui.message; |
| | | |
| | | import android.os.Bundle; |
| | | import android.os.SystemClock; |
| | | import android.view.View; |
| | | |
| | | |
| | | import androidx.fragment.app.Fragment; |
| | | import androidx.fragment.app.FragmentManager; |
| | | import androidx.fragment.app.FragmentTransaction; |
| | | |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.base.CustomBaseFragment; |
| | | import com.hdl.photovoltaic.config.ConstantManage; |
| | | import com.hdl.photovoltaic.databinding.FragmentMessageBinding; |
| | | import com.hdl.photovoltaic.enums.HomepageTitleTabSwitch; |
| | | import com.hdl.photovoltaic.enums.MessageFunctionTabSwitch; |
| | | import com.hdl.photovoltaic.other.HdlCommonLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.ui.bean.BUserInfo; |
| | | import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 消息 |
| | |
| | | public class MessageFragment extends CustomBaseFragment { |
| | | |
| | | private FragmentMessageBinding viewBinding; |
| | | |
| | | //当前显示的fragment |
| | | private static final String CURRENT_FRAGMENT = "state_fragment_show"; |
| | | private FragmentManager mFragmentManager; |
| | | private Fragment currentFragment = new Fragment(); |
| | | private final List<Fragment> fragmentList = new ArrayList<>(); |
| | | private int currentIndex = 0; |
| | | |
| | | @Override |
| | | public Object getContentView() { |
| | |
| | | @Override |
| | | public void onBindView(Bundle savedInstanceState) { |
| | | |
| | | //初始化碎片 |
| | | initFragment(savedInstanceState); |
| | | //初始化界面 |
| | | initView(); |
| | | //初始化监听器 |
| | |
| | | } |
| | | |
| | | private void initEvent() { |
| | | |
| | | //发生中 |
| | | viewBinding.messageTabNascentCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | currentIndex = 0; |
| | | initView(); |
| | | showFragment(); |
| | | |
| | | } |
| | | }); |
| | | //已恢复 |
| | | viewBinding.messageTabRecoverCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | currentIndex = 1; |
| | | initView(); |
| | | showFragment(); |
| | | |
| | | } |
| | | }); |
| | | |
| | | //全部 |
| | | viewBinding.messageTabAllCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.VISIBLE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | viewBinding.messageAllFcv.setVisibility(View.VISIBLE); |
| | | viewBinding.messageNascentFcv.setVisibility(View.GONE); |
| | | viewBinding.messageRecoverFcv.setVisibility(View.GONE); |
| | | |
| | | } |
| | | }); |
| | | |
| | | viewBinding.messageTabNascentCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.GONE); |
| | | viewBinding.line2V.setVisibility(View.VISIBLE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | viewBinding.messageAllFcv.setVisibility(View.GONE); |
| | | viewBinding.messageNascentFcv.setVisibility(View.VISIBLE); |
| | | viewBinding.messageRecoverFcv.setVisibility(View.GONE); |
| | | } |
| | | }); |
| | | |
| | | viewBinding.messageTabRecoverCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.GONE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line3V.setVisibility(View.VISIBLE); |
| | | viewBinding.messageAllFcv.setVisibility(View.GONE); |
| | | viewBinding.messageNascentFcv.setVisibility(View.GONE); |
| | | viewBinding.messageRecoverFcv.setVisibility(View.VISIBLE); |
| | | currentIndex = 2; |
| | | initView(); |
| | | showFragment(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | private void initView() { |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.VISIBLE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | viewBinding.messageAllFcv.setVisibility(View.VISIBLE); |
| | | viewBinding.messageNascentFcv.setVisibility(View.GONE); |
| | | viewBinding.messageRecoverFcv.setVisibility(View.GONE); |
| | | if (this.currentIndex == 0) { |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.VISIBLE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | } else if (this.currentIndex == 1) { |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.GONE); |
| | | viewBinding.line2V.setVisibility(View.VISIBLE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | } else if (this.currentIndex == 2) { |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabRecoverTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.line1V.setVisibility(View.GONE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line3V.setVisibility(View.VISIBLE); |
| | | } |
| | | } |
| | | |
| | | private void initData() { |
| | | } |
| | | |
| | | /** |
| | | * 发布点击事件 |
| | | * |
| | | * @param tabSwitch - |
| | | */ |
| | | private void postEventBus(MessageFunctionTabSwitch tabSwitch) { |
| | | BaseEventBus baseEventBus = new BaseEventBus(); |
| | | baseEventBus.setTopic(ConstantManage.message_function_tab_switch); |
| | | baseEventBus.setType(tabSwitch.toString()); |
| | | EventBus.getDefault().post(baseEventBus); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化Fragment |
| | | * |
| | | * @param savedInstanceState - |
| | | */ |
| | | private void initFragment(Bundle savedInstanceState) { |
| | | mFragmentManager = _mActivity.getSupportFragmentManager(); |
| | | //碎片初始化 |
| | | NascentMessageFragment mNascentMessageFragment = new NascentMessageFragment(); |
| | | RecoverMessageFragment mRecoverMessageFragment = new RecoverMessageFragment(); |
| | | AllMessageFragment mAllMessageFragment = new AllMessageFragment(); |
| | | if (savedInstanceState != null) { |
| | | //“内存重启”时调用 获取“内存重启”时保存的索引下标 |
| | | currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT, 0); |
| | | if (fragmentList.size() != 0) { |
| | | fragmentList.clear(); |
| | | } |
| | | fragmentList.add(mFragmentManager.findFragmentByTag(0 + "")); |
| | | fragmentList.add(mFragmentManager.findFragmentByTag(1 + "")); |
| | | fragmentList.add(mFragmentManager.findFragmentByTag(2 + "")); |
| | | restoreFragment();//恢复fragment页面 |
| | | } else { |
| | | //正常启动时调用 |
| | | fragmentList.add(mNascentMessageFragment); |
| | | fragmentList.add(mRecoverMessageFragment); |
| | | fragmentList.add(mAllMessageFragment); |
| | | showFragment(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 使用show() hide()切换页面 |
| | | * 显示fragment |
| | | */ |
| | | private void showFragment() { |
| | | FragmentTransaction ft = mFragmentManager.beginTransaction(); |
| | | //如果之前没有添加过 |
| | | if (!fragmentList.get(currentIndex).isAdded()) { |
| | | //第三个参数为添加当前的fragment时绑定一个tag |
| | | ft.hide(currentFragment).add(R.id.message_fcv, fragmentList.get(currentIndex), currentIndex + ""); |
| | | } else { |
| | | ft.hide(currentFragment).show(fragmentList.get(currentIndex)); |
| | | } |
| | | currentFragment = fragmentList.get(currentIndex); |
| | | ft.commit(); |
| | | //把当前显示的fragment记录下来 |
| | | currentFragment = fragmentList.get(currentIndex); |
| | | |
| | | if (this.currentIndex == 0) { |
| | | postEventBus(MessageFunctionTabSwitch.nascent); |
| | | } else if (this.currentIndex == 1) { |
| | | postEventBus(MessageFunctionTabSwitch.recover); |
| | | } else if (this.currentIndex == 2) { |
| | | postEventBus(MessageFunctionTabSwitch.all); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 恢复fragment |
| | | */ |
| | | private void restoreFragment() { |
| | | FragmentTransaction ft = mFragmentManager.beginTransaction(); |
| | | for (int i = 0; i < fragmentList.size(); i++) { |
| | | if (i == currentIndex) { |
| | | ft.show(fragmentList.get(i)); |
| | | } else { |
| | | ft.hide(fragmentList.get(i)); |
| | | } |
| | | } |
| | | ft.commit(); |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onEventMessage(BaseEventBus eventBus) { |
| | | super.onEventMessage(eventBus); |
| | | //接收外部点击事件 |
| | | if (eventBus.getTopic().equals(ConstantManage.homepage_title_tab_switch)) { |
| | | if (eventBus.getType().equals(HomepageTitleTabSwitch.message.toString())) { |
| | | postEventBus(MessageFunctionTabSwitch.nascent); |
| | | HdlLogLogic.print("正在点击【消息】"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |