0

At the moment I have a activity with a viewpager which inflates a listfragment. When a user click on a item in the listview I would like to inflate a new fragment.

Test.java

public class Test extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        final Toolbar toolbar;
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        ViewPager vp = (ViewPager) findViewById(R.id.n_Viewpager);
        this.addPages(vp);

    }

    //ADD ALL PAGES
    private void addPages(ViewPager pager) {
        MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
        adapter.addPage(new TestFragment());

        //SET ADAPTER TO PAGER
        pager.setAdapter(adapter);

    }
}

layout.activity_test

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="tutorial.com.ScratchGolfer.Test">

    <RelativeLayout
        android:id="@+id/nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appbarr"
            >

            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />


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

            <android.support.v4.view.ViewPager
                android:id="@+id/n_Viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/appbarr"
                app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
                />

    </RelativeLayout>


</RelativeLayout>

TestFragment.java

public class TestFragment extends ListFragment {

    //Setting the name of each event
    String[] options = {"1", "2", "3", "4", "5", "6", "7"};
    ArrayAdapter<String> adapter;


    //Inflating the view with the fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        adapter = new ArrayAdapter<>(getActivity(), R.layout.listitem, R.id.textview, options);

        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);

    }


    //Click events
    @Override
    public void onStart() {
        super.onStart();

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int pos, long l) {
                switch (pos) {
                    case 0:
                        Intent intent0 = new Intent(v.getContext(), NewActivity.class);
                        v.getContext().startActivity(intent0);
                        break;
                    case 1:

                        break;

                }

            }
        });
    }
}

I have managed to open a activity when I click a item in the listview, but I would like to inflate the current activity with a new fragment depending on what item is clicked in the listview

2
  • Where do you want to replace the fragment? inside the ViewPager? Commented Jan 25, 2017 at 7:18
  • yes, inside the ViewPager Commented Jan 25, 2017 at 7:20

1 Answer 1

1

It's not the best and shortest solution, but I found it more understandable. You can create a Fragment as container and add it to your ViewPager. Then replace fragments inside it.

fragment_container.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/fragment_container_root_view">
</LinearLayout>

ContainerFragment.Java

public class ContainerFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_container, container, false);
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new TestFragment()).commit();
        return view;
    }
}

In you activity:

private void addPages(ViewPager pager) {
    MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
    adapter.addPage(new ContainerFragment());

    //SET ADAPTER TO PAGER
    pager.setAdapter(adapter);

}

In your TestFragment:

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int pos, long l) {
            switch (pos) {
                case 0:
                    getParentFragment().getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new SecondFragment()).addToBackStack("").commit();

                    break;
                case 1:

                    break;

            }

        }
    });

I didn't test this code to see if it has error or not, but with some changes, You can achieve to what you want.

Sign up to request clarification or add additional context in comments.

9 Comments

Ok wait you lost me there. My fragment container is activity_test it holds a ViewHolder, look at my code above. What is the fragment_container.xml you refer to? I also get a error at ContainerFragment.java at new TestFragment()).commit(); (wrong second argument type). Can you please edit your code so it resembles mine. My main activity is Test.java my main activity is activity_test.xml (this holds the viewholder)
@H.Brooks Ok... look... You have a Activity with a ViewPager in it. You want to show a ListFragment in ViewPager and when user clicked on an item, you want to replace another fragment in ViewPage... Is that right?
Now instead of adding TestFragment in ViewPager, Add ContainerFragment in it, then add your TestFragment in ContainerFragment. now when user clicks on any item, you can replace SecondFragment with TestFragment in ContainerFragment.
Ok.. I understand.. but I have some errors trying to accomplish this. I get a error in ContainerFragment -- getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new TestFragment()).commit(); -- wrong 2nd argument type at new TestFragment
@H.Brooks Check your ListViewextended from support-v4 package.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.