Skip to content

Commit

Permalink
Merge pull request #13 from vontell/dev
Browse files Browse the repository at this point in the history
Dev into Master
  • Loading branch information
vontell authored Jul 29, 2016
2 parents b00fd4f + 855a04c commit 08226f8
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,6 +14,11 @@
import java.util.ArrayList;
import java.util.List;

/**
* MainActivity that provides examples for using the IndicatorBinder
* @author Aaron Vontell, Egli Hila
* @version 0.3
*/
public class MainActivity extends AppCompatActivity {

@Override
Expand All @@ -27,18 +34,23 @@ 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<Integer> 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
int selectedImage = R.drawable.indicator_selected;
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,
Expand All @@ -52,7 +64,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,
Expand All @@ -64,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
// EXAMPLE WITH TABS -----------------------------------------------------------------------

// Create each tab TextView
List<TextView> tabViews = new ArrayList<>();
final List<TextView> tabViews = new ArrayList<>();
tabViews.add(new TextView(this));
tabViews.add(new TextView(this));
tabViews.add(new TextView(this));
Expand All @@ -85,7 +97,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,
Expand All @@ -94,5 +106,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();
}
});
}
}
131 changes: 89 additions & 42 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,51 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="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">
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/tab_container"/>
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.avontell.pagerindicatorbinderexample.MainActivity">

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/view_pager_one"
android:layout_below="@+id/tab_container"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="15dp"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:id="@+id/tabe_container_text"
android:text="Tab Indicator"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/tabe_container_text"
android:id="@+id/tab_container"/>

</android.support.v4.view.ViewPager>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/view_pager_one"
android:layout_below="@+id/tab_container"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

</android.support.v4.view.ViewPager>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="15dp"
android:textSize="18sp"
android:layout_below="@+id/view_pager_one"
android:id="@+id/indicator_default_container_text"
android:text="Default Indicator"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/indicator_default"
android:layout_marginTop="8dp"
android:layout_below="@+id/indicator_default_container_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center_horizontal"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="15dp"
android:textSize="18sp"
android:layout_below="@+id/indicator_default"
android:id="@+id/indicator_progress_container_text"
android:text="Progress Indicator"/>


<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:id="@+id/indicator_cont"
android:layout_below="@+id/indicator_progress_container_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

</LinearLayout>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/indicator_cont"
android:layout_alignParentBottom="true"
android:id="@+id/add_to_adapter_btn"
android:text="Add Element to Adapter "/>


</RelativeLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:id="@+id/indicator_cont"
android:layout_below="@+id/indicator_default"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/indicator_default"
android:layout_marginTop="8dp"
android:layout_below="@+id/view_pager_one"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center_horizontal"></LinearLayout>

</ScrollView>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
* Class that serves ImageViews for a view pager, for effects such as a carousel
* @author Aaron Vontell
Expand All @@ -14,21 +16,21 @@
public class ImageAdapter extends PagerAdapter{

Context context;
int[] imageResources;
List<Integer> imageResources;

/**
* Create an adapter that handles a slideshow of images
* @param context The calling activity
* @param imageResources The images to use within the slideshow, as drawable resources
*/
public ImageAdapter(Context context, int[] imageResources) {
public ImageAdapter(Context context, List<Integer> imageResources) {
this.context = context;
this.imageResources = imageResources;
}

@Override
public int getCount() {
return imageResources.length;
return imageResources.size();
}

@Override
Expand All @@ -44,7 +46,7 @@ public Object instantiateItem(ViewGroup container, int position) {
//If this is an image, add an ImageView to the pager
//Otherwise, add the extra view to the pager
ImageView itemView = new ImageView(context);
itemView.setImageResource(imageResources[position]);
itemView.setImageResource(imageResources.get(position));
itemView.setScaleType(ImageView.ScaleType.CENTER_CROP);
container.addView(itemView);
result = itemView;
Expand Down
Loading

0 comments on commit 08226f8

Please sign in to comment.