簡體   English   中英

我們如何從不包含該片段的另一個類中調用一個片段?

[英]How can we call one fragment from another class that doesn't contain that fragment?

我有一個活動類: 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>

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

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

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

這些片段之一是: IdeasFragment

我有另一個活動,名為: HappyWallActivity從這個活動中,我調用了一個名為RecyclerAdapterSeeAll 的適配器類

public class RecyclerAdapterSeeAll extends RecyclerView.Adapter<RecyclerAdapterSeeAll.MyViewHolder>{

    private List<ModelHappySeeAll> modelHappySeeAlls;
    private Context context;
    private int id = 0;
    private String status = "";

    public RecyclerAdapterSeeAll( Context context, List<ModelHappySeeAll> modelHappySeeAlls) {
        this.modelHappySeeAlls = modelHappySeeAlls;
        this.context = context;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_see_all, parent, false);
        return  new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.tvName.setText(modelHappySeeAlls.get(position).getName());
        holder.tvDetails.setText(modelHappySeeAlls.get(position).getDetails());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int pos) {


            }
        });



    }

    @Override
    public int getItemCount() {
        return modelHappySeeAlls.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tvName, tvDetails;
        ItemClickListener itemClickListener;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.textview_name_see_all);
            tvDetails = itemView.findViewById(R.id.textview_happy_post_see_all);
        }

        public void setItemClickListener ( ItemClickListener itemClickListener){
            this.itemClickListener = itemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            this.itemClickListener.onItemClick(this.getLayoutPosition());
        }
    }

}

在這個類中holder.setItemClickListener我想調用IdeaBoxActivityIdeasFragment

有沒有辦法做到這一點? 如果是,那么請幫助我編寫應該可以工作的代碼。

您必須通過可能將在new IdeasFragment()new IdeasFragment()的原始實例存儲到全局變量來使用相同的 Ideas 片段實例,或者您可以在執行new RecyclerAdapterSeeAll(IdeasFragment instance)時傳遞該實例並像這樣使用它。 無論哪種方式,這里的想法都是在兩個類上使用相同的實例。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM