From 8ffa05b15868a78334a943201364b69a62594f0f Mon Sep 17 00:00:00 2001 From: Claes Hallstrom Date: Thu, 5 Feb 2015 09:15:36 +0100 Subject: [PATCH] Added view pager indicator --- .../claha/showtimeremote/ShowtimeRemote.java | 47 ++++-------- .../base/BaseFragmentPagerAdapter.java | 43 +++++++++++ .../base/BaseViewPagerIndicator.java | 75 +++++++++++++++++++ .../res/layout/activity_showtime_remote.xml | 9 ++- app/src/main/res/layout/indicator.xml | 4 + 5 files changed, 144 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/claha/showtimeremote/base/BaseFragmentPagerAdapter.java create mode 100644 app/src/main/java/com/claha/showtimeremote/base/BaseViewPagerIndicator.java create mode 100644 app/src/main/res/layout/indicator.xml diff --git a/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java b/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java index 6890990..2804bb9 100644 --- a/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java +++ b/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java @@ -22,17 +22,21 @@ import com.claha.showtimeremote.adapter.CircularPagerAdapter; import com.claha.showtimeremote.base.BaseActivity; import com.claha.showtimeremote.base.BaseFragment; +import com.claha.showtimeremote.base.BaseFragmentPagerAdapter; +import com.claha.showtimeremote.base.BaseViewPagerIndicator; import com.claha.showtimeremote.core.GitHubHTTP; import com.claha.showtimeremote.core.ShowtimeHTTP; import com.claha.showtimeremote.core.ShowtimeNotification; import com.claha.showtimeremote.core.ShowtimeSettings; +import java.util.ArrayList; import java.util.List; public class ShowtimeRemote extends BaseActivity { private ViewPager viewPagerBottom; private ViewPager viewPagerMain; + private BaseViewPagerIndicator viewPagerIndicator; private ShowtimeHTTP showtimeHTTP; private ShowtimeSettings showtimeSettings; @@ -42,8 +46,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_showtime_remote); - viewPagerBottom = (ViewPager) findViewById(R.id.viewPagerBottom); - viewPagerMain = (ViewPager) findViewById(R.id.viewPagerMain); + viewPagerBottom = (ViewPager)findViewById(R.id.viewPagerBottom); + viewPagerMain = (ViewPager)findViewById(R.id.viewPagerMain); + viewPagerIndicator = (BaseViewPagerIndicator)findViewById(R.id.viewPagerIndicator); showtimeHTTP = new ShowtimeHTTP(getApplicationContext()); showtimeSettings = new ShowtimeSettings(getApplicationContext()); @@ -80,13 +85,17 @@ private void setupAdapters() { ProfileAdapter adapter = new ProfileAdapter(viewPagerBottom, profiles); viewPagerBottom.setAdapter(adapter); - Log.d("ShowtimeDebug", "setupAdapters: " + showtimeSettings.getCurrentProfile()); - int index = showtimeSettings.getCurrentProfileIndex() + 1; viewPagerBottom.setCurrentItem(index); - RemoteFragmentPagerAdapter adapter2 = new RemoteFragmentPagerAdapter(getSupportFragmentManager()); + List> fragments = new ArrayList<>(); + fragments.add(NavigationFragment.class); + fragments.add(MediaFragment.class); + + BaseFragmentPagerAdapter adapter2 = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments); viewPagerMain.setAdapter(adapter2); + + viewPagerIndicator.setViewPager(viewPagerMain); } private void setupNotifications() { @@ -190,32 +199,4 @@ public void onPageSelected(int position) { showtimeSettings.chooseProfile(showtimeSettings.getProfiles().get(position)); } } - - private class RemoteFragmentPagerAdapter extends FragmentPagerAdapter { - - private final int NUM_FRAGMENTS = 2; - - public RemoteFragmentPagerAdapter(FragmentManager fm) { - super(fm); - } - - @Override - public Fragment getItem(int position) { - switch (position) { - case 0: - return new NavigationFragment(); - case 1: - return new MediaFragment(); - default: - return null; - } - } - - @Override - public int getCount() { - return NUM_FRAGMENTS; - } - } - - } \ No newline at end of file diff --git a/app/src/main/java/com/claha/showtimeremote/base/BaseFragmentPagerAdapter.java b/app/src/main/java/com/claha/showtimeremote/base/BaseFragmentPagerAdapter.java new file mode 100644 index 0000000..f97233c --- /dev/null +++ b/app/src/main/java/com/claha/showtimeremote/base/BaseFragmentPagerAdapter.java @@ -0,0 +1,43 @@ +package com.claha.showtimeremote.base; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; + +import java.util.List; + +public class BaseFragmentPagerAdapter extends FragmentStatePagerAdapter { + + private List> fragments; + + public BaseFragmentPagerAdapter(FragmentManager fragmentManager, List> fragments) { + super(fragmentManager); + this.fragments = fragments; + } + + @Override + public Fragment getItem(int position) { + try { + return fragments.get(position).newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public int getCount() { + return fragments.size(); + } + + @Override + public int getItemPosition(Object object) { + return PagerAdapter.POSITION_NONE; + } + + public void setFragments(List> fragments) { + this.fragments = fragments; + notifyDataSetChanged(); + } +} diff --git a/app/src/main/java/com/claha/showtimeremote/base/BaseViewPagerIndicator.java b/app/src/main/java/com/claha/showtimeremote/base/BaseViewPagerIndicator.java new file mode 100644 index 0000000..27909d5 --- /dev/null +++ b/app/src/main/java/com/claha/showtimeremote/base/BaseViewPagerIndicator.java @@ -0,0 +1,75 @@ +package com.claha.showtimeremote.base; + +import android.content.Context; +import android.database.DataSetObserver; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.util.Log; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.RadioGroup; + +import com.claha.showtimeremote.R; + +public class BaseViewPagerIndicator extends RadioGroup implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener { + + private ViewPager viewPager; + + public BaseViewPagerIndicator(Context context) { + super(context, null); + } + + public BaseViewPagerIndicator(Context context, AttributeSet attrs) { + super(context, attrs); + setOrientation(LinearLayout.HORIZONTAL); + setOnCheckedChangeListener(this); + } + + private void createIndicators() { + Log.d("DEBUG", "createIndicators: " + viewPager.getAdapter().getCount()); + removeAllViews(); + for (int i = 0; i < viewPager.getAdapter().getCount(); i++) { + View indicator = inflate(getContext(), R.layout.indicator, null); + addView(indicator); + } + setChecked(viewPager.getCurrentItem()); + } + + public void setViewPager(ViewPager viewPager) { + this.viewPager = viewPager; + this.viewPager.setOnPageChangeListener(this); + createIndicators(); + + this.viewPager.getAdapter().registerDataSetObserver(new DataSetObserver() { + @Override + public void onChanged() { + createIndicators(); + } + }); + } + + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + if (viewPager != null) { + int index = group.indexOfChild(group.findViewById(checkedId)); + viewPager.setCurrentItem(index); + } + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + } + + @Override + public void onPageSelected(int position) { + setChecked(position); + } + + @Override + public void onPageScrollStateChanged(int state) { + } + + private void setChecked(int position) { + check(getChildAt(position).getId()); + } +} diff --git a/app/src/main/res/layout/activity_showtime_remote.xml b/app/src/main/res/layout/activity_showtime_remote.xml index d415d6f..dfc7afd 100644 --- a/app/src/main/res/layout/activity_showtime_remote.xml +++ b/app/src/main/res/layout/activity_showtime_remote.xml @@ -8,7 +8,14 @@ android:id="@+id/viewPagerMain" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_above="@+id/viewPagerBottom" /> + android:layout_above="@+id/viewPagerIndicator" /> + + + \ No newline at end of file