确定当前可见的片段

时间:2015-01-30 11:37:43

标签: android android-fragments

我使用了expandable navigation drawer

这是FrameLayout的活动。如何识别用户当前可见的Fragment

在此活动中,许多imageButton因此差异fragment我必须执行不同的事件,因此imageButton onclick我已确定用户当前可见的片段。

`          

        <ImageView
            android:background="@drawable/navigation_icon"
            android:id="@+id/titleHeaderImageView"
            android:layout_width="75dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            />

        <ImageView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/header"
            android:id="@+id/wallHeader" />
        <ImageButton
            android:id="@+id/btnActivityBack"
            android:src="@drawable/prev"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="100dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
        <ImageButton
            android:id="@+id/btnActivityWrong"
            android:src="@drawable/no"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>

        <ImageButton
            android:id="@+id/btnActivityRight"
            android:src="@drawable/yes"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>

        <ImageButton
            android:id="@+id/btnActivityEdit"
            android:src="@drawable/edit"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
        <ImageButton
            android:id="@+id/btnActivitySearch"
            android:src="@drawable/search"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
        <ImageButton
            android:id="@+id/btnActivityAdd"
            android:src="@drawable/plus"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
        <ImageButton
            android:id="@+id/btnActivityNext"
            android:src="@drawable/next"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
        <ImageButton
            android:id="@+id/btnActivitySetting"
            android:src="@drawable/setting"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"/>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@color/color2"/>
</LinearLayout>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Drawer Content -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color3"
        android:layout_marginTop="0dp"/>


    <!-- The navigation menu -->

    <ExpandableListView
        android:id="@+id/navigationExpandableListView"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"

        android:background="@color/ex_listview_background" >
    </ExpandableListView>
</android.support.v4.widget.DrawerLayout>

`

5 个答案:

答案 0 :(得分:1)

找到当前片段

Fragment currentFragment = getFragmentManager().findFragmentById(R.id.content_frame);

这会找到当前添加到FrameLayout(content_frame)的片段。 这应该足够了,但是如果你想检查它是否真的可见,请使用以下内容:

currentFragment.isVisible();

Fragment.isVisible()的文档 -

  

如果片段当前对用户可见,则返回true。这个   意思是:(1)已添加,(2)其视图附在窗口上,   (3)不隐藏。

答案 1 :(得分:0)

为了识别现在可见的片段,当您单击ListView项时,需要将片段名称存储在任何字符串变量中。每次替换片段时,您需要将该片段的名称存储在该变量中。

答案 2 :(得分:0)

您可以执行以下操作:

String currentFragmentName;

imageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        currentFragmentName = "Current fragment name";

    }
});

这样做,您将可以控制当前可见的fragment

答案 3 :(得分:0)

也许您不需要获取当前片段。只需在片段类中添加事件处理程序,如下所示:

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

  swith(page){
     case:1
       ....
     case:2
       ....

  }

}

页面是页码。通常,您应该在创建时将页码传递给Fragment。例如,Fragment类可以使用它来创建函数:

    int page;
    public static FirstFragment newInstance(int page, String title) {
       FirstFragment fragmentFirst = new FirstFragment();
       fragmentFirst.page=page;
       return fragmentFirst;

}

答案 4 :(得分:0)

当您显示片段时,您必须将其标记放入backstack中,如:

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
 ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);       
 ft.add(R.id.primaryLayout, fragment, tag);
 ft.addToBackStack(tag);
 ft.commit();     

在下面的基本活动中创建一个方法,并在onactivitycreated()方法中的片段中调用它,它将返回片段的标记,然后你可以使用switch case。

public String getActiveFragment() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
    return null;
}
String tag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
return tag;
相关问题