Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

bauke plugge
bauke plugge
291 Points

is there a way to make the onclickitem of a listview go to a new fragment instead of an intent ?

im making an app will the first view all list items and I want them to move to the correct corresponding fragment.

is that possible ?

3 Answers

This is basically a navigation drawer without the pop out navigation. You can create your own simply by using the basic templates and Android Studio.

1) Create a new project with the blank activity with fragment template. 2) Add a new Fragment (List) 3) Change back ground color of fragment_main.xml layout file - I picked sky - blue 4) Implement "Fragment List Class you created".OnFragmentInteractionListener - on main activity dont press Alt + Enter 4a) paste this code in MainActivty

@Override
   public void onFragmentInteraction(String id) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

5) In the onCreate method replace or add this code after the setContentView(R.layout......

// You need to use the name of the Fragment (list) you created I called mine ItemFragment
if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, ItemFragment.newInstance("String","String"))
                    .commit();
        }

6) Comment or delete imports and make sure to select the support Library option if there are Errors

// import android.app.Fragment;
import android.support.v4.app.Fragment; 

That should do It. At least show you it works check out the project in the git link below

Here is the link to code files.

https://github.com/mkline187/ListTest/tree/master/app/src/main/java/com/moonstub/app/tester/listtest

bauke plugge
bauke plugge
291 Points

thanks for the answer.

although it seems a little complex to me :-) but will try to get it implemented in my app

bauke plugge
bauke plugge
291 Points

its not the complete answer im looking for. I would say its not creating an fragment. but also a soort of intent page.