我们如何从另一个不包含该片段的类中调用一个片段? [英] How can we call one fragment from another class that doesn't contain that fragment?

查看:84
本文介绍了我们如何从另一个不包含该片段的类中调用一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动类: 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
工具: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
应用程序: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(),写点子);
sectionPageadapter.addFragment(new MyIdeasFragment(),我的想法);

viewPager.setAdapter(sectionPageadapter);
}

在这些片段中,一个是: IdeasFragment p>

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

 公共类RecyclerAdapterSeeAll扩展了RecyclerView.Adapter< RecyclerAdapterSeeAll.MyViewHolder> {

私有列表< ModelHappySeeAll> ; modelHappySeeAlls;
私有上下文上下文;
private int id = 0;
private String status =;

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


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

@Override
public void onBindViewHolder(MyViewHolderholder,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();
}


公共类MyViewHolder扩展了RecyclerView.ViewHolder实现的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 内的类我想调用 IdeaBoxActivity IdeasFragment



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

解决方案

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



java中的全局变量

 公共静态IdeasFragment myInstance = new IdeasFragment(); 

private void setupViewPager(ViewPager viewPager){
SectionPageadapter sectionPageadapter = new
SectionPageadapter(getSupportFragmentManager());
sectionPageadapter.addFragment(myInstance,我的想法);

viewPager.setAdapter(sectionPageadapter);
}

在点击监听器上:

  public void setItemClickListener(ItemClickListener itemClickListener){

IdeaBoxActivity.myInstance.start(); //在此处调用启动IdeasFragment所需的函数
}


I am having one activity class : IdeaBoxActivity Here is the layout code of the 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>

I have added some fragments in the IdeaBoxActivity .

I have added the fragments using following method-

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

Among these fragments one is : IdeasFragment

I have another activity which named: HappyWallActivity From this Activity I have called one adapter class named: 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());
        }
    }

}

In this class inside holder.setItemClickListener I want to call IdeasFragment of the IdeaBoxActivity.

Is there any way of doing this? If yes, then please help me with the code that should work.

解决方案

You have to use the same instance of Ideas fragment by probably storing the original instance made on new IdeasFragment() to a global variable, or you could pass the instance when you do new RecyclerAdapterSeeAll(IdeasFragment instance) and use it like that. Either way the idea here is to use the same instances on both classes.

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

And on the click listener:

public void setItemClickListener ( ItemClickListener itemClickListener){

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

这篇关于我们如何从另一个不包含该片段的类中调用一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆