1

I want to add FileChooser(← I use this) to my app(my Main Activity extends ActionBarActivity).And I tried to change FileChooser to Fragment or ListFragment. Because I want pages can be selected by user(I use drawer to select Fragment).

My app's structure like the picture:enter link description here(because i can't post pictuer here)

But if I change to Fragment it didn't work. If I change to ListFragment it will work ,but It displayed over last fragment like the picture:it beside the picture of above(please click the link again,because i can't post so many link)

AndroidManifest.xml: android:minSdkVersion="11" android:targetSdkVersion="21"

My selectitem() code:

private void selectItem(int position) {        
    Fragment fragment = null;
    switch (position) {
    case 0:
        Toast.makeText(getApplicationContext(),"case 0", Toast.LENGTH_SHORT).show();
        fragment = new First();
        break;
    case 1:            
        Toast.makeText(getApplicationContext(),"case 1", Toast.LENGTH_SHORT).show();            
        fragment = new Second();
        break;          
    case 2:
        Toast.makeText(getApplicationContext(),"case 2", Toast.LENGTH_SHORT).show();
         fragment = new Third();
         break;        
    }

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.addToBackStack("home");
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();


    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

My question:

  1. How to change FileChooser from ListActivity to Fragment or ListFragment?(If someone has other methods, please tell me)
  2. How can I display it after user click button(in First's layout) and will not see last Fragment's layout? (if it can change to Fragment or ListFragment)
  3. continue2 , How to Transfer data to other fragments?
  4. Which is better to import (in ActionBarActivity)? (android.app.Fragment or android.support.v4.app.Fragment)

Thanks for everyone who read my question

1 Answer 1

1

I'll start from bottom to top.

Which is better to import (in ActionBarActivity)? (android.app.Fragment or android.support.v4.app.Fragment)

Answer: Neither. It depends on what your doing and the API level you wish to conform with. I tend to use android.support.v4.app.Fragment more than the standard android.app.fragment but that's just preference more than anything.

How can I display it after user click button(in First's layout) and will not see last Fragment's layout? (if it can change to Fragment or ListFragment) continue2 , How to Transfer data to other fragments?

Answer: A listfragment is a type of a fragment with a few extra methods. To replace a standard fragment with a listfragment you'll do the same thing.

FragmentTransaction ft = getFragmentManager.beginTransaction;

ft.replace(containerId, new MyListFragment or Fragment)
                         .commit();

As per moving data between fragments, the simplest way is to use a Bundle.

Bundle data = new Bundle();
data.putInt("myInt", 1);
data.putString("myString" "hello java")
myFragment.setArguments(data);

and in your Fragment class you'll get the data out of the bundle like this.

Bundle data = getArguments();
int myInt = data.getInt("myInt");
String myString = data.getString("myString");

and

How to change FileChooser from ListActivity to Fragment or ListFragment?(If someone has other methods, please tell me)

Are you looking at implementing the FileChooser class in a Fragment class or what exactly are you trying to say by this?

My selectFragment method would look like this:

public Fragment getFragment(int pos){
    switch(pos) {
        case 1:
            return new FragmentOne();
        case 2:
            return new FragmentTwo();
        case 3:
            return new FragmentThree();
        default:
            return FragmentOne();


    }
}
2
  • Thank you for your answer. I will try it when I have free time.
    – ponxin
    Oct 13, 2014 at 13:50
  • No problem. If you can't make sense of it / get it working, let me know.
    – Seth
    Oct 13, 2014 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.