381 lines
14 KiB
Java
381 lines
14 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.util.Log;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.widget.RadioButton;
|
||
import android.widget.RadioGroup;
|
||
import android.widget.TextView;
|
||
|
||
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.message.MessageFragment;
|
||
|
||
import java.util.ArrayList;
|
||
|
||
import my_loader.Loader;
|
||
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 MessageFragment messageFragment;
|
||
// private LikeFragment likeFragment;
|
||
private Fragment currentFragment;
|
||
|
||
private int myeditcode = 1000;
|
||
|
||
private Loader mGankLoader;
|
||
|
||
// @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);
|
||
}
|
||
public void setMessageCount(int num){
|
||
TextView viewById = findViewById(R.id.redview);
|
||
if (num>0){
|
||
viewById.setText(num);
|
||
viewById .setVisibility(View.VISIBLE);
|
||
}else {
|
||
viewById.setVisibility(View.GONE);
|
||
}
|
||
}
|
||
//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();
|
||
|
||
// RegisterUser user = Config.getInstance(MainTabActivity.this).getUser();
|
||
// Map<String, String> map = new HashMap<>();
|
||
// if (user != null) {
|
||
// map.put("userid", user.getUserid());
|
||
// } else {
|
||
// map.put("userid", 0 + "");
|
||
// }
|
||
// map.put("app", "Cas");
|
||
// map.put("class", "MyHouseList");
|
||
// //map.put("home_id", "GetHomeByPid");
|
||
// map.put("sign", Md5.md5("Cas" + "MyHouseList" + Md5.secret));
|
||
// getGankList(ApiConfig.BASE_URL, map, getResources().getString(R.string.requsting), 0);
|
||
|
||
|
||
}
|
||
|
||
// private void getGankList(String myurl, Map<String, String> map, String msg, final int requstecode) {
|
||
//// progressDialog.setMessage(msg);
|
||
//// progressDialog.setCancelable(false);
|
||
//// progressDialog.show();
|
||
// mGankLoader = new Loader();
|
||
// Subscription subscription = mGankLoader.getMovie(myurl, map).subscribe(new Action1<Resultcode>() {
|
||
// @Override
|
||
// public void call(Resultcode resultcode) {
|
||
// progressDialog.dismiss();
|
||
//
|
||
// if (!(resultcode.status == 0)) {
|
||
// setToast(resultcode.msg);
|
||
// }
|
||
// if (resultcode.status == 0) {
|
||
// if (requstecode == 0) {
|
||
// Gson gs = new Gson();
|
||
// ArrayList<LinkedTreeMap> adta = (ArrayList<LinkedTreeMap>) resultcode.data;
|
||
// String s = gs.toJson(adta);
|
||
// //final ArrayList<MyHomequEntity> list = null;
|
||
// Type type = new TypeToken<ArrayList<MyHouseEntity>>() {
|
||
// }.getType();
|
||
// final ArrayList<MyHouseEntity> list = new Gson().fromJson(s, type);
|
||
// if(list.size()==0){
|
||
// SPUtils.setSP(MainTabActivity.this, DoConfig.FANGCHAN_DATA, "0");
|
||
// }else {
|
||
// SPUtils.setSP(MainTabActivity.this, DoConfig.FANGCHAN_DATA, "1");
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
// }, new Action1<Throwable>() {
|
||
// @Override
|
||
// public void call(Throwable throwable) {
|
||
// progressDialog.dismiss();
|
||
// setToast(getString(R.string.getdatafailure));
|
||
// throwable.printStackTrace();
|
||
// }
|
||
// });
|
||
//
|
||
// addSubscription(subscription);
|
||
// }
|
||
|
||
|
||
@SuppressLint("MissingSuperCall")
|
||
@Override
|
||
public void onSaveInstanceState(Bundle outState) {
|
||
super.onSaveInstanceState(outState);
|
||
}
|
||
|
||
private void initView() {
|
||
findViewById(R.id.rl2).setOnClickListener(this);
|
||
findViewById(R.id.button).setOnClickListener(this);
|
||
findViewById(R.id.button1).setOnClickListener(this);
|
||
findViewById(R.id.button4).setOnClickListener(this);
|
||
mDataBinding.button.setChecked(true);
|
||
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.button.setChecked(true);
|
||
mDataBinding.button1.setChecked(false);
|
||
mDataBinding.button2.setChecked(false);
|
||
mDataBinding.button4.setChecked(false);
|
||
// 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.button.setChecked(false);
|
||
mDataBinding.button1.setChecked(true);
|
||
mDataBinding.button2.setChecked(false);
|
||
mDataBinding.button4.setChecked(false);
|
||
// 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.rl2:
|
||
mDataBinding.button.setChecked(false);
|
||
mDataBinding.button1.setChecked(false);
|
||
mDataBinding.button2.setChecked(true);
|
||
mDataBinding.button4.setChecked(false);
|
||
if (messageFragment == null) {
|
||
messageFragment = MessageFragment.newInstance();
|
||
fragmentTransaction.add(R.id.mycontent, messageFragment, "fragment2");
|
||
} else {
|
||
fragmentTransaction.show(messageFragment);
|
||
}
|
||
|
||
currentFragment = messageFragment;
|
||
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.button.setChecked(false);
|
||
mDataBinding.button1.setChecked(false);
|
||
mDataBinding.button2.setChecked(false);
|
||
mDataBinding.button4.setChecked(true);
|
||
// 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() == R.id.button4) {
|
||
if ((Config.getInstance(MainTabActivity.this).getUser() == null)) {
|
||
Intent intent = new Intent(MainTabActivity.this, LoginActivity.class);
|
||
startActivity(intent);
|
||
mDataBinding.button.setChecked(false);
|
||
mDataBinding.button1.setChecked(false);
|
||
mDataBinding.button2.setChecked(false);
|
||
mDataBinding.button4.setChecked(false);
|
||
return;
|
||
|
||
}
|
||
}
|
||
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();
|
||
Log.e("测试","测试");
|
||
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);
|
||
}
|
||
}
|
||
|