Преглед изворни кода

page fragments / sliding tabs

David пре 9 година
родитељ
комит
89bc3eb6a4

+ 33 - 32
app/src/main/java/com/example/david/libretasker/MainActivity.java

@@ -1,59 +1,60 @@
 package com.example.david.libretasker;
 
-
-import android.content.Intent;
+import android.content.Context;
 import android.os.Bundle;
+import android.support.design.widget.TabLayout;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.view.Window;
+import android.widget.TableLayout;
 
 
 public class MainActivity extends AppCompatActivity {
-    ViewPager mViewPager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        requestWindowFeature(Window.FEATURE_ACTION_BAR); //Call before setContentView
-        setContentView(R.layout.activity_main);
-
-        final ActionBar actionBar = getSupportActionBar();
-        // Specify that tabs should be displayed in the action bar.
-        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
-        // Create a tab listener that is called when the user changes tabs.
-        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
 
+        setContentView(R.layout.activity_main);
 
-            @Override
-            public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
+        ViewPager vp = (ViewPager) findViewById(R.id.pager);
+        vp.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
+                MainActivity.this));
 
-            }
+        TabLayout t = (TabLayout) findViewById(R.id.tlayout);
+        t.setupWithViewPager(vp);
 
-            @Override
-            public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
 
-            }
+    }
 
-            @Override
-            public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
+    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
+        final int PAGE_COUNT = 3;
+        private String tabTitles[] = new String[] { "Events", "Actions", "User" };
+        private Context context;
 
-            }
-        };
+        public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
+            super(fm);
+            this.context = context;
+        }
 
-        // Add 3 tabs, specifying the tab's text and TabListener
-        for (int i = 0; i < 3; i++) {
-            actionBar.addTab(
-                    actionBar.newTab()
-                            .setText("Tab " + (i + 1))
-                            .setTabListener(tabListener));
+        @Override
+        public int getCount() {
+            return PAGE_COUNT;
         }
 
-        // ViewPager and its adapters use support library
-        // fragments, so use getSupportFragmentManager.
+        @Override
+        public Fragment getItem(int position) {
+            return PageFragment.newInstance(position + 1);
+        }
 
-        startService( new Intent(this, MainService.class) );
+        @Override
+        public CharSequence getPageTitle(int position) {
+            // Generate title based on item position
+            return tabTitles[position];
+        }
     }
-
 }

+ 42 - 0
app/src/main/java/com/example/david/libretasker/PageFragment.java

@@ -0,0 +1,42 @@
+package com.example.david.libretasker;
+
+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.TextView;
+
+/**
+ * Created by david on 01/01/17.
+ */
+
+// In this case, the fragment displays simple text based on the page
+public class PageFragment extends Fragment {
+    public static final String ARG_PAGE = "ARG_PAGE";
+
+    private int mPage;
+
+    public static PageFragment newInstance(int page) {
+        Bundle args = new Bundle();
+        args.putInt(ARG_PAGE, page);
+        PageFragment fragment = new PageFragment();
+        fragment.setArguments(args);
+        return fragment;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mPage = getArguments().getInt(ARG_PAGE);
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        View view = inflater.inflate(R.layout.fragment_page, container, false);
+        TextView textView = (TextView) view;
+        textView.setText("Fragment #" + mPage);
+        return view;
+    }
+}

+ 16 - 7
app/src/main/res/layout/activity_main.xml

@@ -1,12 +1,21 @@
 <?xml version="1.0" encoding="utf-8"?>
-<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:orientation="vertical">
 
-    <android.support.v4.view.ViewPager
-        android:id="@+id/viewpager"
+    <android.support.design.widget.TabLayout
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
-</android.support.design.widget.CoordinatorLayout>
+        android:layout_height="wrap_content"
+        android:layout_gravity="top"
+        app:tabMode="fixed"
+        android:id="@+id/tlayout" />
+
+    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/pager"
+        android:layout_width="match_parent"
+        android:layout_height="0px"
+        android:layout_weight="1" /> >
+
+</LinearLayout>

+ 5 - 0
app/src/main/res/layout/fragment_page.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:gravity="center" />

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -3,4 +3,7 @@
 
     <!-- TODO: Remove or change this placeholder text -->
     <string name="hello_blank_fragment">Hello blank fragment</string>
+    <string name="dummy_section_text">Section %1d is just a dummy section.</string>
+
+
 </resources>