| | |
| | | package com.hdl.photovoltaic.ui.message; |
| | | |
| | | import android.os.Build; |
| | | import android.os.Bundle; |
| | | import android.os.Handler; |
| | | import android.view.View; |
| | | |
| | | |
| | | import androidx.annotation.RequiresApi; |
| | | 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 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 List<Fragment> fragments = new ArrayList<>(); |
| | | private int currentIndex = 0; |
| | | private AllMessageFragment mAllMessageFragment; |
| | | private NascentMessageFragment mNascentMessageFragment; |
| | | private RecoverMessageFragment mRecoverMessageFragment; |
| | | |
| | | @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) { |
| | | 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); |
| | | showFragment(0); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.nascent); |
| | | } |
| | | }); |
| | | //已恢复 |
| | | 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); |
| | | showFragment(1); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.recover); |
| | | } |
| | | }); |
| | | |
| | | //全部 |
| | | viewBinding.messageTabAllCl.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | |
| | | 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); |
| | | showFragment(2); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.all); |
| | | |
| | | } |
| | | }); |
| | | |
| | | 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); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.nascent); |
| | | } |
| | | }); |
| | | |
| | | 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); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.recover); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | private void initView() { |
| | | viewBinding.messageTabAllTitleTv.setTextColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.messageTabNascentTitleTv.setTextColor(getResources().getColor(R.color.text80_FFFFFF, null)); |
| | | 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.VISIBLE); |
| | | viewBinding.line2V.setVisibility(View.GONE); |
| | | viewBinding.line1V.setVisibility(View.GONE); |
| | | viewBinding.line2V.setVisibility(View.VISIBLE); |
| | | viewBinding.line3V.setVisibility(View.GONE); |
| | | viewBinding.messageAllFcv.setVisibility(View.VISIBLE); |
| | | viewBinding.messageNascentFcv.setVisibility(View.GONE); |
| | | viewBinding.messageRecoverFcv.setVisibility(View.GONE); |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.all); |
| | | if (this.currentIndex == 0) { |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.nascent); |
| | | } else if (this.currentIndex == 1) { |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.recover); |
| | | } else if (this.currentIndex == 2) { |
| | | postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.all); |
| | | } |
| | | } |
| | | |
| | | private void initData() { |
| | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onResume() { |
| | | super.onResume(); |
| | | /** |
| | | * 初始化Fragment |
| | | * |
| | | * @param savedInstanceState - |
| | | */ |
| | | private void initFragment(Bundle savedInstanceState) { |
| | | mFragmentManager = _mActivity.getSupportFragmentManager(); |
| | | //碎片内需要使用权限和forresult |
| | | |
| | | new Handler().postDelayed(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | viewBinding.messageAllFcv.setVisibility(View.VISIBLE); |
| | | mNascentMessageFragment = new NascentMessageFragment(); |
| | | mRecoverMessageFragment = new RecoverMessageFragment(); |
| | | mAllMessageFragment = new AllMessageFragment(); |
| | | if (savedInstanceState != null) { |
| | | //“内存重启”时调用 获取“内存重启”时保存的索引下标 |
| | | currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT, 0); |
| | | if (fragments.size() != 0) { |
| | | fragments.clear(); |
| | | } |
| | | },2000L); |
| | | fragments.add(mFragmentManager.findFragmentByTag(0 + "")); |
| | | fragments.add(mFragmentManager.findFragmentByTag(1 + "")); |
| | | fragments.add(mFragmentManager.findFragmentByTag(2 + "")); |
| | | restoreFragment();//恢复fragment页面 |
| | | } else { |
| | | //正常启动时调用 |
| | | fragments.add(mAllMessageFragment); |
| | | fragments.add(mNascentMessageFragment); |
| | | fragments.add(mRecoverMessageFragment); |
| | | showFragment(); |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 切换Fragment |
| | | * |
| | | * @param index 索引 |
| | | */ |
| | | private void showFragment(int index) { |
| | | currentIndex = index; |
| | | showFragment(); |
| | | } |
| | | |
| | | /** |
| | | * 使用show() hide()切换页面 |
| | | * 显示fragment |
| | | */ |
| | | private void showFragment() { |
| | | FragmentTransaction ft = mFragmentManager.beginTransaction(); |
| | | //如果之前没有添加过 |
| | | if (!fragments.get(currentIndex).isAdded()) { |
| | | //第三个参数为添加当前的fragment时绑定一个tag |
| | | ft.hide(currentFragment) |
| | | .add(R.id.message_fcv, fragments.get(currentIndex), "" + currentIndex); |
| | | } else { |
| | | ft.hide(currentFragment) |
| | | .show(fragments.get(currentIndex)); |
| | | } |
| | | currentFragment = fragments.get(currentIndex); |
| | | ft.commit(); |
| | | } |
| | | |
| | | /** |
| | | * 恢复fragment |
| | | */ |
| | | |
| | | private void restoreFragment() { |
| | | FragmentTransaction ft = mFragmentManager.beginTransaction(); |
| | | for (int i = 0; i < fragments.size(); i++) { |
| | | if (i == currentIndex) { |
| | | ft.show(fragments.get(i)); |
| | | } else { |
| | | ft.hide(fragments.get(i)); |
| | | } |
| | | } |
| | | ft.commit(); |
| | | //把当前显示的fragment记录下来 |
| | | currentFragment = fragments.get(currentIndex); |
| | | } |
| | | |
| | | |
| | | } |