Mahesh
Mahesh

Reputation: 1589

Fragment not work when actionbar activity start in android?

hello i am implement list view in action bar activity when first time it is start but it is not work

i have tried below code but it is not working for me i have just started the learn actionbar

MainActivity.java

public class MainActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the
     * navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in
     * {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        Fragment fr=new FragmentOne();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager .beginTransaction().replace(R.id.container, fr);

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments



    }


    public void restoreActionBar() {


        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class FragmentOne extends Fragment {


            private String[] mStrings={
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png"

            };

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {


                return inflater.inflate(R.layout.fragmentone,container,false);

            }


            @Override
            public void onActivityCreated(Bundle savedInstanceState) {

                super.onActivityCreated(savedInstanceState);

                ListView listview=(ListView) getActivity().findViewById(R.id.listView);

                  // Create custom adapter for listview
                LazyImageLoadAdapter adapter=new LazyImageLoadAdapter(getActivity(), mStrings);

                //Set adapter to listview
                listview.setAdapter(adapter);


            }

        }
}

Upvotes: 0

Views: 341

Answers (1)

Juan Aguilar Guisado
Juan Aguilar Guisado

Reputation: 1701

Commit the transaction!

fragmentManager.beginTransaction().replace(R.id.container, fr).commit();

Upvotes: 1

Related Questions