2018-05-16 18:06:09.452 30265-30265/com.sky_wf.chinachat D/ChinaChat: , <> ||| com.sky_wf.chinachat.activity.fragment.Fragment_Msg$onStop()@52
2018-05-16 18:06:09.459 30265-30265/com.sky_wf.chinachat D/ChinaChat: , <> ||| com.sky_wf.chinachat.activity.fragment.Fragment_Msg$onDestroyView()@58
2018-05-16 18:06:12.936 30265-30265/com.sky_wf.chinachat D/ChinaChat: , <> ||| com.sky_wf.chinachat.activity.fragment.Fragment_Msg$onCreateView()@38
2018-05-16 18:06:12.947 30265-30265/com.sky_wf.chinachat D/ChinaChat: , <> ||| com.sky_wf.chinachat.activity.fragment.Fragment_Msg$onActivityCreated()@46
方法名
|
方法说明
|
public
FragmentTransaction beginTransaction()
|
获取FragmentTransaction对象
|
public
Fragment findFragmentById(
int
id)
|
根据id获取对应的Fragment
|
public
Fragment findFragmentByTag(String tag)
|
根据tag获取对应的Fragment
|
boolean
popBackStackState
|
执行回退栈的出栈
|
public int
getBackStackEntryCount()
|
获取回退栈的大小
|
方法名
|
方法说明
|
FragmentTransaction add(Fragment var1
,
String var2)
;
FragmentTransaction add(@IdRes
int
var1
,
Fragment var2)
;
FragmentTransaction add(@IdRes
int
var1
,
Fragment var2
,
@Nullable String var3)
;
|
将Fragment添加到Activity的Container中
|
public
FragmentTransaction replace(
int
containerViewId
,
Fragment fragment)
|
移除Container中的Fragment,替换为该Fragment
|
public
FragmentTransaction remove(Fragment fragment)
|
从Container中移除该Fragment
|
public
FragmentTransaction hide(Fragment fragment)
|
隐藏参数中给出的Fragment
|
public
FragmentTransaction show(Fragment fragment)
|
显示参数中给出Fragment
|
public int
commit()
|
执行完上述操作后,必须调用commit进行提交
|
public
FragmentTransaction addToBackStack(String name)
|
将当前事务(在commit之前)中所有的Fragment加入到回退栈中
|
/**
* @Date : 2018/5/14
* @Author : WF
* @Description :Msg界面
*/
public class Fragment_Msg extends BaseFragment {
private final String TAG = "Fragment_Msg";
@Override
public void onAttach(Context context) {
super.onAttach(context);
Debugger.d(TAG,"<>");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Debugger.d(TAG,"<>");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Debugger.d(TAG,"<>");
View view = inflater.inflate(R.layout.fragment_msg,container,false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Debugger.d(TAG,"<>");
}
@Override
public void onStop() {
super.onStop();
Debugger.d(TAG,"<>");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Debugger.d(TAG,"<>");
}
@Override
public void onDestroy() {
super.onDestroy();
Debugger.d(TAG,"<>");
}
}
public class MainActivity extends BaseFragmentActivity
{
private Fragment[] fragments;
private Fragment_Msg msg_fragment;
private Fragment_Friends friend_fragment;
private Fragment_Discover discover_fragment;
private Fragment_Porfile profile_fragment;
private TextView unreadMsgLable;
private TextView unreadAdressLable;
private TextView unreadFindLable;
private TextView unreadProfileLable;
private ImageView[] imageBottom;// 底部img
private TextView[] txtBottom;//底部txt
private TextView txt_title;
private ImageView img_right;
private int currentTabIndex = 0;// 当前Fragment的index
private int index = 0;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Debugger.d(TAG, ">>onCreate<<");
findViewById();
initViews();
initTabView();
}
private void initViews() {
img_right.setVisibility(View.VISIBLE);
img_right.setImageResource(R.drawable.icon_add);
txt_title.setVisibility(View.VISIBLE);
}
private void findViewById()
{
img_right = (ImageView)findViewById(R.id.img_right);
txt_title = (TextView)findViewById(R.id.txt_left);
}
private void initTabView() {
unreadMsgLable = (TextView)findViewById(R.id.unread_msg_number);
unreadAdressLable = (TextView)findViewById(R.id.unread_friend_number);
unreadFindLable = (TextView)findViewById(R.id.unread_discover_number);
unreadProfileLable = (TextView)findViewById(R.id.unread_profile_number);
msg_fragment = new Fragment_Msg();
friend_fragment = new Fragment_Friends();
discover_fragment = new Fragment_Discover();
profile_fragment = new Fragment_Porfile();
fragments = new Fragment[]{msg_fragment,friend_fragment,discover_fragment,profile_fragment};
imageBottom = new ImageView[4];
imageBottom[0] = (ImageView)findViewById(R.id.img_chinachat);
imageBottom[1] = (ImageView)findViewById(R.id.img_friend);
imageBottom[2] = (ImageView)findViewById(R.id.img_discover);
imageBottom[3] = (ImageView)findViewById(R.id.img_profile);
imageBottom[0].setSelected(true);
txtBottom = new TextView[4];
txtBottom[0] = (TextView)findViewById(R.id.txt_chinachat);
txtBottom[1] = (TextView)findViewById(R.id.txt_friend);
txtBottom[2] = (TextView)findViewById(R.id.txt_discover);
txtBottom[3] = (TextView)findViewById(R.id.txt_profile);
txtBottom[0].setTextColor(0xFF45C01A);
//添加四个Fragment,并默认显示msg_fragment
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_container,msg_fragment)
.add(R.id.frame_container,friend_fragment)
.add(R.id.frame_container,discover_fragment)
.add(R.id.frame_container,profile_fragment)
.hide(friend_fragment)
.hide(profile_fragment)
.hide(discover_fragment)
.commit();
}
/**根据点击,呈现对应的Fragment
* @param view
*/
public void onTabClicked(View view)
{
switch (view.getId())
{
case R.id.bottom_chinachat:
index = 0;
txt_title.setText("喵信");
break;
case R.id.bottom_friend:
index = 1;
break;
case R.id.bottom_discover:
index = 2;
break;
case R.id.bottom_profile:
index = 3;
break;
}
if(currentTabIndex!=index)
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[currentTabIndex]);
if(!fragments[index].isAdded())
{
transaction.add(R.id.frame_container,fragments[index]);
}
transaction.show(fragments[index]);
transaction.commit();
}
imageBottom[currentTabIndex].setSelected(false);
imageBottom[index].setSelected(true);
txtBottom[currentTabIndex].setTextColor(0xFF999999);
txtBottom[index].setTextColor(0xFF45C01A);
currentTabIndex = index;
}
@Override
protected void onStop() {
super.onStop();
Debugger.d(TAG, ">>onStop<<");
}
@Override
protected void onDestroy() {
super.onDestroy();
Debugger.d(TAG, ">>onDestroy<<");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_BACK:
// FragmentManager manager = getSupportFragmentManager();
// manager.popBackStack();
// return true;
}
return super.onKeyDown(keyCode, event);
}
}
(1)add操作:
(2)hide操作:
(3)show操作