<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_fl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background">
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"<span style="color:#ff6666;">//重点</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"<span style="color:#ff6666;">//重点</span>
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:background="#ffc7c7c7" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/background" />
<TabWidget
android:id="@android:id/tabs"<span style="color:#ff0000;">//重点,(滑动卡片)</span>
android:layout_width="fill_parent"
android:layout_height="52.0dip"
android:layout_gravity="bottom"
android:background="@color/white"
android:divider="@color/white" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</FrameLayout>
package com.neusoft.lanxi.ui.activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import com.alibaba.fastjson.TypeReference;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;
import com.neusoft.lanxi.R;
import com.neusoft.lanxi.common.AppContant;
import com.neusoft.lanxi.common.AppContext;
import com.neusoft.lanxi.common.AppManager;
import com.neusoft.lanxi.common.net.RequestManager;
import com.neusoft.lanxi.common.utils.CommUtils;
import com.neusoft.lanxi.common.utils.JsonUtils;
import com.neusoft.lanxi.common.utils.StringUtils;
import com.neusoft.lanxi.model.BasicData;
import com.neusoft.lanxi.model.BodyData;
import com.neusoft.lanxi.model.DoctorData;
import com.neusoft.lanxi.model.DoctorFriendData;
import com.neusoft.lanxi.model.DoctorUserData;
import com.neusoft.lanxi.model.ResultData;
import com.neusoft.lanxi.ui.BaseActivity;
import com.neusoft.lanxi.ui.BaseFragment;
import com.neusoft.lanxi.ui.activity.service.ChatActivity;
import com.neusoft.lanxi.ui.fragment.CommunityFragment;
import com.neusoft.lanxi.ui.fragment.FamilyFragment;
import com.neusoft.lanxi.ui.fragment.ServiceFragment;
import org.litepal.crud.DataSupport;
import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
public class HomeActivity extends BaseActivity implements BaseFragment.BackHandleInterface {
@Bind(android.R.id.tabhost)
FragmentTabHost mTabHost;
private int versioncode;
@Bind(android.R.id.tabs)
TabWidget mTabWidget;
private FragmentManager mFragmentManager;
private BaseFragment mBaseFragment;
@Override
protected int initLayout() {
return R.layout.activity_home;
}
@Override
protected void initView(Bundle savedInstanceState) {
//fragment切换到含有surfaceView的fragment,黑屏的问题的解决方案.
getWindow().setFormat(PixelFormat.TRANSLUCENT);
versioncode=CommUtils.getAppVersionCode(getApplicationContext());
mFragmentManager = getSupportFragmentManager();<span style="color:#ff0000;">//获得一个mFragmentManager;</span>
mTabHost.setup(this, mFragmentManager, android.R.id.tabcontent);//<span style="color:#ff0000;">应该是设置容器</span>
mTabHost.addTab(buildTabSpec("1", R.string.tab_one, R.drawable.tab_person_center_icon),
FamilyFragment.class, null);<span style="color:#ff0000;">//添加tab</span>
mTabHost.addTab(buildTabSpec("2", R.string.tab_two, R.drawable.tab_service_icon),
ServiceFragment.class, null);
mTabHost.addTab(buildTabSpec("3", R.string.tab_three, R.drawable.tab_community_icon),
CommunityFragment.class, null);
// <span style="color:#ff0000;">默认显示TAB</span>
mTabHost.setCurrentTabByTag("1");
mTabHost.getTabWidget().getChildAt(1).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
// 根据好友ID获取好友列表
AppContext.getInstance().postRunnable(new Runnable() {
@Override
public void run() {
try {
List<String> usernames = EMClient.getInstance().contactManager().getAllContactsFromServer();
HashMap<String, String> params1 = new HashMap<String, String>();
params1.put("schema", AppContext.userInfo.getSchema());
params1.put("userIdArray", StringUtils.formatListString(usernames));
RequestManager.getInstance().postObject(params1, HomeActivity.this, AppContant.FRIENDS_LIST);
//请求接口头像,称呼,姓名
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("userId", StringUtils.formatObject(AppContext.userInfo.getUserId()));
params2.put("memberId", StringUtils.formatObject(AppContext.userInfo.getUserId()));
params2.put("schema", StringUtils.formatObject(AppContext.userInfo.getSchema()));
RequestManager.getInstance().postObject(params2, HomeActivity.this, AppContant.PERSON_INFORMATION);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0);
if(AppContext.versionNum>versioncode){
if(AppContext.avaiLable==1){
updateVersionOne(AppContext.update_url, CommUtils.getAppVersionName(HomeActivity.this),true);
}else{
updateVersion(AppContext.update_url,CommUtils.getAppVersionName(HomeActivity.this),false);
}
}
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mTabHost.setCurrentTabByTag("1");
}
public Fragment getCurrentFragment() {
return getSupportFragmentManager().findFragmentByTag(
mTabHost.getCurrentTabTag());
}
private TabHost.TabSpec buildTabSpec(String tag, int textId, int imageId) {
View view = mInflater.inflate(R.layout.layout_tab_indicator_bottom, null);<span style="color:#ff0000;">//底部标签的布局</span>
TextView textView = (TextView) view.findViewById(R.id.tab_title);
ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
textView.setText(getString(textId));
imageView.setImageDrawable(getResources().getDrawable(imageId));
return mTabHost.newTabSpec(tag).setIndicator(view);
}
@Override
public void onBackPressed() {
if (mBaseFragment == null || !mBaseFragment.onBackPressed()) {
if (mFragmentManager.getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
mFragmentManager.popBackStack();
}
}
}
@Override
public void setSelectedFragment(BaseFragment selectedFragment) {
this.mBaseFragment = selectedFragment;
}
}
<div class="page" title="Page 163" style="font-size: 11.3pt;"><div class="section"><div class="layoutArea"><div class="column"><ol><li style="font-size: 10.000000pt; font-family: 'TimesNewRomanPSMT'"><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">创建待添加的碎片实例。</span></p></li><li style="font-size: 10.000000pt; font-family: 'TimesNewRomanPSMT'"><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">获取到 </span><span style="font-size: 10pt;">FragmentManager</span><span style="font-size: 10.000000pt; font-family: 'SimSun'">,在活动中可以直接调用 </span><span style="font-size: 10pt;">getFragmentManager()</span><span style="font-size: 10.000000pt; font-family: 'SimSun'">方法得到。</span></p></li><li style="font-size: 10.000000pt; font-family: 'TimesNewRomanPSMT'"><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">开启一个事务,通过调用 </span><span style="font-size: 10pt;">beginTransaction()</span><span style="font-size: 10.000000pt; font-family: 'SimSun'">方法开启。</span></p></li><li style="font-size: 10.000000pt; font-family: 'TimesNewRomanPSMT'"><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">向容器内加入碎片,一般使用 </span><span style="font-size: 10pt;">replace()</span><span style="font-size: 10.000000pt; font-family: 'SimSun'">方法实现,需要传入容器的 </span><span style="font-size: 10pt;">id </span><span style="font-size: 10.000000pt; font-family: 'SimSun'">和待添加的碎</span></p><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">片实例。</span></p></li><li style="font-size: 10.000000pt; font-family: 'TimesNewRomanPSMT'"><p><span style="font-size: 10.000000pt; font-family: 'SimSun'">提交事务,调用 </span><span style="font-size: 10pt;">commit()</span><span style="font-size: 10.000000pt; font-family: 'SimSun'">方法来完成。 </span></p></li></ol></div></div></div></div><div class="page" title="Page 163"><div class="section"><div class="layoutArea"><div class="column"><pre><pre name="code" class="java" style="font-size: 11.3pt;">AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.
beginTransaction();
transaction.replace(R.id.right_layout, fragment);
<div class="page" title="Page 164"><div class="section"><div class="layoutArea"><div class="column"><pre><span style="font-family: CourierNewPS; font-weight: 700;"><span style="font-size:18px;">transaction.addToBackStack(null);//给fragment添加返回栈</span></span><span style="font-size: 9pt; font-family: CourierNewPS; font-weight: 700;">
</span>
transaction.commit();
<span style="font-size: 11.3pt;">
</span>
<span style="font-size: 11.3pt;"></span><pre name="code" class="java">RightFragment rightFragment = (RightFragment) getFragmentManager()
.findFragmentById(R.id.right_fragment);//在activity获得Fragment的实例,从而可以在activity中调用fragment的方法