Android Open Source - android-navigation-drawer-example Center Fragment






From Project

Back to project page android-navigation-drawer-example.

License

The source code is released under:

Apache License

If you think the Android project android-navigation-drawer-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bnsantos.navigationdrawer.fragment;
/*from www .j a va2  s.c  om*/
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.bnsantos.navigationdrawer.R;
import com.bnsantos.navigationdrawer.Utils;
import com.bnsantos.navigationdrawer.adapter.ItemAdapter;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
 */
public class CenterFragment extends Fragment implements AbsListView.OnItemClickListener {

    /**
     * The fragment's ListView/GridView.
     */
    private AbsListView mListView;

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private ItemAdapter mAdapter;

    public static CenterFragment newInstance() {
        CenterFragment fragment = new CenterFragment();
        return fragment;
    }

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public CenterFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAdapter = new ItemAdapter(getActivity(), Utils.generateRamdomItems(getResources().getStringArray(R.array.countries)));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);

        // Set the adapter
        mListView = (AbsListView) view.findViewById(android.R.id.list);
        ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

        // Set OnItemClickListener so we can be notified on item clicks
        mListView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();

        if (emptyView instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }
}




Java Source Code List

com.bnsantos.navigationdrawer.ApplicationTest.java
com.bnsantos.navigationdrawer.Utils.java
com.bnsantos.navigationdrawer.activity.MainActivity.java
com.bnsantos.navigationdrawer.adapter.ItemAdapter.java
com.bnsantos.navigationdrawer.adapter.ViewPagerAdapter.java
com.bnsantos.navigationdrawer.fragment.CenterFragment.java
com.bnsantos.navigationdrawer.fragment.LeftFragment.java
com.bnsantos.navigationdrawer.fragment.RightFragment.java
com.bnsantos.navigationdrawer.model.Item.java