解释, 我使用viewpager类进行2个活动。当您单击ActivityMainTab列表中的某个项目时,它将被带到另一个活动(ActivityAtTab),它还使用与前一个相同的viewpager类来生成其选项卡和片段。
我需要知道Conduit片段是否在屏幕上,所以在我将在下面显示的条件下,我可以转到正确的方法并继续流程。
代码下方:
ActivityAtTab,此方法检查屏幕上的哪个片段,以便匹配变量转到正确的showResults。在mFragmentConduit的情况下,我无法判断它是否在屏幕上,因为它来自viewPager。 ActivityAtTab
@Override
public void onResults(Bundle results) {
//inicializando as variáveis de fragments
av = (FragmentAv) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_av));
rec = (FragmentRec) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_rec));
ex = (FragmentEx) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_ex));
enc = (FragmentEnc) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_enc));
atest = (FragmentAtest) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_atest));
dig = (FragmentDiag) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_diag));
mFragmentConduit = (FragmentConduit) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_conduit));
//pegando a lista de comandos
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
//Verifica qual fragment está chamando a interface
//e passa o valor da string para que seja implementada nos respectivos campos.
try {
if (av != null && av instanceof ResultFragmentsConduit) {
resultFragmentsConduit = av;
resultFragmentsConduit.showCondutaResults(matches);
} else if (rec != null && rec instanceof ResultFragmentsConduit) {
ResultFragmentsConduit = rec;
ResultFragmentsConduit.showCondutaResults(matches);
} else if (ex != null && ex instanceof ResultFragmentsConduit) {
ResultFragmentsConduit = ex;
ResultFragmentsConduit.showCondutaResults(matches);
} else if (enc != null && enc instanceof ResultFragmentsConduit) {
ResultFragmentsConduit = enc;
ResultFragmentsConduit.showCondutaResults(matches);
} else if (at != null && at instanceof ResultFragmentsConduit) {
ResultFragmentsConduit = at;
ResultFragmentsConduit.showCondutaResults(matches);
} else if (dig != null && dig instanceof ResultFragmentsConduit) {
ResultFragmentsConduit = dig;
ResultFragmentsConduit.showCondutaResults(matches);
} else if (mFragmentConduit != null && mFragmentConduit.isVisible() ){ //ALWAYS NULL!!!!!!!!!
showResultsConduit(matches);
}
} catch (Exception e) {
e.getStackTrace();
Log.e(TAG_TABATENDIMENTO, e.getMessage());
}
}
ActivityAtTab的onCreate方法,其中片段在viewPager中提交构造:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
//TODO FRAGMENTOS
mFragmentsArrayList = new ArrayList<>();
mFragmentsArrayList.add(mFragmentConduit = new FragmentConduit());
mFragmentsArrayList.add(new FragmentHistorico());
stringArrayListTitleFragment = new ArrayList<>();
stringArrayListTitleFragment.add(getString(R.string.fragment_conduit));
stringArrayListTitleFragment.add(getString(R.string.fragment_historico));
mCustomTabLayout = new CustomTabLayout(this, mFragmentsArrayList, stringArrayListTitleFragment);
requestPermission();
}
ViewPager:
public class CustomTabLayout {
public AppCompatActivity mAppCompatActivity;
private TabLayout tabLayout;
private ViewPager viewPager;
private PagerAdapter adapter;
private ArrayList<Fragment> mFragmentArrayList;
private ArrayList<String> mFragmentArrayListTitle;
public CustomTabLayout(AppCompatActivity appCompatActivity, ArrayList<Fragment> fragments, ArrayList<String> stringArrayListFragments) {
mAppCompatActivity = appCompatActivity;
mFragmentArrayList = fragments;
mFragmentArrayListTitle = stringArrayListFragments;
viewPager = mAppCompatActivity.findViewById(R.id.viewpager);
tabLayout = mAppCompatActivity.findViewById(R.id.tabs);
createViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(adapter);
createTabIcons();
}
private void createViewPager(ViewPager viewPager) {
adapter = new PagerAdapter(mAppCompatActivity.getSupportFragmentManager());
for (int contador = 0; contador < mFragmentArrayList.size(); contador++) {
adapter.addFrag(mFragmentArrayList.get(contador), mFragmentArrayListTitle.get(contador));
}
viewPager.setAdapter(adapter);
}
class PagerAdapter extends FragmentStatePagerAdapter implements TabLayout.OnTabSelectedListener {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
LinearLayout linearLayoutAnimation = (LinearLayout) tab.getCustomView().findViewById(R.id.custom_tab_linear_layout_animation);
linearLayoutAnimation.setVisibility(View.VISIBLE);
linearLayoutAnimation.setBackground(ContextCompat.getDrawable(mAppCompatActivity, R.drawable.aba_grande));
Animation bottomUp = AnimationUtils.loadAnimation(mAppCompatActivity,
R.anim.bottom_up);
linearLayoutAnimation.startAnimation(bottomUp);
//TODO CHAMA ACTIVITY
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
final LinearLayout linearLayoutAnimation = (LinearLayout) tab.getCustomView().findViewById(R.id.custom_tab_linear_layout_animation);
Animation bottomDown = AnimationUtils.loadAnimation(mAppCompatActivity,
R.anim.bottom_down);
bottomDown.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
linearLayoutAnimation.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
linearLayoutAnimation.startAnimation(bottomDown);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
private void createTabIcons() {
for (int contador = 0; contador < mFragmentArrayList.size(); contador++) {
LinearLayout tabCustom = (LinearLayout) LayoutInflater.from(mAppCompatActivity).inflate(R.layout.custom_tab, null);
TextView textViewTabCustom = tabCustom.findViewById(R.id.custom_tab_title);
textViewTabCustom.setText(mFragmentArrayListTitle.get(contador));
tabLayout.getTabAt(contador).setCustomView(tabCustom);
}
}
/**
*
*/
public void selectTabInitialTab() {
LinearLayout linearLayoutRoot = (LinearLayout) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.custom_tab_linear_layout_animation);
linearLayoutRoot.setBackground(ContextCompat.getDrawable(mAppCompatActivity, R.drawable.aba_grande));
Animation bottomUp = AnimationUtils.loadAnimation(mAppCompatActivity,
R.anim.bottom_up);
linearLayoutRoot.startAnimation(bottomUp);
}
}
在activityattab中,Conduit片段始终是第一个创建的,位置0。 我不能用TAG来搜索这个片段。
答案 0 :(得分:2)
在CustomTabLayout
内添加
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
int position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
答案 1 :(得分:0)
您可以使用以下方式获取当前可见片段。它会让你回到原位。
viewPager.getCurrentItem();
答案 2 :(得分:0)
在您的活动中,您可以这样检查: -
PermissionRule
答案 3 :(得分:0)
使用此:
Fragment f = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem());
if (f instanceof FragmentA) {
} else {
}