From d1ebb94e1a17b7c25d4fcf73d85345d92cc86b5c Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期二, 02 一月 2024 18:41:34 +0800 Subject: [PATCH] 2024年01月02日18:41:32 --- app/src/main/java/com/hdl/photovoltaic/ui/message/MessageFragment.java | 169 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 88 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/message/MessageFragment.java b/app/src/main/java/com/hdl/photovoltaic/ui/message/MessageFragment.java index 40e921a..026dfa0 100644 --- a/app/src/main/java/com/hdl/photovoltaic/ui/message/MessageFragment.java +++ b/app/src/main/java/com/hdl/photovoltaic/ui/message/MessageFragment.java @@ -1,12 +1,10 @@ package com.hdl.photovoltaic.ui.message; -import android.os.Build; import android.os.Bundle; -import android.os.Handler; +import android.os.SystemClock; import android.view.View; -import androidx.annotation.RequiresApi; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; @@ -15,10 +13,16 @@ 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 org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; import java.util.ArrayList; import java.util.List; @@ -34,11 +38,8 @@ private static final String CURRENT_FRAGMENT = "state_fragment_show"; private FragmentManager mFragmentManager; private Fragment currentFragment = new Fragment(); - private List<Fragment> fragments = new ArrayList<>(); + private final List<Fragment> fragmentList = new ArrayList<>(); private int currentIndex = 0; - private AllMessageFragment mAllMessageFragment; - private NascentMessageFragment mNascentMessageFragment; - private RecoverMessageFragment mRecoverMessageFragment; @Override public Object getContentView() { @@ -49,6 +50,7 @@ @Override public void onBindView(Bundle savedInstanceState) { + //鍒濆鍖栫鐗� initFragment(savedInstanceState); //鍒濆鍖栫晫闈� initView(); @@ -64,28 +66,22 @@ 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); + currentIndex = 0; + initView(); + showFragment(); + postEventBus(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); + currentIndex = 1; + initView(); + showFragment(); + postEventBus(MessageFunctionTabSwitch.recover); + } }); @@ -93,34 +89,39 @@ 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); - showFragment(2); - postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.all); - + currentIndex = 2; + initView(); + showFragment(); + postEventBus(MessageFunctionTabSwitch.all); } }); } private void initView() { - 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); if (this.currentIndex == 0) { - postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.nascent); + 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) { - postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.recover); + 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) { - postEventBus(HdlCommonLogic.MessageFunctionTabSwitch.all); + 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() { @@ -131,11 +132,12 @@ * * @param tabSwitch - */ - private void postEventBus(HdlCommonLogic.MessageFunctionTabSwitch tabSwitch) { + private void postEventBus(MessageFunctionTabSwitch tabSwitch) { BaseEventBus baseEventBus = new BaseEventBus(); baseEventBus.setTopic(ConstantManage.message_function_tab_switch); - baseEventBus.setType(tabSwitch + ""); - EventBus.getDefault().post(baseEventBus); + baseEventBus.setType(tabSwitch.toString()); + //鍙戦�佺矘鎬т簨浠�(濡傛灉涓嶅彂绮樻�т簨浠朵細閬囧埌涓�涓病鏈夊姞杞藉嚭鏉ョ殑纰庣墖鎺ユ敹涓嶅埌淇℃伅) + EventBus.getDefault().postSticky(baseEventBus); } @@ -146,41 +148,28 @@ */ private void initFragment(Bundle savedInstanceState) { mFragmentManager = _mActivity.getSupportFragmentManager(); - //纰庣墖鍐呴渶瑕佷娇鐢ㄦ潈闄愬拰forresult - - mNascentMessageFragment = new NascentMessageFragment(); - mRecoverMessageFragment = new RecoverMessageFragment(); - mAllMessageFragment = new AllMessageFragment(); + //纰庣墖鍒濆鍖� + NascentMessageFragment mNascentMessageFragment = new NascentMessageFragment(); + RecoverMessageFragment mRecoverMessageFragment = new RecoverMessageFragment(); + AllMessageFragment mAllMessageFragment = new AllMessageFragment(); if (savedInstanceState != null) { //鈥滃唴瀛橀噸鍚�濇椂璋冪敤 鑾峰彇鈥滃唴瀛橀噸鍚�濇椂淇濆瓨鐨勭储寮曚笅鏍� currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT, 0); - if (fragments.size() != 0) { - fragments.clear(); + if (fragmentList.size() != 0) { + fragmentList.clear(); } - fragments.add(mFragmentManager.findFragmentByTag(0 + "")); - fragments.add(mFragmentManager.findFragmentByTag(1 + "")); - fragments.add(mFragmentManager.findFragmentByTag(2 + "")); + fragmentList.add(mFragmentManager.findFragmentByTag(0 + "")); + fragmentList.add(mFragmentManager.findFragmentByTag(1 + "")); + fragmentList.add(mFragmentManager.findFragmentByTag(2 + "")); restoreFragment();//鎭㈠fragment椤甸潰 } else { //姝e父鍚姩鏃惰皟鐢� - fragments.add(mAllMessageFragment); - fragments.add(mNascentMessageFragment); - fragments.add(mRecoverMessageFragment); + fragmentList.add(mNascentMessageFragment); + fragmentList.add(mRecoverMessageFragment); + fragmentList.add(mAllMessageFragment); showFragment(); - - } - } - - /** - * 鍒囨崲Fragment - * - * @param index 绱㈠紩 - */ - private void showFragment(int index) { - currentIndex = index; - showFragment(); } /** @@ -190,34 +179,52 @@ private void showFragment() { FragmentTransaction ft = mFragmentManager.beginTransaction(); //濡傛灉涔嬪墠娌℃湁娣诲姞杩� - if (!fragments.get(currentIndex).isAdded()) { + if (!fragmentList.get(currentIndex).isAdded()) { //绗笁涓弬鏁颁负娣诲姞褰撳墠鐨刦ragment鏃剁粦瀹氫竴涓猼ag - ft.hide(currentFragment) - .add(R.id.message_fcv, fragments.get(currentIndex), "" + currentIndex); + ft.hide(currentFragment).add(R.id.message_fcv, fragmentList.get(currentIndex), currentIndex + ""); } else { - ft.hide(currentFragment) - .show(fragments.get(currentIndex)); + ft.hide(currentFragment).show(fragmentList.get(currentIndex)); } - currentFragment = fragments.get(currentIndex); + currentFragment = fragmentList.get(currentIndex); ft.commit(); + //鎶婂綋鍓嶆樉绀虹殑fragment璁板綍涓嬫潵 + currentFragment = fragmentList.get(currentIndex); + + } /** * 鎭㈠fragment */ - private void restoreFragment() { FragmentTransaction ft = mFragmentManager.beginTransaction(); - for (int i = 0; i < fragments.size(); i++) { + for (int i = 0; i < fragmentList.size(); i++) { if (i == currentIndex) { - ft.show(fragments.get(i)); + ft.show(fragmentList.get(i)); } else { - ft.hide(fragments.get(i)); + ft.hide(fragmentList.get(i)); } } ft.commit(); - //鎶婂綋鍓嶆樉绀虹殑fragment璁板綍涓嬫潵 - currentFragment = fragments.get(currentIndex); + + } + + @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) + public void onEventMessage(BaseEventBus eventBus) { + super.onEventMessage(eventBus); + if (eventBus == null) { + return; + } + //鎺ユ敹澶栭儴鐐瑰嚮浜嬩欢 + if (eventBus.getTopic().equals(ConstantManage.homepage_title_tab_switch)) { + if (eventBus.getType().equals(HomepageTitleTabSwitch.message.toString())) { + postEventBus(MessageFunctionTabSwitch.nascent); + HdlLogLogic.print("姝e湪鐐瑰嚮銆愭秷鎭��"); + // 鍙栨秷绮樻�т簨浠� + EventBus.getDefault().removeStickyEvent(eventBus); + + } + } } -- Gitblit v1.8.0