Commit 60a03b86 authored by Robert Hitt's avatar Robert Hitt
Browse files

We got tabs now!

- Currently they all show the same stuff, however
- Also, "Favorites" is cut off
parent 1552de91
......@@ -26,6 +26,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'io.realm:android-adapters:1.4.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
......
......@@ -32,7 +32,6 @@ import srct.whatsopen.model.OpenTimes;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
* Also contains a bit of logic for figuring out What's Open
*/
public class FacilityListAdapter extends
......
package srct.whatsopen.ui;
import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.Sort;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
public class FacilityListFragment extends android.support.v4.app.Fragment {
public static final String ARG_MODE = "ARG_MODE";
private String mMode;
private Realm mRealm;
private RecyclerView mRecyclerView;
public static FacilityListFragment newInstance(String mode) {
Bundle args = new Bundle();
args.putString(ARG_MODE, mode);
FacilityListFragment fragment = new FacilityListFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMode = getArguments().getString(ARG_MODE);
mRealm = Realm.getDefaultInstance();
}
@Override
public void onDestroy() {
super.onDestroy();
mRealm.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_facility, container, false);
mRecyclerView = ButterKnife.findById(view, R.id.rvFacilities);
setUpRecyclerView(view);
return view;
}
// Handles set up for the Recycler View
private void setUpRecyclerView(View view) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
switch(mMode) {
case "All":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Favorites":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Open":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Closed":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
default:
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
}
// Speeds things up for static lists
mRecyclerView.setHasFixedSize(true);
// Adds dividers between items
Drawable dividerDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(dividerDrawable));
}
}
package srct.whatsopen.ui;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FacilityListFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[] {"All", "Favorites", "Open", "Closed"};
public FacilityListFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FacilityListFragment.newInstance(tabTitles[position]);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
......@@ -4,10 +4,14 @@ import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.astuetz.PagerSlidingTabStrip;
import java.text.ParseException;
import java.text.SimpleDateFormat;
......@@ -49,9 +53,13 @@ public class MainActivity extends AppCompatActivity {
WhatsOpenService service = WhatsOpenClient.getInstance();
callWhatsOpenAPI(service);
// Set up view
mRecyclerView = ButterKnife.findById(this, R.id.rvFacilities);
setUpRecyclerView();
// Get the ViewPager and set its PagerAdapter
ViewPager viewPager = ButterKnife.findById(this, R.id.view_pager);
viewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setViewPager(viewPager);
}
@Override
......@@ -60,20 +68,6 @@ public class MainActivity extends AppCompatActivity {
mRealm.close();
}
// Handles set up for the Recycler View
private void setUpRecyclerView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new FacilityListAdapter(this,
mRealm.where(Facility.class).findAllSortedAsync("isOpen", Sort.DESCENDING)));
// Speeds things up for static lists
mRecyclerView.setHasFixedSize(true);
// Adds dividers between items
Drawable dividerDrawable = ContextCompat.getDrawable(this, R.drawable.divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(dividerDrawable));
}
// Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB
private void callWhatsOpenAPI(WhatsOpenService service) {
......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context="srct.whatsopen.ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvFacilities"
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
app:pstsShouldExpand="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="48dp"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
tools:context="srct.whatsopen.ui.FacilityListFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvFacilities"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment