mardi 5 mai 2015

gridview inside tab fragment

I need help with my project, I was following this tutorial I found that creating an app with navigation drawer and tabs, when you run the app it will look like this http://ift.tt/1ENTWi3

and i was trying to change view inside the tab fragment with gridview, so I need help anyone could tell me where and how should i add the gridview

so here's my main activity java :

@SuppressWarnings("deprecation")
public class MenuUtama extends ActionBarActivity{

    Toolbar toolbar;
    private ViewPager mPager;
    private SlidingTabLayout mTabs;
    public DaftarMakanan foodList;
    GridView makananGrid;

    ViewPager viewPager;
    ActionBar actionBar;

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


        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);

        NavDrawerFragment drawerFragment = (NavDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

        mPager = (ViewPager) findViewById(R.id.MyPager);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        mTabs = (SlidingTabLayout) findViewById(R.id.MyTabs);
        mTabs.setDistributeEvenly(true);
        mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabsText);
        mTabs.setBackgroundColor(getResources().getColor(R.color.orange));
        mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.brown));

        mTabs.setViewPager(mPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_utama, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Toast.makeText(this, "This Is " + item.getTitle() + " Button",
                    Toast.LENGTH_SHORT).show();
            return true;
        }

        if (id == R.id.bill) {
            startActivity(new Intent(this, BillTagihanAnda.class));
        }

        if (id == R.id.pesanan) {
            startActivity(new Intent(this, DaftarPesananAnda.class));
        }

        if (id == R.id.callwaiter) {
            startActivity(new Intent(this, PanggilPelayan.class));
            finish();
        }
        return super.onOptionsItemSelected(item);
    }



    class MyPagerAdapter extends FragmentPagerAdapter {

        int icon[] = { R.drawable.ic_food, R.drawable.ic_drink, R.drawable.ic_desserts };
        String[] tabsText = getResources().getStringArray(R.array.tabs);


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            tabsText = getResources().getStringArray(R.array.tabs);

        }

        @Override
        public Fragment getItem(int position) {
            MyFragment myFragment = MyFragment.getInstance(position);
            return myFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Drawable drawable = getResources().getDrawable(icon[position]);
            drawable.setBounds(0, 0, 40, 40);
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            return spannableString;
        }

        @Override
        public int getCount() {
            return 3;
        }

    }

    public static class MyFragment extends Fragment {
        private TextView textView;
        private GridView gridView;

        public static MyFragment getInstance(int position) {
            MyFragment myFragment = new MyFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.my_fragment, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            gridView = (GridView) layout.findViewById(R.id.gridView);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText("The Page Selected is " + bundle.getInt("position"));
            }

            return layout;
        }

    }

}

my main activity xml :

<android.support.v4.widget.DrawerLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    xmlns:yourapp="http://ift.tt/GEGVYd"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="id.WKKR.ktcafe.MenuUtama" >

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />

        <tabs.SlidingTabLayout
            android:id="@+id/MyTabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/MyPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="id.WKKR.ktcafe.NavDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/activity_nav_drawer_fragment"
        yourapp:layout="@layout/activity_nav_drawer_fragment" />

</android.support.v4.widget.DrawerLayout>

and my fragment xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/position"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="Page Number 1"
    android:textColor="@color/black"
    android:textSize="20sp" />

(Edited) so i change the fragment xml with gridview,and now where should i put the adapter for the gridview to show data, should i create new adapter class or just add the adapter inside main activity java class? :

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="150dp"
    android:numColumns="3"
    android:stretchMode="spacingWidthUniform" >

</GridView>

Aucun commentaire:

Enregistrer un commentaire