305 lines
11 KiB
Java
305 lines
11 KiB
Java
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.MotionEvent;
|
||
import android.view.View;
|
||
import android.widget.RadioButton;
|
||
import android.widget.RadioGroup;
|
||
|
||
import com.gyf.immersionbar.ImmersionBar;
|
||
import com.sl.house_property.cart.CartFragment;
|
||
import com.sl.house_property.databinding.ActivityMainTabBinding;
|
||
import com.sl.house_property.discovery.DiscoveryFragment;
|
||
import com.sl.house_property.discovery.LikeFragment;
|
||
|
||
import java.util.ArrayList;
|
||
|
||
import tools.Config;
|
||
|
||
public class MainTabActivity extends BaseActivity<ActivityMainTabBinding>
|
||
implements View.OnClickListener,
|
||
Main1Fragment.OnFragmentInteractionListener,
|
||
Main2Fragment.OnFragmentInteractionListener,
|
||
Main3Fragment.OnFragmentInteractionListener,
|
||
Main4Fragment.OnFragmentInteractionListener {
|
||
|
||
/*定义的全局返回码,例如在主界面的某个Fragment点击某一项后进入下一个界面后
|
||
如果在下一个界面有数据更改,需要Fragment刷新时,通过setResult方法通知主界面,主界面的
|
||
onActivityResult方法会接收到通知,再刷新相应的Fragment
|
||
*/
|
||
|
||
public static final int RESUlT_CODE_0 = 201;//首页
|
||
public static final int RESUlT_CODE_1 = 202;//发现
|
||
public static final int RESUlT_CODE_2 = 203;//购物车
|
||
// public static final int RESUlT_CODE_3 = 204;//
|
||
public static final int RESUlT_CODE_4 = 205;//我的
|
||
private RadioGroup radioGroup;
|
||
private MainFragment fragment0;
|
||
private DiscoveryFragment fragment1;
|
||
private Main3Fragment fragment2;
|
||
private CartFragment fragment3;
|
||
private Main4Fragment fragment4;
|
||
// private LikeFragment likeFragment;
|
||
private Fragment currentFragment;
|
||
|
||
private int myeditcode = 1000;
|
||
|
||
|
||
private int nowradioId;
|
||
|
||
// @Override
|
||
// public boolean dispatchTouchEvent(MotionEvent ev) {
|
||
// switch (ev.getAction()) {
|
||
// case MotionEvent.ACTION_DOWN:
|
||
//
|
||
// UtilHelpers.hideKeyboard(ev, getCurrentFocus(), this);
|
||
// break;
|
||
// }
|
||
// return super.dispatchTouchEvent(ev);
|
||
//
|
||
// }
|
||
public void visibilyBottom(int isVisibily) {
|
||
findViewById(R.id.rl_bottom).setVisibility(isVisibily);
|
||
}
|
||
|
||
//2. 保存MyOnTouchListener接口的列表
|
||
private ArrayList<MyOnTouchListener> onTouchListeners = new ArrayList<MyOnTouchListener>();
|
||
|
||
//1.触摸事件接口
|
||
public interface MyOnTouchListener {
|
||
public boolean onTouch(MotionEvent ev);
|
||
}
|
||
|
||
@Override
|
||
public boolean dispatchTouchEvent(MotionEvent ev) {
|
||
for (MyOnTouchListener listener : onTouchListeners) {
|
||
// listener.onTouch(ev);
|
||
}
|
||
return super.dispatchTouchEvent(ev);
|
||
}
|
||
|
||
//4.提供给Fragment通过getActivity()方法来注册自己的触摸事件的方法
|
||
public void registerMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
|
||
onTouchListeners.add(myOnTouchListener);
|
||
}
|
||
|
||
//5.提供给Fragment通过getActivity()方法来注销自己的触摸事件的方法
|
||
public void unregisterMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
|
||
onTouchListeners.remove(myOnTouchListener);
|
||
}
|
||
|
||
@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();
|
||
ImmersionBar.with(this)
|
||
.keyboardEnable(true) //解决软键盘与底部输入框冲突问题
|
||
// .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
|
||
// | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //软键盘自动弹出
|
||
.init();
|
||
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) {
|
||
|
||
fragment1 = DiscoveryFragment.newInstance("", "");
|
||
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 = CartFragment.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;
|
||
// case R.id.like:
|
||
// if (likeFragment == null) {
|
||
// likeFragment = LikeFragment.newInstance();
|
||
// fragmentTransaction.add(R.id.mycontent, likeFragment, "likeFragment");
|
||
// } else {
|
||
// fragmentTransaction.show(likeFragment);
|
||
// }
|
||
// currentFragment = likeFragment;
|
||
// 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);
|
||
}
|
||
}
|
||
|