Creating Fragments from List with tabs
After way to long of a time between learning to code and now, I am trying
to create an Android app that is using a card list view and use tabs.
I want to be able to have the cards list in all 3 tabs but a different
list in each.Right now with what I have done, I get the same set in all 3
tabs. I know I am doing something wrong but after reading on Fragments and
tabs, I cannot figure out how to implement this.
I have the following in my MainActivity.
public class MainActivity extends FragmentActivity implements
Card.CardMenuListener<Card>{
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private Drawable oldBackground = null;
private int currentColor = 0xFF3F9FE0;
@Override
public void onCreate(Bundle savedInstanceState) {
// This is quick way of theming the action bar without using
styles.xml (e.g. using ActionBar Style Generator)
getActionBar().setBackgroundDrawable(new
ColorDrawable(getResources().getColor(android.R.color.holo_blue_dark)));
getActionBar().setDisplayShowHomeEnabled(false);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new
MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
// Initializes a CardAdapter with a blue accent color and
basic popup menu for each card
CardAdapter<Card> cardsAdapter = new CardAdapter<Card>(this)
.setAccentColorRes(android.R.color.holo_blue_light)
.setPopupMenu(R.menu.card_popup, this);
cardsAdapter.add(new CardHeader(this, R.string.themeheader));
cardsAdapter.add(new Card("Action", "Launcher")
.setThumbnail(this,
R.drawable.apps_actionlauncherpro)); // sets
a thumbnail image from drawable resources
cardsAdapter.add(new Card("ADW", "Launcher")
.setThumbnail(this,
R.drawable.apps_adwex)); // sets a
thumbnail image from drawable
resources
cardsAdapter.add(new Card("Apex", "Launcher")
.setThumbnail(this,
R.drawable.apps_apexlauncher)); //
sets a thumbnail image from drawable
resources
cardsAdapter.add(new Card("Nova", "Launcher")
.setThumbnail(this,
R.drawable.apps_novalauncher)); //
sets a thumbnail image from drawable
resources
CardListView cardsList = (CardListView)
findViewById(R.id.cardsList);
cardsList.setAdapter(cardsAdapter);
final int pageMargin = (int)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
}
@Override
public void onMenuItemClick(Card card, MenuItem item) {
Toast.makeText(this, card.getTitle() + ": " + item.getTitle(),
Toast.LENGTH_SHORT).show();
}
I have this to set the tabs to provide the 3 that I want
public class MyPagerAdapter extends FragmentPagerAdapter {
private final int[] TITLES = { R.string.tab1, R.string.tab2,
R.string.tab3 };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
case 2:
return getString(R.string.tab3).toUpperCase(l);
}
return null;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=ThemeCardFragment.newInstance(position);
break;
case 1:
f=ThemeCardFragment.newInstance(position);
break;
}
return f;
}
@Override
public int getCount() {
return TITLES.length;
}
I placed the following in my Fragment which I am sure is wrong.
public class ThemeCardFragment extends Fragment {
private static final String ARG_POSITION = "position";
private int position;
public static ThemeCardFragment newInstance(int position) {
ThemeCardFragment f = new ThemeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(getActivity());
fl.setLayoutParams(params);
return fl;
}
}
I have tried to look at examples on here, tutorials and the Android dev
but I am missing something that would allow me to do this.
Realistically I would like to define the
("Action", "Launcher")
in the CardsAdapter as strings from my Strings.xml
Any insight to pointing me in the right direction would be appreciated. I
seem to keep heading in wrong directions and have spent at least 10 hours
trying to figure it out. Thanks.
No comments:
Post a Comment