Created
November 16, 2011 09:16
-
-
Save anonymous/1369653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PagerActivity extends FragmentActivity implements OnSelectInterestChanged { | |
private ViewPager mViewPager; | |
private TabsAdapter mTabsAdapter; | |
private ActionBar.Tab tSearch, tInterest; | |
private SearchFragment searchInterests; | |
private SelectFragment selectInterests; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.pager); | |
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
tSearch = getSupportActionBar().newTab().setText(R.string.abs_tab_search); | |
tInterest = getSupportActionBar().newTab().setText(R.string.abs_tab_interest); | |
mViewPager = (ViewPager) findViewById(R.id.pager_viewPager); | |
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager); | |
mViewPager.setAdapter(mTabsAdapter); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
searchInterests = new SearchFragment(); | |
selectInterests = new SelectFragment(); | |
mTabsAdapter.addTab(tInterest, selectInterests.getClass()); | |
mTabsAdapter.addTab(tSearch, searchInterests.getClass()); | |
mViewPager.setAdapter(mTabsAdapter); | |
// Check to see if this is the user's first run | |
switch (SomeActivity.userRun) { | |
case SomeActivity.VETERAN: | |
mTabsAdapter.setPrimaryItem(mTabsAdapter.getClassPosition(searchInterests.getClass().getName())); | |
break; | |
case SomeActivity.NOVICE: | |
mTabsAdapter.setPrimaryItem(mTabsAdapter.getClassPosition(selectInterests.getClass().getName())); | |
break; | |
} | |
} | |
public void onChangedInterest() { | |
} | |
public static class TabsAdapter extends FragmentPagerAdapter implements OnPageChangeListener, ActionBar.TabListener{ | |
private final Context mContext; | |
private final ActionBar mActionBar; | |
private final ViewPager mViewPager; | |
private final ArrayList<String> mTabs = new ArrayList<String>(); | |
public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) { | |
super(activity.getSupportFragmentManager()); | |
mContext = activity; | |
mActionBar = actionBar; | |
mViewPager = pager; | |
mViewPager.setAdapter(this); | |
mViewPager.setOnPageChangeListener(this); | |
} | |
public int getClassPosition(String name) { | |
if(mTabs.contains(name)) { | |
for(int x = 0; x < mTabs.size(); x++) { | |
if(mTabs.get(x).equalsIgnoreCase(name)) { | |
return x; | |
} | |
} | |
} | |
return -1; | |
} | |
public void addTab(ActionBar.Tab tab, Class<?> clss) { | |
mTabs.add(clss.getName()); | |
mActionBar.addTab(tab.setTabListener(this)); | |
notifyDataSetChanged(); | |
} | |
public void onTabReselected(Tab tab, FragmentTransaction ft) { | |
} | |
public void onTabSelected(Tab tab, FragmentTransaction ft) { | |
mViewPager.setCurrentItem(tab.getPosition()); | |
} | |
public void onTabUnselected(Tab tab, FragmentTransaction ft) { | |
// Nothing happens yet | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return Fragment.instantiate(mContext, mTabs.get(position), null); | |
} | |
@Override | |
public int getCount() { | |
return mTabs.size(); | |
} | |
public void onPageScrolled(int position, float positionOffset, | |
int positionOffsetPixels) { | |
// Nothing happens yet | |
} | |
public void onPageSelected(int position) { | |
mActionBar.setSelectedNavigationItem(position); | |
} | |
public void onPageScrollStateChanged(int state) { | |
// Nothing happens yet | |
} | |
/* | |
* Set a primary item to be shown first. | |
*/ | |
public void setPrimaryItem(int position) { | |
mViewPager.setCurrentItem(position); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchFragment extends Fragment{ | |
private String mInterests; | |
private String mSentence; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.bounty_search_layout, container, false); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
getUserInterests(); | |
} | |
/* | |
* Get the users interests from the underlying | |
* data store. | |
*/ | |
public void getUserInterests() { | |
TextView tv = (TextView) getView().findViewById(R.id.bounty_search_txtResult); | |
DatabaseHelper dbHelper = new DatabaseHelper(getActivity()); | |
SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
Cursor cursor = db.query(DatabaseConstants.TABLE_NAME, null, null, null, null, null, null); | |
cursor.moveToFirst(); | |
mInterests = cursor.getString(cursor.getColumnIndex(DatabaseConstants.INTERESTS)); | |
if(mInterests.length() == 0) { | |
mSentence = "You have no interests."; | |
} | |
else { | |
mSentence = "Your interests are: "; | |
mInterests = cursor.getString(cursor.getColumnIndex(DatabaseConstants.INTERESTS)); | |
} | |
tv.setText(mSentence + mInterests); | |
cursor.close(); | |
db.close(); | |
dbHelper.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment