Files
HouseProperty/app/src/main/java/com/sl/house_property/MainTabActivity.java

244 lines
8.6 KiB
Java
Raw Normal View History

2020-08-03 09:11:54 +08:00
package com.sl.house_property;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.sl.house_property.databinding.ActivityMainTabBinding;
2020-08-11 14:48:10 +08:00
import com.sl.house_property.discovery.DiscoveryFragment;
2020-08-03 09:11:54 +08:00
import tools.Config;
public class MainTabActivity extends BaseActivity<ActivityMainTabBinding>
implements View.OnClickListener,MainFragment.OnFragmentInteractionListener,
Main1Fragment.OnFragmentInteractionListener,
Main2Fragment.OnFragmentInteractionListener,
Main3Fragment.OnFragmentInteractionListener,
Main4Fragment.OnFragmentInteractionListener{
/*定义的全局返回码例如在主界面的某个Fragment点击某一项后进入下一个界面后
如果在下一个界面有数据更改需要Fragment刷新时通过setResult方法通知主界面主界面的
onActivityResult方法会接收到通知再刷新相应的Fragment
*/
public static final int RESUlT_CODE_0 = 201;//首页
2020-08-11 14:48:10 +08:00
public static final int RESUlT_CODE_1 = 202;//发现
public static final int RESUlT_CODE_2 = 203;//购物车
// public static final int RESUlT_CODE_3 = 204;//
2020-08-03 09:11:54 +08:00
public static final int RESUlT_CODE_4 = 205;//我的
private RadioGroup radioGroup;
private MainFragment fragment0;
2020-08-11 14:48:10 +08:00
private DiscoveryFragment fragment1;
2020-08-03 09:11:54 +08:00
private Main3Fragment fragment2;
private Main2Fragment fragment3;
private Main4Fragment fragment4;
private Fragment currentFragment;
private int myeditcode=1000;
private int nowradioId;
@Override
protected int getLayoutResId() {
return R.layout.activity_main_tab;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Config.getInstance(MainTabActivity.this).put("MainTabActivity",true);
//setPhotoSysyrmbar();
//setSytemBar(R.color.colorPrimary);//设置主题样式
//setFramentMarginTop();
initView();
}
@SuppressLint("MissingSuperCall")
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void initView() {
radioGroup= mDataBinding.radioGroup;
for (int i = 0; i <radioGroup.getChildCount() ; i++) {
RadioButton myRadioButton= (RadioButton) radioGroup.getChildAt(i);
myRadioButton.setOnClickListener(this);
}
mDataBinding.button.setChecked(true);
nowradioId= mDataBinding.button.getId();
mDataBinding.getRoot().setBackground(getResources().getDrawable(R.drawable.background_slowly));
changeFragment( mDataBinding.button.getId());
}
private void changeFragment(int i) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (currentFragment != null)
fragmentTransaction.hide(currentFragment);
switch (i) {
case R.id.button:
// mDataBinding.getRoot().setBackground(getResources().getDrawable(R.drawable.background_slowly));
if (fragment0 == null) {
fragment0 = MainFragment.newInstance("", "");
fragmentTransaction.add(R.id.mycontent, fragment0, "fragment0");
} else {
fragmentTransaction.show(fragment0);
}
currentFragment = fragment0;
//setSytemBar(R.color.transparent);
break;
case R.id.button1:
// mDataBinding.getRoot().setBackground(getResources().getDrawable(R.drawable.background_slowly));
if (fragment1 == null) {
2020-08-11 14:48:10 +08:00
fragment1 = DiscoveryFragment.newInstance("", "");
2020-08-03 09:11:54 +08:00
fragmentTransaction.add(R.id.mycontent, fragment1, "fragment1");
} else {
fragmentTransaction.show(fragment1);
}
currentFragment = fragment1;
break;
case R.id.button2:
if (fragment2 == null) {
fragment2 = Main3Fragment.newInstance("", "");
fragmentTransaction.add(R.id.mycontent, fragment2, "fragment2");
} else {
fragmentTransaction.show(fragment2);
}
currentFragment = fragment2;
break;
case R.id.button3:
// mDataBinding.getRoot().setBackground(getResources().getDrawable(R.drawable.background_slowly));
if (fragment3 == null) {
fragment3 = Main2Fragment.newInstance("", "");
fragmentTransaction.add(R.id.mycontent, fragment3, "fragment3");
} else {
fragmentTransaction.show(fragment3);
}
currentFragment = fragment3;
break;
case R.id.button4:
// mDataBinding.getRoot().setBackground(getResources().getDrawable(R.mipmap.beijing));
if (fragment4 == null) {
fragment4 = Main4Fragment.newInstance("", "");
fragmentTransaction.add(R.id.mycontent, fragment4, "fragment4");
} else {
fragmentTransaction.show(fragment4);
}
currentFragment = fragment4;
break;
}
try {
fragmentTransaction.commit();
} catch (Exception e) {
e.fillInStackTrace();
fragmentTransaction.commitAllowingStateLoss();
}
}
@Override
public void onClick(View view) {
if(view.getId()==nowradioId)return;
RadioButton myRadioButton=(RadioButton)findViewById(nowradioId);
if(view.getId()==R.id.button4){
if( (Config.getInstance(MainTabActivity.this).getUser()==null)){
Intent intent =new Intent(MainTabActivity.this,LoginActivity.class);
startActivity(intent);
myRadioButton=(RadioButton)view;
myRadioButton.setChecked(false);
myRadioButton=(RadioButton)findViewById(nowradioId);
myRadioButton.setChecked(true);
return;
}
}
myRadioButton.setChecked(false);
myRadioButton=(RadioButton)view;
myRadioButton.setChecked(true);
nowradioId=view.getId();
changeFragment(view.getId());
}
public void onFragmentInteraction(Uri uri) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(resultCode==RESUlT_CODE_4){
if(currentFragment==fragment4){
// if(fragment0!=null){
// fragmentTransaction.remove(fragment0);
// fragment0 = null;
// }
// if(fragment1!=null){
// fragmentTransaction.remove(fragment1);
// fragment1 = null;
// }
// if(fragment2!=null){
// fragmentTransaction.remove(fragment2);
// fragment2 = null;
// }
// if(fragment3!=null){
// fragmentTransaction.remove(fragment3);
// fragment3 = null;
// }
//
// fragmentTransaction.remove(fragment4);
// mDataBinding.getRoot().setBackground(getResources().getDrawable(R.mipmap.beijing));
// if (fragment4 == null) {
// fragment4 = Main4Fragment.newInstance("", "");
// fragmentTransaction.add(R.id.mycontent, fragment4, "fragment4");
// } else {
//
// }
// fragmentTransaction.show(fragment4);
// currentFragment = fragment4;
fragment4.refreshData();
}
try {
// fragmentTransaction.commit();
} catch (Exception e) {
e.fillInStackTrace();
fragmentTransaction.commitAllowingStateLoss();
}
}else if(resultCode==myeditcode){
if(currentFragment==fragment1){
fragment1.loadData();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Config.getInstance(MainTabActivity.this).put("MainTabActivity",false);
}
}