Android 使用holoeverywhere滑块插件时如何从活动中获取当前可见的片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20926763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:42:22  来源:igfitidea点击:

How to get currently visible fragment from activity when using holoeverywhere slider addon?

androidandroid-holo-everywhere

提问by Mehdi Fanai

I want to know how to perform these actions in holoeverywhere:

我想知道如何在holoeverywhere 中执行这些操作:

Get reference to the currently visible and active fragment from the activity when using slider addon in combination with tabber.

将滑块插件与 tabber 结合使用时,从活动中获取对当前可见和活动片段的引用。

Get reference to TabsTabsSwipeFragment from the activity/other fragments and dynamically disable/enable swiping.

从活动/其他片段获取对 TabsTabsSwipeFragment 的引用并动态禁用/启用滑动。

采纳答案by Mehdi Fanai

Get a reference to the currently visible and active Fragment from the Activity when using slider addon in combination with tabber:

You can provide a tag for a Fragment like this:

sliderMenu.add("tab2", Fragment2.class, SliderMenu.GREEN).setTag("mynavigation-2");

Also you can get the current page number using:

sliderMenu.getCurrentPage();

You have no way of obtaining the current Fragment directly, but you can combine these two methods and find a Fragment via the FragmentManager:

getSupportFragmentManager().findFragmentByTag("mynavigation-" + sliderMenu.getCurrentPage());

Get a reference to the TabsTabsSwipeFragment.javafrom the Activity/other Fragments and dynamically disable/enable swiping. In your code:

getSupportFragmentManager().findFragmentByTag("mynavigation-2").getChildFragmentManager().findFragmentById(tagFragmentId);

In your Activity:

private int tagFragmentId;
public void setTagFragmentId(int i) {
    Log.i(TAG, "fetched setTagFragmentId: " + i);
    tagFragmentId= i;
}

In your tab Fragment (which is a child of TabsTabsSwipeFragment):

   public void onViewCreated(View view, Bundle savedInstanceState) 
        ((mainActivity) getSupportActivity()).setTagFragmentId(this
                .getId());
    }

将滑块插件与 tabber 结合使用时,从 Activity 获取对当前可见和活动 Fragment 的引用:

您可以为 Fragment 提供一个标签,如下所示:

sliderMenu.add("tab2", Fragment2.class, SliderMenu.GREEN).setTag("mynavigation-2");

您也可以使用以下方法获取当前页码:

sliderMenu.getCurrentPage();

您无法直接获取当前的 Fragment,但是您可以将这两种方法结合起来,通过以下方式找到一个 Fragment FragmentManager

getSupportFragmentManager().findFragmentByTag("mynavigation-" + sliderMenu.getCurrentPage());

TabsTabsSwipeFragment.java从活动/其他片段获取对 的引用并动态禁用/启用滑动。在您的代码中:

getSupportFragmentManager().findFragmentByTag("mynavigation-2").getChildFragmentManager().findFragmentById(tagFragmentId);

在您的活动中:

private int tagFragmentId;
public void setTagFragmentId(int i) {
    Log.i(TAG, "fetched setTagFragmentId: " + i);
    tagFragmentId= i;
}

在您的选项卡 Fragment (它是 的子代TabsTabsSwipeFragment)中:

   public void onViewCreated(View view, Bundle savedInstanceState) 
        ((mainActivity) getSupportActivity()).setTagFragmentId(this
                .getId());
    }

Reference: https://github.com/Prototik/HoloEverywhere/wiki/Addon:-Slider

参考:https: //github.com/Prototik/HoloEverywhere/wiki/Addon: -Slider

回答by Tom

For getting the current active and visible Fragment, it's my understanding that HoloEverywhere uses Android's Support Library. Try something similar to "get currently displayed fragment."

为了获取当前活动和可见的 Fragment,我的理解是 HoloEverywhere 使用 Android 的支持库。尝试类似于“获取当前显示的片段”

public Fragment getActiveFragment() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
        return null;
    }
    String tag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
    return getSupportFragmentManager().findFragmentByTag(tag);
}

That said, when using ActionBar tabs this may get complicated because two or more Fragments can be visible at any given time (i.e. swiping between the two). You may wish to give your Fragments tags and search for them manually by querying the isVisiblemethod.

也就是说,当使用 ActionBar 选项卡时,这可能会变得复杂,因为在任何给定时间(即在两者之间滑动)都可以看到两个或多个 Fragment。您可能希望提供 Fragments 标签并通过查询isVisible方法手动搜索它们。

Now for disabling swiping, I have no idea what TabsTabsSwipeFragment is but you can get a reference to any Fragment by querying their tag from the Fragment manager, of if you really want by looping through all Fragments and comparing the class (Object.getClass). Disabling a ViewPager from being swiped can be accomplished by something similar to "How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?"

现在为了禁用滑动,我不知道 TabsTabsSwipeFragment 是什么,但是您可以通过从 Fragment 管理器查询它们的标签来获得对任何 Fragment 的引用,如果您真的需要,可以通过循环遍历所有 Fragment 并比较类 ( Object.getClass)。可以通过类似于“如何通过在 ViewPager 中用手指滑动来禁用分页但仍然能够以编程方式滑动?”来实现禁用 ViewPager 的滑动。

package com.yourcompany;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class SwipeableViewPager extends ViewPager {

    private boolean swipeable = true;

    public SwipeableViewPager(Context context) {
        super(context);
    }

    public SwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setSwipeable(boolean swipe) {
        this.swipeable = swipe;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return (swipeable) super.onInterceptTouchEvent(event) : false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return (swipeable) super.onTouchEvent(event) : false;
    }
}