From 0761accde5326f455889ae48eff5078dc6eb2b84 Mon Sep 17 00:00:00 2001 From: vontell Date: Wed, 13 Jul 2016 20:32:40 -0400 Subject: [PATCH] Created text tabs, ready for alpha release on Jitpack.io --- .idea/gradle.xml | 4 +- .idea/misc.xml | 2 +- .idea/modules.xml | 3 +- app/build.gradle | 2 +- .../MainActivity.java | 43 +++- app/src/main/res/layout/activity_main.xml | 24 +- app/src/main/res/values/colors.xml | 6 + .../pagerindicatorbinder/IndicatorBinder.java | 85 -------- .../.gitignore | 0 .../build.gradle | 2 +- .../proguard-rules.pro | 0 .../pagerindicatorbinder/ApplicationTest.java | 0 .../src/main/AndroidManifest.xml | 0 .../pagerindicatorbinder/ImageAdapter.java | 0 .../pagerindicatorbinder/IndicatorBinder.java | 205 ++++++++++++++++++ .../src/main/res/values/strings.xml | 0 .../pagerindicatorbinder/ExampleUnitTest.java | 0 readme.md | 21 +- settings.gradle | 2 +- 19 files changed, 285 insertions(+), 114 deletions(-) delete mode 100644 iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java rename {iindicatorbinder => indicatorbinder}/.gitignore (100%) rename {iindicatorbinder => indicatorbinder}/build.gradle (95%) rename {iindicatorbinder => indicatorbinder}/proguard-rules.pro (100%) rename {iindicatorbinder => indicatorbinder}/src/androidTest/java/com/avontell/pagerindicatorbinder/ApplicationTest.java (100%) rename {iindicatorbinder => indicatorbinder}/src/main/AndroidManifest.xml (100%) rename {iindicatorbinder => indicatorbinder}/src/main/java/com/avontell/pagerindicatorbinder/ImageAdapter.java (100%) create mode 100644 indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java rename {iindicatorbinder => indicatorbinder}/src/main/res/values/strings.xml (100%) rename {iindicatorbinder => indicatorbinder}/src/test/java/com/avontell/pagerindicatorbinder/ExampleUnitTest.java (100%) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 1cdfc84..7ceadaf 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -10,14 +10,14 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb6828..5d19981 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 343e878..015bc70 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,7 +4,8 @@ - + + diff --git a/app/build.gradle b/app/build.gradle index 3fe0fdb..3a3fcdd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,5 +23,5 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0' - compile project(path: ':iindicatorbinder') + compile project(path: ':indicatorbinder') } diff --git a/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java b/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java index ed39803..01c8b77 100644 --- a/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java +++ b/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java @@ -4,10 +4,14 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.LinearLayout; +import android.widget.TextView; import com.avontell.pagerindicatorbinder.ImageAdapter; import com.avontell.pagerindicatorbinder.IndicatorBinder; +import java.util.ArrayList; +import java.util.List; + public class MainActivity extends AppCompatActivity { @Override @@ -39,13 +43,38 @@ protected void onCreate(Bundle savedInstanceState) { selectedImage, unselectedImage); - // OPTIONAL: Attach more than one indicator container! - LinearLayout alternateContainer = (LinearLayout) findViewById(R.id.indicator_alt); - IndicatorBinder.bind(this, - viewPager, - alternateContainer, - selectedImage, - unselectedImage); + // EXAMPLE WITH TABS ----------------------------------------------------------------------- + + // Create each tab TextView + List tabViews = new ArrayList<>(); + tabViews.add(new TextView(this)); + tabViews.add(new TextView(this)); + tabViews.add(new TextView(this)); + tabViews.add(new TextView(this)); + tabViews.add(new TextView(this)); + tabViews.get(0).setText("Tab 1"); + tabViews.get(1).setText("Tab 2"); + tabViews.get(2).setText("Tab 3"); + tabViews.get(3).setText("Tab 4"); + tabViews.get(4).setText("Tab 6"); + + // Set the requested colors + int selectedBackgroundColor = R.color.tabBackgroundSelected; + int selectedTextColor = R.color.tabTextSelected; + int unselectedBackgroundColor = R.color.tabBackgroundUnselected; + int unselectedTextColor = R.color.tabTextUnselected; + + // Grab the LinearLayout + LinearLayout tabContainer = (LinearLayout) findViewById(R.id.tab_container); + + IndicatorBinder.bindTextTabs(this, + viewPager, + tabContainer, + tabViews, + selectedBackgroundColor, + unselectedBackgroundColor, + selectedTextColor, + unselectedTextColor); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ca0874d..70de161 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,10 +7,19 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.avontell.pagerindicatorbinderexample.MainActivity"> + + + android:id="@+id/view_pager_one" + android:layout_below="@+id/tab_container" + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true"> @@ -26,18 +35,5 @@ - - - - diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..eee415f 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,10 @@ #3F51B5 #303F9F #FF4081 + + #3f51b5 + #5c6bc0 + #FFFFFF + #FFFFFF + diff --git a/iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java b/iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java deleted file mode 100644 index bdb1035..0000000 --- a/iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.avontell.pagerindicatorbinder; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.v4.view.ViewPager; -import android.view.ViewGroup; -import android.widget.ImageView; - -/** - * A class that binds together a ViewPager with a view that supports - * children. The position of the ViewPager will update the view - * with the children such that the current position is indicated - * @author Aaron Vontell - * @version 0.1 - */ -public class IndicatorBinder { - - /** - * Binds the viewPager to indicatorContainer, such that indicatorContainer has a list of - * indicators, all displaying the drawable at indicatorOffResource except for the ith child, - * where i is the position of the viewPager, and whose visual is given by indicatorOffResource. - * * Note that this attaches an OnPageChangeListener to viewPager. Also note that viewPager - * must have an adapter attached. - * @param context The calling application - * @param viewPager The ViewPager you wish to attach indicators to - * @param indicatorContainer The container that will hold each indicator resource - * @param indicatorOnResource The drawable to display for indicators that are not selected - * @param indicatorOffResource The drawable to display for the selected indicator - */ - public static void bind(final @NonNull Context context, - @NonNull ViewPager viewPager, - @NonNull final ViewGroup indicatorContainer, - @DrawableRes final int indicatorOnResource, - @DrawableRes final int indicatorOffResource) { - - // Load the indicator drawables - final Drawable selected = context.getResources().getDrawable(indicatorOnResource); - final Drawable unselected = context.getResources().getDrawable(indicatorOffResource); - - // Load the container with indicators - final int numItems = viewPager.getAdapter().getCount(); - indicatorContainer.removeAllViews(); - for(int i = 0; i < numItems; i++) { - ImageView indImage = new ImageView(context); - if(i == viewPager.getCurrentItem()) { - indImage.setImageDrawable(selected); - } else { - indImage.setImageDrawable(unselected); - } - indicatorContainer.addView(indImage); - } - - // Listener to detect when to update indicators - viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - - } - - @Override - public void onPageSelected(int position) { - - for(int i = 0; i < numItems; i++){ - ImageView indicator = (ImageView) indicatorContainer.getChildAt(i); - if(i == position){ - indicator.setImageDrawable(selected); - } - else { - indicator.setImageDrawable(unselected); - } - } - - } - - @Override - public void onPageScrollStateChanged(int state) { - - } - }); - - } - -} diff --git a/iindicatorbinder/.gitignore b/indicatorbinder/.gitignore similarity index 100% rename from iindicatorbinder/.gitignore rename to indicatorbinder/.gitignore diff --git a/iindicatorbinder/build.gradle b/indicatorbinder/build.gradle similarity index 95% rename from iindicatorbinder/build.gradle rename to indicatorbinder/build.gradle index 6941a8c..51f491d 100644 --- a/iindicatorbinder/build.gradle +++ b/indicatorbinder/build.gradle @@ -8,7 +8,7 @@ android { minSdkVersion 16 targetSdkVersion 24 versionCode 1 - versionName "0.1" + versionName "0.1.1" } buildTypes { release { diff --git a/iindicatorbinder/proguard-rules.pro b/indicatorbinder/proguard-rules.pro similarity index 100% rename from iindicatorbinder/proguard-rules.pro rename to indicatorbinder/proguard-rules.pro diff --git a/iindicatorbinder/src/androidTest/java/com/avontell/pagerindicatorbinder/ApplicationTest.java b/indicatorbinder/src/androidTest/java/com/avontell/pagerindicatorbinder/ApplicationTest.java similarity index 100% rename from iindicatorbinder/src/androidTest/java/com/avontell/pagerindicatorbinder/ApplicationTest.java rename to indicatorbinder/src/androidTest/java/com/avontell/pagerindicatorbinder/ApplicationTest.java diff --git a/iindicatorbinder/src/main/AndroidManifest.xml b/indicatorbinder/src/main/AndroidManifest.xml similarity index 100% rename from iindicatorbinder/src/main/AndroidManifest.xml rename to indicatorbinder/src/main/AndroidManifest.xml diff --git a/iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/ImageAdapter.java b/indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/ImageAdapter.java similarity index 100% rename from iindicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/ImageAdapter.java rename to indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/ImageAdapter.java diff --git a/indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java b/indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java new file mode 100644 index 0000000..0d6731f --- /dev/null +++ b/indicatorbinder/src/main/java/com/avontell/pagerindicatorbinder/IndicatorBinder.java @@ -0,0 +1,205 @@ +package com.avontell.pagerindicatorbinder; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.annotation.ColorRes; +import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; +import android.support.v4.view.ViewPager; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ScrollView; +import android.widget.TextView; + +import java.util.List; + +/** + * A class that binds together a ViewPager with a view that supports + * children. The position of the ViewPager will update the view + * with the children such that the current position is indicated + * @author Aaron Vontell + * @version 0.1 + */ +public class IndicatorBinder { + + /** + * Binds the viewPager to indicatorContainer, such that indicatorContainer has a list of + * indicators, all displaying the drawable at indicatorOffResource except for the ith child, + * where i is the position of the viewPager, and whose visual is given by indicatorOffResource. + * * Note that this attaches an OnPageChangeListener to viewPager. Also note that viewPager + * must have an adapter attached. + * @param context The calling application + * @param viewPager The ViewPager you wish to attach indicators to + * @param indicatorContainer The container that will hold each indicator resource + * @param indicatorOnResource The drawable to display for indicators that are not selected + * @param indicatorOffResource The drawable to display for the selected indicator + */ + public static void bind(@NonNull final Context context, + @NonNull final ViewPager viewPager, + @NonNull final ViewGroup indicatorContainer, + @DrawableRes final int indicatorOnResource, + @DrawableRes final int indicatorOffResource) { + + // Load the indicator drawables + final Drawable selected = context.getResources().getDrawable(indicatorOnResource); + final Drawable unselected = context.getResources().getDrawable(indicatorOffResource); + + // Load the container with indicators + final int numItems = viewPager.getAdapter().getCount(); + indicatorContainer.removeAllViews(); + for(int i = 0; i < numItems; i++) { + ImageView indImage = new ImageView(context); + if(i == viewPager.getCurrentItem()) { + indImage.setImageDrawable(selected); + } else { + indImage.setImageDrawable(unselected); + } + indicatorContainer.addView(indImage); + } + + // Listener to detect when to update indicators + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + + for(int i = 0; i < numItems; i++){ + ImageView indicator = (ImageView) indicatorContainer.getChildAt(i); + if(i == position){ + indicator.setImageDrawable(selected); + } + else { + indicator.setImageDrawable(unselected); + } + } + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + + } + + /** + * Binds the viewPager to tabContainer, such that tabContainer serves as a list of + * clickable tabs that map to sections of the ViewPager. Pass in a list of TextViews to use in the + * tab (must be equal to viewPager.getAdapter().getCount()), background colors, and text colors for the + * selected and unselected states + * @param context The calling application + * @param viewPager The ViewPager you wish to attach tabs to + * @param tabContainer The Linear Layout that will hold each tab TextView + * @param tabViews The list of tabs that you wish to use + * @param backgroundSelectedColor The background color resource for a selected tab + * @param backgroundUnselectedColor The background color resource for an unselected tab + * @param textSelectedColor The text color resource for a selected tab + * @param textUnselectedColor The text color resource for an unselected tab + */ + public static void bindTextTabs( + @NonNull final Context context, + @NonNull final ViewPager viewPager, + @NonNull final LinearLayout tabContainer, + @NonNull final List tabViews, + @ColorRes final int backgroundSelectedColor, + @ColorRes final int backgroundUnselectedColor, + @ColorRes final int textSelectedColor, + @ColorRes final int textUnselectedColor) { + + // Check that the correct number of tabs are available + if(viewPager.getAdapter().getCount() != tabViews.size()) { + throw new RuntimeException("Expected " + viewPager.getAdapter().getCount() + + " tab views, but found " + tabViews.size()); + } + + // Load tabView into tabContainer + for(int i = 0; i < tabViews.size(); i++) { + + TextView tab = tabViews.get(i); + + // Make sure each TextView takes the right amount of space and gravity + tab.setLayoutParams( + new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f)); + tab.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); + + // Set colors of the TextView + if(i == viewPager.getCurrentItem()) { + tab.setBackgroundColor(context.getResources().getColor(backgroundSelectedColor)); + tab.setTextColor(context.getResources().getColor(textSelectedColor)); + } else { + tab.setBackgroundColor(context.getResources().getColor(backgroundUnselectedColor)); + tab.setTextColor(context.getResources().getColor(textUnselectedColor)); + } + + // Set Click Listeners of the tabs + final Integer pos = Integer.valueOf(i); + tab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + viewPager.setCurrentItem(pos, true); + } + }); + + tabContainer.addView(tab); + + } + + // Set listener for ViewPager changes + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + + for(int i = 0; i < tabViews.size(); i++){ + TextView tab = (TextView) tabContainer.getChildAt(i); + if(i == position) { + tab.setBackgroundColor(context.getResources().getColor(backgroundSelectedColor)); + tab.setTextColor(context.getResources().getColor(textSelectedColor)); + } else { + tab.setBackgroundColor(context.getResources().getColor(backgroundUnselectedColor)); + tab.setTextColor(context.getResources().getColor(textUnselectedColor)); + } + } + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + + } + + /** + * Binds the scrollView to indicatorContainer, such that indicatorContainer has a list of + * indicators, all displaying the drawable at indicatorOffResource except for the ith child, + * where i is the ith segment of the scrollview, and each segment is a child in the ScrollView's + * immediate ViewGroup + * @param context The calling application + * @param scrollView The scrollView you wish to attach indicators to + * @param indicatorContainer The container that will hold each indicator resource + * @param indicatorOnResource The drawable to display for indicators that are not selected + * @param indicatorOffResource The drawable to display for the selected indicator + */ + public static void bindScroll(final @NonNull Context context, + @NonNull ScrollView scrollView, + @NonNull final ViewGroup indicatorContainer, + @DrawableRes final int indicatorOnResource, + @DrawableRes final int indicatorOffResource) { + + throw new RuntimeException("Not yet implemented"); + + } + +} diff --git a/iindicatorbinder/src/main/res/values/strings.xml b/indicatorbinder/src/main/res/values/strings.xml similarity index 100% rename from iindicatorbinder/src/main/res/values/strings.xml rename to indicatorbinder/src/main/res/values/strings.xml diff --git a/iindicatorbinder/src/test/java/com/avontell/pagerindicatorbinder/ExampleUnitTest.java b/indicatorbinder/src/test/java/com/avontell/pagerindicatorbinder/ExampleUnitTest.java similarity index 100% rename from iindicatorbinder/src/test/java/com/avontell/pagerindicatorbinder/ExampleUnitTest.java rename to indicatorbinder/src/test/java/com/avontell/pagerindicatorbinder/ExampleUnitTest.java diff --git a/readme.md b/readme.md index 2dcff1b..096f35e 100644 --- a/readme.md +++ b/readme.md @@ -11,12 +11,28 @@ the indicators. With PagerIndicatorBinder, one simply uses one method to bind a ViewGroup that will hold the indicators, and is therefore very easy to use and open to modification. +Also feel free to reference the sample application. + ## Referencing the Library The library may be referenced by either downloading and importing the library directly, referencing it as a Maven dependency, or referencing it as a Gradle dependency. -Put dependencies here +1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories: +```groovy +allprojects { + repositories { + ... + maven { url "https://jitpack.io" } + } +} +``` +2. Add the dependency +```grrovy +dependencies { + compile 'com.github.vontell:IndicatorBinder:0.1.1' +} +``` ## Usage @@ -28,6 +44,9 @@ ViewPager to the container: This will populate the viewGroup with indicators, and presto! You have indicators for your ViewPager. +**NEW**: You can now add **text tabs** to your ViewPagers with this simple method (custom view coming soon): +`IndicatorBinder.bindTextTabs(context, viewPager, tabContainer, tabViews, R.color.selectedBackgroundColor, R.color.unselectedBackgroundColor, R.color.selectedTextColor, R.color.unselectedTextColor);` + NOTE: Full documentation can be found within the wiki! ## License diff --git a/settings.gradle b/settings.gradle index 5c02770..ee64988 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':iindicatorbinder' +include ':app', ':indicatorbinder'