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

心已入冬 提交于 2020-01-13 05:59:08

问题


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

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

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


回答1:


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."

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 isVisible method.

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?"

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;
    }
}



回答2:


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.java from 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());
    }

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



来源:https://stackoverflow.com/questions/20926763/how-to-get-currently-visible-fragment-from-activity-when-using-holoeverywhere-sl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!