-1

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.

Asif047
  • 21
  • 1
  • 1
  • 9

1 Answers1

0

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
}
TheFlyBiker 420
  • 69
  • 1
  • 13
  • Sorry I am not getting it properly, can you please help me out with the code that I should write inside the onItemClick method? – Asif047 Dec 04 '18 at 16:17
  • What do you want to do inside setItemClickListener with ideaboxactivity? – TheFlyBiker 420 Dec 04 '18 at 16:19
  • To Call it's function you need to do IdeaBoxActivity.myInstance.functionName(params). Did that help? – TheFlyBiker 420 Dec 04 '18 at 16:20
  • from setItemClickListener I just want to start my IdeasFragment – Asif047 Dec 04 '18 at 16:21
  • Note that you should not instantiate IdeaBoxActivity as `new IdeaBoxActivity()` to be able to use the myInstance variable. It's a static variable, meaning a reference to the base class is not needed to use it else where. Although, you may have to import the class but that's about it. – TheFlyBiker 420 Dec 04 '18 at 16:22
  • I have edited my question, can you please refer to it? – TheFlyBiker 420 Dec 04 '18 at 16:24
  • Sorry, still not solving my problem – Asif047 Dec 04 '18 at 16:53
  • I have created the instance of IdeaFragment inside the IdeaBoxActivity and inside the onClick method ------- SectionPageadapter sectionPageadapter = new SectionPageadapter(getSupportFragmentManager()); sectionPageadapter.addFragment(IdeaBoxActivity.ideasFragment, "My Ideas"); viewPager.setAdapter(sectionPageadapter); ----------------------- I wrote the above code. but it is not getting the getSupportFragmentManager() method and viewPager as well – Asif047 Dec 04 '18 at 17:27