【问题标题】:How can we call one fragment from another class that doesn't contain that fragment?我们如何从另一个不包含该片段的类中调用一个片段?
【发布时间】:2018-12-04 15:53:55
【问题描述】:

我有一个活动课程:IdeaBoxActivity 这是activity的布局代码-

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

有没有办法做到这一点?如果是,那么请帮助我编写应该可以工作的代码。

【问题讨论】:

    标签: java android android-activity fragment adapter


    【解决方案1】:

    您必须通过可能将在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
    }
    

    【讨论】:

    • 对不起,我没有正确理解它,您能帮我解决一下我应该在 onItemClick 方法中编写的代码吗?
    • 你想用ideaboxactivity在setItemClickListener里面做什么?
    • 要调用它的函数,您需要执行 IdeaBoxActivity.myInstance.functionName(params)。这有帮助吗?
    • 来自 setItemClickListener 我只想开始我的 IdeasFragment
    • 请注意,您不应将 IdeaBoxActivity 实例化为 new IdeaBoxActivity() 以便能够使用 myInstance 变量。它是一个静态变量,这意味着不需要在其他地方使用对基类的引用。虽然,您可能必须导入该类,但仅此而已。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多