1

I want to replace a fragment containing a list of items with a new fragment depending on the item clicked. This is my code, how do I need to approach this? Should I declare my ListView, Adapter and such in onCreateView or onActivityCreated? The container is a ViewPager if that makes a difference.

public class PreferenceListFragment extends SherlockFragment 
{
    ListView lv;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)  {
        return (LinearLayout)inflater.inflate(R.layout.settings, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lv = (ListView)getView().findViewById(R.id.SettingsLV);
        lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Preferences.TITLES));
        lv.setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    Log.i("FragmentList", "Item clicked: " + arg2);
                    // Switch case that starts fragment depending on 'position'.

                    Fragment1 f1 = new Fragment1();
                    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.pager, f1);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();     
                }
            }
        );
    }
}

Link to some very weird behaviour from my app.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
CodePrimate
  • 6,646
  • 13
  • 48
  • 86
  • Your problem not in Fragment but in the ViewPager. This question may help you: http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager – Sergey Glotov Apr 26 '12 at 11:53

1 Answers1

0

The setting of adapter and listview should be done in onActivityCreated. OnCreateView is just to draw the user interface for the first time. The processing should be in onActivityCreated(). Refer http://developer.android.com/guide/topics/fundamentals/fragments.html

user936414
  • 7,574
  • 3
  • 30
  • 29
  • I was afraid you'd say that. I edited my original post to reflect the code I had initially. It draws the listview but clicking an item doesnt act accordingly. – CodePrimate Mar 27 '12 at 10:44
  • You mean to say Fragment1 is not started onItemClick? – user936414 Mar 27 '12 at 10:50
  • This is what happens : 1. I click on an item -> Nothing happens. 2-> I click on an item again -> The list animates out and a black screen appears in stead of the list(The fragment that is supposed to be started is pink). I edited my original question to show a video of what is going on. But tell me, does me code seem correct at all ? – CodePrimate Mar 27 '12 at 10:56
  • ya.. The code you have is correct.. Found what the mistake is? – user936414 Mar 27 '12 at 11:38
  • I doubt Fragment1 is the problem. I edited my question to show Fragment1. – CodePrimate Mar 27 '12 at 12:02
  • Did you check the SherlockFragment class and fragment_1 layout. Dows fragment_1 layout also contain a fragment? – user936414 Mar 27 '12 at 12:06
  • fragment_1 only contains a LinearLayout. – CodePrimate Mar 27 '12 at 12:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9368/discussion-between-user936414-and-litemode) – user936414 Mar 27 '12 at 12:09