android 多个fragment切换,android fragment replace 多个切换fragment时的遇到的焦点变化...

第一个fragment代码:

package com.example.liuyj.mstarsysseting.fragment;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import com.example.liuyj.mstarsysseting.R;

public class FragmentSysTools extends Fragment {

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.layout_systools, container, false);

view.findViewById(R.id.toolsReset).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

getFragmentManager()

.beginTransaction()

.addToBackStack(null)

.replace(R.id.fragment, new FragmentSysToolsReset())

.commit();

}

});

view.findViewById(R.id.toolsReset).requestFocus();

return view;

}

@Override

public void onStart() {

Log.e("FragmentSysTools", "onStart");

super.onStart();

}

@Override

public void onResume() {

Log.e("FragmentSysTools", "onResume");

super.onResume();

}

@Override

public void onPause() {

Log.e("FragmentSysTools", "onPause");

super.onPause();

}

@Override

public void onStop() {

Log.e("FragmentSysTools", "onStop");

super.onStop();

}

@Override

public void onDestroy() {

Log.e("FragmentSysTools", "onDestroy");

super.onDestroy();

}

}

第二个fragment代码:

package com.example.liuyj.mstarsysseting.fragment;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.Toast;

import com.example.liuyj.mstarsysseting.R;

import java.security.Key;

public class FragmentSysToolsReset extends Fragment {

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.layout_systools_reset, container, false);

Button button = view.findViewById(R.id.toolsRest_test);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

getFragmentManager().popBackStack();

}

});

button.requestFocus();

return view;

}

@Override

public void onStart() {

Log.e("FragmentSysToolsReset", "onStart");

super.onStart();

}

@Override

public void onResume() {

Log.e("FragmentSysToolsReset", "onResume");

super.onResume();

}

@Override

public void onPause() {

Log.e("FragmentSysToolsReset", "onPause");

super.onPause();

}

@Override

public void onStop() {

Log.e("FragmentSysToolsReset", "onStop");

super.onStop();

}

@Override

public void onDestroy() {

Log.e("FragmentSysTools", "onDestroy");

super.onDestroy();

}

}

切换代码:

getFragmentManager()

.beginTransaction()

.addToBackStack(null) //入栈,默认按返回时将会返回到这个fragment。

.replace(R.id.fragment, new FragmentSysToolsReset()) //使用的时replace函数切换fragment

.commit();

遇到的问题:

因为进入下一个fragment,和返回上一个fragment时。是先完全关闭了当前显示的fragment后再打开另一个fragment,

-->所以页面上的焦点将会跳转到android页面的其他焦点过渡。

-->若,焦点上存在焦点监听的事件也将会进行处理。

生命周期中查看,fragment的运行状态:

b6b3917c8290526831e27a45cbda63e8.png

解决办法:使用 add() hide() show() 函数来处理: 切换时 hide() 函数放在 show() 函数后面

public void showFragment(Fragment fragmentTo) {

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction transaction = fragmentManager.beginTransaction();

if(fragmentCurr == fragmentTo) {

return;

}

if (fragmentTo.isAdded()) {

transaction.show(fragmentTo);

} else {

transaction.add(R.id.fragment, fragmentTo).show(fragmentTo);

}

if (null != fragmentCurr) {

transaction.hide(fragmentCurr); //hide()函数放在show()函数后面。保证焦点存在

}

fragmentCurr = fragmentTo;

transaction.commit();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值