Skip to content
This repository has been archived by the owner on May 20, 2020. It is now read-only.

Commit

Permalink
Added view pager indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Feb 5, 2015
1 parent 78e6699 commit 8ffa05b
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 34 deletions.
47 changes: 14 additions & 33 deletions app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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<Class<? extends BaseFragment>> 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() {
Expand Down Expand Up @@ -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;
}
}


}
Original file line number Diff line number Diff line change
@@ -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<Class<? extends BaseFragment>> fragments;

public BaseFragmentPagerAdapter(FragmentManager fragmentManager, List<Class<? extends BaseFragment>> 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<Class<? extends BaseFragment>> fragments) {
this.fragments = fragments;
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
9 changes: 8 additions & 1 deletion app/src/main/res/layout/activity_showtime_remote.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<com.claha.showtimeremote.base.BaseViewPagerIndicator
android:id="@+id/viewPagerIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/viewPagerBottom"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewPagerBottom"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/layout/indicator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

0 comments on commit 8ffa05b

Please sign in to comment.