我们如何从另一个不包含该片段的类中调用一个片段?

时间:2018-12-04 15:53:55

标签: java android android-activity fragment adapter

我正在参加一个活动课程: IdeaBoxActivity 这是活动的布局代码-

fillna()

我在 IdeaBoxActivity 中添加了一些片段。

我使用以下方法添加了片段-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.asif047">


    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/dl"
        >

        <LinearLayout
            android:id="@+id/flcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_idea_box"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="16dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />



            <android.support.design.widget.TabLayout
                android:id="@+id/tabs_idea"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                app:tabTextColor="#ffffff"
                />


        <android.support.v4.view.ViewPager
            android:id="@+id/container_idea"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />



        </LinearLayout>

        <android.support.design.widget.NavigationView

            android:id="@+id/nv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#ffffff"
            app:headerLayout="@layout/header"
            app:itemIconTint="#000000"
            app:itemTextColor="#000000"
            app:menu="@menu/drawermenu"

            >


        </android.support.design.widget.NavigationView>


    </android.support.v4.widget.DrawerLayout>



</android.support.design.widget.CoordinatorLayout>

这些片段中的一个是: IdeasFragment

我还有另一个活动,名为: HappyWallActivity 在此活动中,我调用了一个适配器类,名为: RecyclerAdapterSeeAll

private void setupViewPager(ViewPager viewPager) {
    SectionPageadapter sectionPageadapter = new SectionPageadapter(getSupportFragmentManager());
    sectionPageadapter.addFragment(new IdeasFragment(), "Ideas");
    sectionPageadapter.addFragment(new WriteIdeaFragment(), "Write Idea");
    sectionPageadapter.addFragment(new MyIdeasFragment(), "My Ideas");

    viewPager.setAdapter(sectionPageadapter);
}

在此类中的 holder.setItemClickListener 中,我想调用 IdeaBoxActivity IdeasFragment

有什么办法吗?如果是,那么请帮助我提供应该起作用的代码。

1 个答案:

答案 0 :(得分:0)

您必须使用相同的Ideas片段实例,方法是将在new IdeasFragment()上创建的原始实例存储到全局变量中,否则您可以在执行new RecyclerAdapterSeeAll(IdeasFragment instance)时使用该实例并像这样使用它。无论哪种方式,这里的想法都是在两个类上使用相同的实例。

Globals in java

public static IdeasFragment myInstance= new IdeasFragment(); 

private void setupViewPager(ViewPager viewPager) {
    SectionPageadapter sectionPageadapter = new 
    SectionPageadapter(getSupportFragmentManager());
    sectionPageadapter.addFragment(myInstance, "My Ideas");

    viewPager.setAdapter(sectionPageadapter);
} 

在点击侦听器上:

public void setItemClickListener ( ItemClickListener itemClickListener){

    IdeaBoxActivity.myInstance.start(); // call the function needed to start the IdeasFragment here
}