From febef54e245027d6806af5bd9e9f9a6d72aba523 Mon Sep 17 00:00:00 2001 From: EHila Date: Thu, 28 Jul 2016 20:13:58 -0400 Subject: [PATCH 1/2] Added invalidate option to refresh indicators when new elements are added --- .../MainActivity.java | 43 ++++- app/src/main/res/layout/activity_main.xml | 131 +++++++++----- .../pagerindicatorbinder/ImageAdapter.java | 10 +- .../pagerindicatorbinder/IndicatorBinder.java | 167 ++++++++++++------ 4 files changed, 244 insertions(+), 107 deletions(-) diff --git a/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java b/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java index f4211c7..67379af 100644 --- a/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java +++ b/app/src/main/java/com/avontell/pagerindicatorbinderexample/MainActivity.java @@ -3,6 +3,8 @@ import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; @@ -27,10 +29,15 @@ protected void onCreate(Bundle savedInstanceState) { LinearLayout indicatorContainer = (LinearLayout) findViewById(R.id.indicator_cont); // Populate ViewPager with necessary information - int[] drawableResources = {R.drawable.image_one, R.drawable.image_two, - R.drawable.image_three, R.drawable.image_four, - R.drawable.image_five}; - ImageAdapter adapter = new ImageAdapter(this, drawableResources); + final ArrayList drawableResources = new ArrayList<>(); + + drawableResources.add(R.drawable.image_one); + drawableResources.add(R.drawable.image_two); + drawableResources.add(R.drawable.image_three); + drawableResources.add(R.drawable.image_four); + drawableResources.add(R.drawable.image_five); + + final ImageAdapter adapter = new ImageAdapter(this, drawableResources); viewPager.setAdapter(adapter); // Get resources for drawables @@ -38,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) { int unselectedImage = R.drawable.indicator_unselected; // Bind the view pager to the indicatorContainer - IndicatorBinder sample = new IndicatorBinder().bind(this, + final IndicatorBinder sample = new IndicatorBinder().bind(this, viewPager, indicatorContainer, selectedImage, @@ -52,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) { LinearLayout indicatorContainerDefault = (LinearLayout) findViewById(R.id.indicator_default); // Bind the view pager to the indicatorContainer - IndicatorBinder defaultInd = new IndicatorBinder().bind(this, + final IndicatorBinder defaultInd = new IndicatorBinder().bind(this, viewPager, indicatorContainerDefault, selectedImage, @@ -64,7 +71,7 @@ protected void onCreate(Bundle savedInstanceState) { // EXAMPLE WITH TABS ----------------------------------------------------------------------- // Create each tab TextView - List tabViews = new ArrayList<>(); + final List tabViews = new ArrayList<>(); tabViews.add(new TextView(this)); tabViews.add(new TextView(this)); tabViews.add(new TextView(this)); @@ -85,7 +92,7 @@ protected void onCreate(Bundle savedInstanceState) { // Grab the LinearLayout LinearLayout tabContainer = (LinearLayout) findViewById(R.id.tab_container); - new IndicatorBinder().bindTextTabs(this, + final IndicatorBinder tabBinder = new IndicatorBinder().bindTextTabs(this, viewPager, tabContainer, tabViews, @@ -94,5 +101,25 @@ protected void onCreate(Bundle savedInstanceState) { selectedTextColor, unselectedTextColor); + // Button to add more images to viewPager + Button addBtn = (Button) findViewById(R.id.add_to_adapter_btn); + + addBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + // Create new textview for adapter + TextView textView = new TextView(MainActivity.this); + textView.setText("Tab " + (tabViews.size() + 1)); + + // Add image to viewpager + drawableResources.add(R.drawable.image_one); + // Add textview to tab views + tabViews.add(textView); + // Refresh all three sample data sets + tabBinder.invalidate(); + sample.invalidate(); + defaultInd.invalidate(); + } + }); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 47aa5af..2e8230f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,51 +1,98 @@ - + android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:paddingBottom="@dimen/activity_vertical_margin" + android:paddingTop="@dimen/activity_vertical_margin" + tools:context="com.avontell.pagerindicatorbinderexample.MainActivity"> - + + - + + + + + + + + + + + + + + + +